diff options
Diffstat (limited to 'php-symfony3-generate-autoloaders.php')
-rwxr-xr-x | php-symfony3-generate-autoloaders.php | 112 |
1 files changed, 103 insertions, 9 deletions
diff --git a/php-symfony3-generate-autoloaders.php b/php-symfony3-generate-autoloaders.php index 0508257..90d2c50 100755 --- a/php-symfony3-generate-autoloaders.php +++ b/php-symfony3-generate-autoloaders.php @@ -10,12 +10,18 @@ $autoloader->register(); 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, "\tgenerate %s\n", $composerFile); + fprintf(STDERR, 'generating autoloader from %s'.PHP_EOL, $composerFile); $autoloadGenerator = new AutoloadGenerator($composerFile); echo $autoloadGenerator->getFilename().PHP_EOL; echo $autoloadGenerator->getDevFilename().PHP_EOL; @@ -65,7 +71,7 @@ final class AutoloadGenerator { ], 'egulias/email-validator' => [ 'prefix' => 'FEDORA_SYMFONY3_PHP_DIR', - 'path' => 'Egulias/EmailValidator/autoload.php', + 'path' => 'Egulias/EmailValidator2/autoload.php', ], 'fig/link-util' => [ 'prefix' => 'FEDORA_SYMFONY3_PHP_DIR', @@ -366,11 +372,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') ); @@ -384,7 +396,12 @@ final class AutoloadGenerator { // autoload-dev.php $content = static::content( - $composerJson, + $composerPath, + $composerJson['name'], + [], + [], + [], + [], static::dependencyAutoloaders($composerJson, 'require-dev'), [], true @@ -465,8 +482,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 @@ -502,22 +529,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 ]); |