From c8859240d24ad6f715bbd241a4929e87c417ef75 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 3 Nov 2011 22:22:12 +0100 Subject: [PATCH 1/4] 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; /** From d4b7f802df933b278056e4f454aeef3196566b70 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 4 Nov 2011 16:33:48 +0100 Subject: [PATCH 2/4] Fix typos --- src/Composer/Installer/InstallerInstaller.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php index c7900059b..d2e8524fe 100644 --- a/src/Composer/Installer/InstallerInstaller.php +++ b/src/Composer/Installer/InstallerInstaller.php @@ -49,7 +49,7 @@ class InstallerInstaller extends LibraryInstaller { $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.') + 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); @@ -63,7 +63,7 @@ class InstallerInstaller extends LibraryInstaller { $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.') + 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); @@ -74,6 +74,7 @@ class InstallerInstaller extends LibraryInstaller { $downloadPath = $this->getInstallPath($package); + $extra = $target->getExtra(); $class = $extra['class']; if (class_exists($class, false)) { $reflClass = new \ReflectionClass($class); @@ -83,7 +84,7 @@ class InstallerInstaller extends LibraryInstaller $class .= '_composer_tmp'; } else { $generator = new AutoloadGenerator; - $map = $generator->parseAutoloads(array($target, $downloadPath)); + $map = $generator->parseAutoloads(array($package, $downloadPath)); $generator->createLoader($map)->register(); } From b7f6cf69ab74b98a06bf148d1dc9543778ec1ae7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 5 Nov 2011 23:51:35 +0100 Subject: [PATCH 3/4] Add tests and fixes some issues --- src/Composer/Autoload/AutoloadGenerator.php | 16 ++- src/Composer/Installer/InstallerInstaller.php | 23 ++-- .../installer-v1/Installer/Custom.php | 18 +++ .../installer-v1/Installer/Exception.php | 7 ++ .../Fixtures/installer-v1/composer.json | 9 ++ .../installer-v2/Installer/Custom2.php | 18 +++ .../installer-v2/Installer/Exception.php | 7 ++ .../Fixtures/installer-v2/composer.json | 9 ++ .../installer-v3/Installer/Custom2.php | 18 +++ .../installer-v3/Installer/Exception.php | 7 ++ .../Fixtures/installer-v3/composer.json | 9 ++ .../Test/Installer/InstallerInstallerTest.php | 119 ++++++++++++++++++ tests/Composer/Test/Json/JsonFileTest.php | 9 ++ 13 files changed, 256 insertions(+), 13 deletions(-) create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/composer.json create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Custom2.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/composer.json create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Custom2.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/composer.json create mode 100644 tests/Composer/Test/Installer/InstallerInstallerTest.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 2eab9176f..f51b89605 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -76,7 +76,12 @@ EOF; foreach ($autoloads['psr-0'] as $def) { $exportedPrefix = var_export($def['namespace'], true); $exportedPath = var_export($def['path'], true); - $namespacesFile .= " $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n"; + if (!$this->isAbsolutePath($def['path'])) { + $baseDir = 'dirname(dirname(__DIR__)).'; + } else { + $baseDir = ''; + } + $namespacesFile .= " $exportedPrefix => {$baseDir}{$exportedPath},\n"; } } @@ -106,7 +111,7 @@ EOF; foreach ($mapping as $namespace => $path) { $autoloads[$type][] = array( 'namespace' => $namespace, - 'path' => ($installPath ? '/'.$installPath : '').'/'.$path, + 'path' => $installPath.'/'.$path, ); } } @@ -133,10 +138,15 @@ EOF; if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $def) { - $loader->add($def['namespace'], '.'.$def['path']); + $loader->add($def['namespace'], $def['path']); } } return $loader; } + + protected function isAbsolutePath($path) + { + return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; + } } diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php index d2e8524fe..00c5c2508 100644 --- a/src/Composer/Installer/InstallerInstaller.php +++ b/src/Composer/Installer/InstallerInstaller.php @@ -26,6 +26,7 @@ use Composer\Package\PackageInterface; class InstallerInstaller extends LibraryInstaller { private $installationManager; + private static $classCounter = 0; /** * @param string $dir relative path for packages home @@ -74,18 +75,20 @@ class InstallerInstaller extends LibraryInstaller { $downloadPath = $this->getInstallPath($package); - $extra = $target->getExtra(); + $extra = $package->getExtra(); $class = $extra['class']; + + $generator = new AutoloadGenerator; + $map = $generator->parseAutoloads(array(array($package, $downloadPath))); + $classLoader = $generator->createLoader($map); + $classLoader->register(); + 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($package, $downloadPath)); - $generator->createLoader($map)->register(); + $code = file_get_contents($classLoader->findFile($class)); + $code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code); + eval('?>'.$code); + $class .= '_composer_tmp'.self::$classCounter; + self::$classCounter++; } $extra = $package->getExtra(); diff --git a/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php b/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php new file mode 100644 index 000000000..4bb58ded8 --- /dev/null +++ b/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php @@ -0,0 +1,18 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Installer; + +use Composer\Installer\InstallerInstaller; +use Composer\Package\Loader\JsonLoader; +use Composer\Package\PackageInterface; + +class InstallerInstallerTest extends \PHPUnit_Framework_TestCase +{ + protected function setUp() + { + $repositoryManager = $this->getMockBuilder('Composer\Repository\RepositoryManager') + ->disableOriginalConstructor() + ->getMock(); + + $loader = new JsonLoader($repositoryManager); + $this->packages = array(); + for ($i = 1; $i <= 3; $i++) { + $this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json'); + } + + $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->repository = $this->getMockBuilder('Composer\Repository\WritableRepositoryInterface') + ->disableOriginalConstructor() + ->getMock(); + } + + public function testInstallNewInstaller() + { + $this->repository + ->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im); + + $test = $this; + $this->im + ->expects($this->once()) + ->method('addInstaller') + ->will($this->returnCallback(function ($installer) use ($test) { + $test->assertEquals('installer-v1', $installer->version); + })); + + $installer->install($this->packages[0]); + } + + public function testUpgradeWithNewClassName() + { + $this->repository + ->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array($this->packages[0]))); + $this->repository + ->expects($this->once()) + ->method('hasPackage') + ->will($this->returnValue(true)); + $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im); + + $test = $this; + $this->im + ->expects($this->once()) + ->method('addInstaller') + ->will($this->returnCallback(function ($installer) use ($test) { + $test->assertEquals('installer-v2', $installer->version); + })); + + $installer->update($this->packages[0], $this->packages[1]); + } + + public function testUpgradeWithSameClassName() + { + $this->repository + ->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array($this->packages[1]))); + $this->repository + ->expects($this->once()) + ->method('hasPackage') + ->will($this->returnValue(true)); + $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', $this->dm, $this->repository, $this->im); + + $test = $this; + $this->im + ->expects($this->once()) + ->method('addInstaller') + ->will($this->returnCallback(function ($installer) use ($test) { + $test->assertEquals('installer-v3', $installer->version); + })); + + $installer->update($this->packages[1], $this->packages[2]); + } +} + +class InstallerInstallerMock extends InstallerInstaller +{ + public function getInstallPath(PackageInterface $package) + { + $version = $package->getVersion(); + return __DIR__.'/Fixtures/installer-v'.$version[0].'/'; + } +} \ No newline at end of file diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index e5899f769..93f7ecef7 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -42,6 +42,15 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase $this->expectParseException('unescaped backslash (\\) on line 2, char 12', $json); } + public function testParseErrorSkipsEscapedBackslash() + { + $json = '{ + "fo\\\\o": "bar" + "a": "b" +}'; + $this->expectParseException('missing comma on line 2, char 23', $json); + } + public function testParseErrorDetectSingleQuotes() { $json = '{ From 1a7bf8a7f8083501da5ddfe7ee004b8c614895f8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 6 Nov 2011 01:09:48 +0100 Subject: [PATCH 4/4] Bugfixes --- src/Composer/Autoload/AutoloadGenerator.php | 15 ++++++++------- src/Composer/Installer/InstallerInstaller.php | 4 +++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index f51b89605..d075da6d7 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -24,7 +24,7 @@ use Composer\Repository\RepositoryInterface; */ class AutoloadGenerator { - public function dump(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager, $targetDir) + public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) { $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); @@ -60,27 +60,28 @@ EOF; // build package => install path map $packageMap = array(); - foreach ($localRepo->getPackages() as $package) { + foreach ($localRepo->getPackages() as $installedPackage) { $packageMap[] = array( - $package, - $installationManager->getInstallPath($package) + $installedPackage, + $installationManager->getInstallPath($installedPackage) ); } // add main package - $packageMap[] = array($package, ''); + $packageMap[] = array($mainPackage, ''); $autoloads = $this->parseAutoloads($packageMap); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $def) { - $exportedPrefix = var_export($def['namespace'], true); - $exportedPath = var_export($def['path'], true); if (!$this->isAbsolutePath($def['path'])) { $baseDir = 'dirname(dirname(__DIR__)).'; + $def['path'] = '/'.$def['path']; } else { $baseDir = ''; } + $exportedPrefix = var_export($def['namespace'], true); + $exportedPath = var_export($def['path'], true); $namespacesFile .= " $exportedPrefix => {$baseDir}{$exportedPath},\n"; } } diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php index 00c5c2508..4177b15a2 100644 --- a/src/Composer/Installer/InstallerInstaller.php +++ b/src/Composer/Installer/InstallerInstaller.php @@ -39,7 +39,9 @@ class InstallerInstaller extends LibraryInstaller $this->installationManager = $im; foreach ($repository->getPackages() as $package) { - $this->registerInstaller($package); + if ('composer-installer' === $package->getType()) { + $this->registerInstaller($package); + } } }