diff options
Diffstat (limited to 'php-symfony4-generate-autoloaders.php')
-rwxr-xr-x | php-symfony4-generate-autoloaders.php | 109 |
1 files changed, 102 insertions, 7 deletions
diff --git a/php-symfony4-generate-autoloaders.php b/php-symfony4-generate-autoloaders.php index 557ff68..232435e 100755 --- a/php-symfony4-generate-autoloaders.php +++ b/php-symfony4-generate-autoloaders.php @@ -8,11 +8,18 @@ require_once '__PHPDIR__/Fedora/Autoloader/autoload.php'; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; $finder = new Finder(); -$finder->in(SYMFONY_SOURCE_DIR)->name('composer.json')->sortByName(); +$finder + ->in(SYMFONY_SOURCE_DIR) + ->notPath('Tests') + ->name('composer.json') + ->sortByName(); foreach ($finder as $composerFile) { + fprintf(STDERR, 'generating autoloader from %s'.PHP_EOL, $composerFile); $autoloadGenerator = new AutoloadGenerator($composerFile); echo $autoloadGenerator->getFilename().PHP_EOL; echo $autoloadGenerator->getDevFilename().PHP_EOL; @@ -359,11 +366,17 @@ final class AutoloadGenerator { private $devFilename = null; public function __construct(SplFileInfo $composerFile) { + $composerPath = $composerFile->getPath(); $composerJson = static::composerJson($composerFile); // autoload.php $content = static::content( - $composerJson, + $composerPath, + $composerJson['name'], + isset($composerJson['autoload']['psr-0']) ? $composerJson['autoload']['psr-0'] : [], + isset($composerJson['autoload']['psr-4']) ? $composerJson['autoload']['psr-4'] : [], + isset($composerJson['autoload']['files']) ? $composerJson['autoload']['files'] : [], + isset($composerJson['autoload']['classmap']) ? $composerJson['autoload']['classmap'] : [], static::dependencyAutoloaders($composerJson, 'require'), static::dependencyAutoloaders($composerJson, 'suggest') ); @@ -377,7 +390,12 @@ final class AutoloadGenerator { // autoload-dev.php $content = static::content( - $composerJson, + $composerPath, + $composerJson['name'], + [], + [], + [], + [], static::dependencyAutoloaders($composerJson, 'require-dev'), [], true @@ -458,8 +476,18 @@ final class AutoloadGenerator { : sprintf("%s.'/%s'", $prefix, $path); } - public function content($composerJson, array $dependencyAutoloadersRequired, array $dependencyAutoloadersOptional = [], $dev = false) { - $pkg = explode('/', $composerJson['name'])[1]; + public function content( + $path, + $name, + array $psr0, + array $psr4, + array $files, + array $classmap, + array $dependencyAutoloadersRequired, + array $dependencyAutoloadersOptional, + $dev = false + ) { + $pkg = explode('/', $name)[1]; $content = <<<AUTOLOAD <?php @@ -495,22 +523,89 @@ AUTOLOAD; } } + // PSR-0 + if (!empty($psr0)) { + $content .= PHP_EOL.'// Self PSR-0'.PHP_EOL; + + foreach ($psr0 as $namespace => $directory) { + $content .= sprintf( + "\\Fedora\\Autoloader\\Autoload::addPsr0('%s', %s, true);".PHP_EOL, + str_replace('\\', '\\\\', $namespace), + $directory ? "'$directory'" : '__DIR__' + ); + } + } + + // PSR-4 + if (!empty($psr4)) { + $content .= PHP_EOL.'// Self PSR-4'.PHP_EOL; + + foreach ($psr4 as $namespace => $directory) { + $content .= sprintf( + "\\Fedora\\Autoloader\\Autoload::addPsr4('%s', %s, true);".PHP_EOL, + str_replace('\\', '\\\\', $namespace), + $directory ? "'$directory'" : '__DIR__' + ); + } + } + + // Files + if (!empty($files)) { + $content .= PHP_EOL.'// Self files'.PHP_EOL; + + foreach ($files as $file) { + $content .= sprintf( + "require_once __DIR__.'/$file';".PHP_EOL, + $file + ); + } + } + + // Classmap + if (!empty($classmap)) { + $cmd = array_merge( + [ + 'phpab', + '--template', 'fedora', + '--output', 'autoload.classmap.php', + ], + $classmap + ); + + $process = new Process($cmd, $path); + $process->run(); + + if (!$process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + $content .= <<<SELF_CLASSMAP + +// Self classmap +require_once __DIR__.'/autoload.classmap.php'; + +SELF_CLASSMAP; + } + + // Required dependencies if (!empty($dependencyAutoloadersRequired)) { $dependencyAutoloadersRequiredString = implode(",\n ", $dependencyAutoloadersRequired); $content .= <<<DEPENDENCY_AUTOLOADERS_REQUIRED - +// Required dependencies \Fedora\Autoloader\Dependencies::required([ $dependencyAutoloadersRequiredString ]); + DEPENDENCY_AUTOLOADERS_REQUIRED; } + // Optional dependencies if (!empty($dependencyAutoloadersOptional)) { $dependencyAutoloadersOptionalString = implode(",\n ", $dependencyAutoloadersOptional); $content .= <<<DEPENDENCY_AUTOLOADERS_REQUIRED - +// Optional dependencies \Fedora\Autoloader\Dependencies::optional([ $dependencyAutoloadersOptionalString ]); |