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..d075da6d7 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 $mainPackage, InstallationManager $installationManager, $targetDir) { $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); @@ -69,13 +58,31 @@ return array( EOF; - $autoloads = $this->parseAutoloads(); + // build package => install path map + $packageMap = array(); + foreach ($localRepo->getPackages() as $installedPackage) { + $packageMap[] = array( + $installedPackage, + $installationManager->getInstallPath($installedPackage) + ); + } + + // add main package + $packageMap[] = array($mainPackage, ''); + + $autoloads = $this->parseAutoloads($packageMap); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $def) { + 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 => dirname(dirname(__DIR__)).$exportedPath,\n"; + $namespacesFile .= " $exportedPrefix => {$baseDir}{$exportedPath},\n"; } } @@ -85,19 +92,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()) { @@ -108,7 +112,7 @@ EOF; foreach ($mapping as $namespace => $path) { $autoloads[$type][] = array( 'namespace' => $namespace, - 'path' => ($installPath ? '/'.$installPath : '').'/'.$path, + 'path' => $installPath.'/'.$path, ); } } @@ -122,4 +126,28 @@ 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; + } + + protected function isAbsolutePath($path) + { + return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; + } } 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..4177b15a2 --- /dev/null +++ b/src/Composer/Installer/InstallerInstaller.php @@ -0,0 +1,100 @@ + + * 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; + private static $classCounter = 0; + + /** + * @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) { + if ('composer-installer' === $package->getType()) { + $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); + + $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)) { + $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(); + $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; /** 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 = '{