From b5f1bffcfae87331ffe902042a61eb9ff0cb0541 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sun, 16 Oct 2011 20:04:29 +0200 Subject: [PATCH] [autoload] refactor autoload generation code into a AutoloadGenerator --- src/Composer/Autoload/AutoloadGenerator.php | 127 ++++++++++++++++++++ src/Composer/Command/InstallCommand.php | 100 +-------------- 2 files changed, 132 insertions(+), 95 deletions(-) create mode 100644 src/Composer/Autoload/AutoloadGenerator.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php new file mode 100644 index 000000000..be456d2bb --- /dev/null +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -0,0 +1,127 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +use Composer\Installer\InstallationManager; +use Composer\Json\JsonFile; +use Composer\Package\Loader\JsonLoader; +use Composer\Package\PackageInterface; +use Composer\Repository\RepositoryInterface; + +/** + * @author Igor Wiedler + */ +class AutoloadGenerator +{ + private $localRepo; + private $package; + private $installationManager; + + public function __construct(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager) + { + $this->localRepo = $localRepo; + $this->package = $package; + $this->installationManager = $installationManager; + } + + public function dump($targetFilename) + { + $autoloads = $this->parseAutoloads(); + + $file = <<<'EOF' + $path) { + $exportedNamespace = var_export($namespace, true); + $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true); + $file .= <<registerNamespace($exportedNamespace, dirname(__DIR__).$exportedPath); + +EOF; + } + } + } + + if (isset($autoloads['pear'])) { + foreach ($autoloads['pear'] as $def) { + foreach ($def['mapping'] as $prefix => $path) { + $exportedPrefix = var_export($prefix, true); + $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true); + $file .= <<registerPrefix($exportedPrefix, dirname(__DIR__).$exportedPath); + +EOF; + } + } + } + + $file .= <<<'EOF' +$loader->register(); + +EOF; + + file_put_contents($targetFilename, $file); + } + + private function parseAutoloads() + { + $installPaths = array(); + foreach ($this->localRepo->getPackages() as $package) { + $installPaths[] = array( + $this->getFullPackage($package), + $this->installationManager->getInstallPath($package) + ); + } + $installPaths[] = array($package, ''); + + $autoloads = array(); + foreach ($installPaths as $item) { + list($package, $installPath) = $item; + + if (null !== $package->getTargetDir()) { + $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); + } + + foreach ($package->getAutoload() as $type => $mapping) { + $autoloads[$type] = isset($autoloads[$type]) ? $autoloads[$type] : array(); + $autoloads[$type][] = array( + 'mapping' => $mapping, + 'path' => $installPath, + ); + } + } + + return $autoloads; + } + + private function getFullPackage(PackageInterface $package) + { + $path = $this->installationManager->getInstallPath($package); + + $loader = new JsonLoader(); + $fullPackage = $loader->load(new JsonFile($path.'/composer.json')); + + return $fullPackage; + } +} diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 203a41237..b8718c50a 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -12,6 +12,7 @@ namespace Composer\Command; +use Composer\Autoload\AutoloadGenerator; use Composer\DependencyResolver; use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Request; @@ -110,102 +111,11 @@ EOT $localRepo->write(); $output->writeln('> Generating autoload.php'); - $this->generateAutoload($composer, $installationManager); + $localRepo = new \Composer\Repository\FilesystemRepository( + new \Composer\Json\JsonFile('.composer/installed.json')); + $generator = new AutoloadGenerator($localRepo, $composer->getPackage(), $installationManager); + $generator->dump('.composer/autoload.php'); $output->writeln('> Done'); } - - private function generateAutoload(\Composer\Composer $composer, - \Composer\Installer\InstallationManager $installationManager) - { - $localRepo = new \Composer\Repository\FilesystemRepository( - new \Composer\Json\JsonFile('.composer/installed.json')); - - $installPaths = array(); - foreach ($localRepo->getPackages() as $package) { - $installPaths[] = array( - $this->getFullPackage($package, $installationManager), - $installationManager->getInstallPath($package) - ); - } - $installPaths[] = array($composer->getPackage(), ''); - - $autoloads = array(); - foreach ($installPaths as $item) { - list($package, $installPath) = $item; - - if (null !== $package->getTargetDir()) { - $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); - } - - foreach ($package->getAutoload() as $type => $mapping) { - $autoloads[$type] = isset($autoloads[$type]) ? $autoloads[$type] : array(); - $autoloads[$type][] = array( - 'mapping' => $mapping, - 'path' => $installPath, - ); - } - } - - $this->dumpAutoload($autoloads); - } - - private function dumpAutoload(array $autoloads) - { - $file = <<<'EOF' - $path) { - $exportedNamespace = var_export($namespace, true); - $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true); - $file .= <<registerNamespace($exportedNamespace, dirname(__DIR__).$exportedPath); - -EOF; - } - } - } - - if (isset($autoloads['pear'])) { - foreach ($autoloads['pear'] as $def) { - foreach ($def['mapping'] as $prefix => $path) { - $exportedPrefix = var_export($prefix, true); - $exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true); - $file .= <<registerPrefix($exportedPrefix, dirname(__DIR__).$exportedPath); - -EOF; - } - } - } - - $file .= <<<'EOF' -$loader->register(); - -EOF; - - file_put_contents('.composer/autoload.php', $file); - } - - private function getFullPackage(\Composer\Package\PackageInterface $package, - \Composer\Installer\InstallationManager $installationManager) - { - $path = $installationManager->getInstallPath($package); - - $loader = new \Composer\Package\Loader\JsonLoader(); - $fullPackage = $loader->load(new \Composer\Json\JsonFile($path.'/composer.json')); - - return $fullPackage; - } }