Merge pull request #88 from Seldaek/installerinstaller
Add InstallerInstaller, refactored AutoloadGenerator, fixes #59pull/95/head
commit
20db55e3aa
|
@ -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);
|
||||
|
|
|
@ -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) === ':';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* 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 <j.boggiano@seld.be>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
use Composer\Installer\InstallerInterface;
|
||||
use Composer\Package\PackageInterface;
|
||||
|
||||
class Custom implements InstallerInterface
|
||||
{
|
||||
public $version = 'installer-v1';
|
||||
|
||||
public function supports($packageType) {}
|
||||
public function isInstalled(PackageInterface $package) {}
|
||||
public function install(PackageInterface $package) {}
|
||||
public function update(PackageInterface $initial, PackageInterface $target) {}
|
||||
public function uninstall(PackageInterface $package) {}
|
||||
public function getInstallPath(PackageInterface $package) {}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "",
|
||||
"version": "1.0.0",
|
||||
"type": "composer-installer",
|
||||
"autoload": { "psr-0": { "Installer": "" } },
|
||||
"extra": {
|
||||
"class": "Installer\\Custom"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
use Composer\Installer\InstallerInterface;
|
||||
use Composer\Package\PackageInterface;
|
||||
|
||||
class Custom2 implements InstallerInterface
|
||||
{
|
||||
public $version = 'installer-v2';
|
||||
|
||||
public function supports($packageType) {}
|
||||
public function isInstalled(PackageInterface $package) {}
|
||||
public function install(PackageInterface $package) {}
|
||||
public function update(PackageInterface $initial, PackageInterface $target) {}
|
||||
public function uninstall(PackageInterface $package) {}
|
||||
public function getInstallPath(PackageInterface $package) {}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "",
|
||||
"version": "2.0.0",
|
||||
"type": "composer-installer",
|
||||
"autoload": { "psr-0": { "Installer": "" } },
|
||||
"extra": {
|
||||
"class": "Installer\\Custom2"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
use Composer\Installer\InstallerInterface;
|
||||
use Composer\Package\PackageInterface;
|
||||
|
||||
class Custom2 implements InstallerInterface
|
||||
{
|
||||
public $version = 'installer-v3';
|
||||
|
||||
public function supports($packageType) {}
|
||||
public function isInstalled(PackageInterface $package) {}
|
||||
public function install(PackageInterface $package) {}
|
||||
public function update(PackageInterface $initial, PackageInterface $target) {}
|
||||
public function uninstall(PackageInterface $package) {}
|
||||
public function getInstallPath(PackageInterface $package) {}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Installer;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "",
|
||||
"version": "3.0.0",
|
||||
"type": "composer-installer",
|
||||
"autoload": { "psr-0": { "Installer": "" } },
|
||||
"extra": {
|
||||
"class": "Installer\\Custom2"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* 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].'/';
|
||||
}
|
||||
}
|
|
@ -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 = '{
|
||||
|
|
Loading…
Reference in New Issue