diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-27 19:44:09 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-27 20:18:56 +0900 |
| commit | 3cb13f1d7025351615c254b9225a4c5d1bcbd170 (patch) | |
| tree | b2118f2d0c7ad22bd686175739564a3e554b135c /crates/shirabe/src/autoload/autoload_generator.rs | |
| parent | 7c847bf896e80d028c48973b15b681203a1509c1 (diff) | |
| download | php-shirabe-3cb13f1d7025351615c254b9225a4c5d1bcbd170.tar.gz php-shirabe-3cb13f1d7025351615c254b9225a4c5d1bcbd170.tar.zst php-shirabe-3cb13f1d7025351615c254b9225a4c5d1bcbd170.zip | |
fix(autoload): correct generated autoloader blank lines
PHP heredocs drop the newline before the closing marker, so a two-blank-line
fragment must port to "\n\n", not "\n\n\n", and the static initializer needs a
"\n" after the $initializer placeholder. This makes autoload_real.php and
autoload_static.php byte-identical to Composer's output. Rewrite the templates
as raw strings mirroring the PHP heredocs.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/autoload/autoload_generator.rs')
| -rw-r--r-- | crates/shirabe/src/autoload/autoload_generator.rs | 98 |
1 files changed, 87 insertions, 11 deletions
diff --git a/crates/shirabe/src/autoload/autoload_generator.rs b/crates/shirabe/src/autoload/autoload_generator.rs index 1d0960d..133348c 100644 --- a/crates/shirabe/src/autoload/autoload_generator.rs +++ b/crates/shirabe/src/autoload/autoload_generator.rs @@ -1322,26 +1322,67 @@ impl AutoloadGenerator { check_platform: bool, ) -> String { let mut file = format!( - "<?php\n\n// autoload_real.php @generated by Composer\n\nclass ComposerAutoloaderInit{}\n{{\n private static $loader;\n\n public static function loadClassLoader($class)\n {{\n if ('Composer\\Autoload\\ClassLoader' === $class) {{\n require __DIR__ . '/ClassLoader.php';\n }}\n }}\n\n /**\n * @return \\Composer\\Autoload\\ClassLoader\n */\n public static function getLoader()\n {{\n if (null !== self::$loader) {{\n return self::$loader;\n }}\n\n\n", + r#"<?php + +// autoload_real.php @generated by Composer + +class ComposerAutoloaderInit{} +{{ + private static $loader; + + public static function loadClassLoader($class) + {{ + if ('Composer\Autoload\ClassLoader' === $class) {{ + require __DIR__ . '/ClassLoader.php'; + }} + }} + + /** + * @return \Composer\Autoload\ClassLoader + */ + public static function getLoader() + {{ + if (null !== self::$loader) {{ + return self::$loader; + }} + +"#, suffix ); if check_platform { - file.push_str(" require __DIR__ . '/platform_check.php';\n\n\n"); + file.push_str( + r#" require __DIR__ . '/platform_check.php'; + +"#, + ); } file.push_str(&format!( - " spl_autoload_register(array('ComposerAutoloaderInit{}', 'loadClassLoader'), true, {});\n self::$loader = $loader = new \\Composer\\Autoload\\ClassLoader(\\dirname(__DIR__));\n spl_autoload_unregister(array('ComposerAutoloaderInit{}', 'loadClassLoader'));\n\n", + r#" spl_autoload_register(array('ComposerAutoloaderInit{}', 'loadClassLoader'), true, {}); + self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); + spl_autoload_unregister(array('ComposerAutoloaderInit{}', 'loadClassLoader')); + +"#, suffix, prepend_autoloader, suffix )); if use_include_path { - file.push_str(" $includePaths = require __DIR__ . '/include_paths.php';\n $includePaths[] = get_include_path();\n set_include_path(implode(PATH_SEPARATOR, $includePaths));\n\n\n"); + file.push_str( + r#" $includePaths = require __DIR__ . '/include_paths.php'; + $includePaths[] = get_include_path(); + set_include_path(implode(PATH_SEPARATOR, $includePaths)); + +"#, + ); } // keeping PHP 5.6+ compatibility for the autoloader here by using call_user_func vs getInitializer()() file.push_str(&format!( - " require __DIR__ . '/autoload_static.php';\n call_user_func(\\Composer\\Autoload\\ComposerStaticInit{}::getInitializer($loader));\n\n\n", + r#" require __DIR__ . '/autoload_static.php'; + call_user_func(\Composer\Autoload\ComposerStaticInit{}::getInitializer($loader)); + +"#, suffix )); @@ -1370,24 +1411,44 @@ impl AutoloadGenerator { if target_dir_loader.is_some() { file.push_str(&format!( - " spl_autoload_register(array('ComposerAutoloaderInit{}', 'autoload'), true, true);\n\n\n", + r#" spl_autoload_register(array('ComposerAutoloaderInit{}', 'autoload'), true, true); + +"#, suffix )); } file.push_str(&format!( - " $loader->register({});\n\n\n", + r#" $loader->register({}); + +"#, prepend_autoloader )); if use_include_files { file.push_str(&format!( - " $filesToLoad = \\Composer\\Autoload\\ComposerStaticInit{}::$files;\n $requireFile = \\Closure::bind(static function ($fileIdentifier, $file) {{\n if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {{\n $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;\n\n require $file;\n }}\n }}, null, null);\n foreach ($filesToLoad as $fileIdentifier => $file) {{\n $requireFile($fileIdentifier, $file);\n }}\n\n\n", + r#" $filesToLoad = \Composer\Autoload\ComposerStaticInit{}::$files; + $requireFile = \Closure::bind(static function ($fileIdentifier, $file) {{ + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {{ + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; + + require $file; + }} + }}, null, null); + foreach ($filesToLoad as $fileIdentifier => $file) {{ + $requireFile($fileIdentifier, $file); + }} + +"#, suffix )); } - file.push_str(" return $loader;\n }\n\n"); + file.push_str( + r#" return $loader; + } +"#, + ); if let Some(target_dir_loader_str) = target_dir_loader { file.push_str(&target_dir_loader_str); @@ -1404,7 +1465,15 @@ impl AutoloadGenerator { base_path: &str, ) -> String { let mut file = format!( - "<?php\n\n// autoload_static.php @generated by Composer\n\nnamespace Composer\\Autoload;\n\nclass ComposerStaticInit{}\n{{\n", + r#"<?php + +// autoload_static.php @generated by Composer + +namespace Composer\Autoload; + +class ComposerStaticInit{} +{{ +"#, suffix ); @@ -1612,7 +1681,14 @@ impl AutoloadGenerator { } format!( - "{} public static function getInitializer(ClassLoader $loader)\n {{\n return \\Closure::bind(function () use ($loader) {{\n{} }}, null, ClassLoader::class);\n }}\n}}\n", + r#"{} public static function getInitializer(ClassLoader $loader) + {{ + return \Closure::bind(function () use ($loader) {{ +{} + }}, null, ClassLoader::class); + }} +}} +"#, file, initializer ) } |
