From c8859240d24ad6f715bbd241a4929e87c417ef75 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 3 Nov 2011 22:22:12 +0100 Subject: [PATCH] Add InstallerInstaller, refactored AutoloadGenerator, fixes #59 --- bin/composer | 1 + src/Composer/Autoload/AutoloadGenerator.php | 65 ++++++++----- src/Composer/Command/InstallCommand.php | 4 +- src/Composer/Installer/InstallerInstaller.php | 94 +++++++++++++++++++ src/Composer/Installer/LibraryInstaller.php | 6 +- 5 files changed, 141 insertions(+), 29 deletions(-) create mode 100644 src/Composer/Installer/InstallerInstaller.php diff --git a/bin/composer b/bin/composer index ddc04b627..c08194cac 100755 --- a/bin/composer +++ b/bin/composer @@ -35,6 +35,7 @@ $dm->setDownloader('zip', new Downloader\ZipDownloader()); // initialize installation manager $im = new Installer\InstallationManager(); $im->addInstaller(new Installer\LibraryInstaller($vendorPath, $dm, $rm->getLocalRepository(), null)); +$im->addInstaller(new Installer\InstallerInstaller($vendorPath, $dm, $rm->getLocalRepository(), $im)); // load package $loader = new Package\Loader\ArrayLoader($rm); diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index ea705b811..2eab9176f 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -24,18 +24,7 @@ use Composer\Repository\RepositoryInterface; */ 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($targetDir) + public function dump(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager, $targetDir) { $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); @@ -69,7 +58,19 @@ return array( EOF; - $autoloads = $this->parseAutoloads(); + // build package => install path map + $packageMap = array(); + foreach ($localRepo->getPackages() as $package) { + $packageMap[] = array( + $package, + $installationManager->getInstallPath($package) + ); + } + + // add main package + $packageMap[] = array($package, ''); + + $autoloads = $this->parseAutoloads($packageMap); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $def) { @@ -85,19 +86,16 @@ EOF; file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); } - private function parseAutoloads() + /** + * Compiles an ordered list of namespace => path mappings + * + * @param array $packageMap array of array(package, installDir-relative-to-composer.json) + * @return array array('psr-0' => array(array('namespace' => 'Foo', 'path' => 'installDir'))) + */ + public function parseAutoloads(array $packageMap) { - $installPaths = array(); - foreach ($this->localRepo->getPackages() as $package) { - $installPaths[] = array( - $package, - $this->installationManager->getInstallPath($package) - ); - } - $installPaths[] = array($this->package, ''); - $autoloads = array(); - foreach ($installPaths as $item) { + foreach ($packageMap as $item) { list($package, $installPath) = $item; if (null !== $package->getTargetDir()) { @@ -122,4 +120,23 @@ EOF; return $autoloads; } + + /** + * Registers an autoloader based on an autoload map returned by parseAutoloads + * + * @param array $autoloads see parseAutoloads return value + * @return ClassLoader + */ + public function createLoader(array $autoloads) + { + $loader = new ClassLoader(); + + if (isset($autoloads['psr-0'])) { + foreach ($autoloads['psr-0'] as $def) { + $loader->add($def['namespace'], '.'.$def['path']); + } + } + + return $loader; + } } diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 55cb2d8e1..a632a3255 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -153,8 +153,8 @@ EOT $localRepo->write(); $output->writeln('> Generating autoload files'); - $generator = new AutoloadGenerator($localRepo, $composer->getPackage(), $installationManager); - $generator->dump('vendor/.composer/'); + $generator = new AutoloadGenerator; + $generator->dump($localRepo, $composer->getPackage(), $installationManager, 'vendor/.composer/'); $output->writeln('> Done'); } diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php new file mode 100644 index 000000000..c7900059b --- /dev/null +++ b/src/Composer/Installer/InstallerInstaller.php @@ -0,0 +1,94 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Installer; + +use Composer\Autoload\AutoloadGenerator; +use Composer\Downloader\DownloadManager; +use Composer\Repository\WritableRepositoryInterface; +use Composer\DependencyResolver\Operation\OperationInterface; +use Composer\Package\PackageInterface; + +/** + * Installer installation manager. + * + * @author Jordi Boggiano + */ +class InstallerInstaller extends LibraryInstaller +{ + private $installationManager; + + /** + * @param string $dir relative path for packages home + * @param DownloadManager $dm download manager + * @param WritableRepositoryInterface $repository repository controller + */ + public function __construct($directory, DownloadManager $dm, WritableRepositoryInterface $repository, InstallationManager $im) + { + parent::__construct($directory, $dm, $repository, 'composer-installer'); + $this->installationManager = $im; + + foreach ($repository->getPackages() as $package) { + $this->registerInstaller($package); + } + } + + /** + * {@inheritDoc} + */ + public function install(PackageInterface $package) + { + $extra = $package->getExtra(); + if (empty($extra['class'])) { + throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.') + } + + parent::install($package); + $this->registerInstaller($package); + } + + /** + * {@inheritDoc} + */ + public function update(PackageInterface $initial, PackageInterface $target) + { + $extra = $target->getExtra(); + if (empty($extra['class'])) { + throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.') + } + + parent::update($initial, $target); + $this->registerInstaller($target); + } + + private function registerInstaller(PackageInterface $package) + { + $downloadPath = $this->getInstallPath($package); + + $class = $extra['class']; + if (class_exists($class, false)) { + $reflClass = new \ReflectionClass($class); + $code = file_get_contents($reflClass->getFileName()); + $code = preg_replace('{^class (\S+)}mi', 'class $1_composer_tmp', $code); + eval($code); + $class .= '_composer_tmp'; + } else { + $generator = new AutoloadGenerator; + $map = $generator->parseAutoloads(array($target, $downloadPath)); + $generator->createLoader($map)->register(); + } + + $extra = $package->getExtra(); + $installer = new $class($this->directory, $this->downloadManager, $this->repository); + $this->installationManager->addInstaller($installer); + } +} diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index dc28a095d..898c4a6b9 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -25,9 +25,9 @@ use Composer\Package\PackageInterface; */ class LibraryInstaller implements InstallerInterface { - private $directory; - private $downloadManager; - private $repository; + protected $directory; + protected $downloadManager; + protected $repository; private $type; /**