1
0
Fork 0

Add InstallerInstaller, refactored AutoloadGenerator, fixes #59

pull/88/head
Jordi Boggiano 2011-11-03 22:22:12 +01:00
parent f572bbb0bf
commit c8859240d2
5 changed files with 141 additions and 29 deletions

View File

@ -35,6 +35,7 @@ $dm->setDownloader('zip', new Downloader\ZipDownloader());
// initialize installation manager // initialize installation manager
$im = new Installer\InstallationManager(); $im = new Installer\InstallationManager();
$im->addInstaller(new Installer\LibraryInstaller($vendorPath, $dm, $rm->getLocalRepository(), null)); $im->addInstaller(new Installer\LibraryInstaller($vendorPath, $dm, $rm->getLocalRepository(), null));
$im->addInstaller(new Installer\InstallerInstaller($vendorPath, $dm, $rm->getLocalRepository(), $im));
// load package // load package
$loader = new Package\Loader\ArrayLoader($rm); $loader = new Package\Loader\ArrayLoader($rm);

View File

@ -24,18 +24,7 @@ use Composer\Repository\RepositoryInterface;
*/ */
class AutoloadGenerator class AutoloadGenerator
{ {
private $localRepo; public function dump(RepositoryInterface $localRepo, PackageInterface $package, InstallationManager $installationManager, $targetDir)
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)
{ {
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
@ -69,7 +58,19 @@ return array(
EOF; 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'])) { if (isset($autoloads['psr-0'])) {
foreach ($autoloads['psr-0'] as $def) { foreach ($autoloads['psr-0'] as $def) {
@ -85,19 +86,16 @@ EOF;
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); 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(); $autoloads = array();
foreach ($installPaths as $item) { foreach ($packageMap as $item) {
list($package, $installPath) = $item; list($package, $installPath) = $item;
if (null !== $package->getTargetDir()) { if (null !== $package->getTargetDir()) {
@ -122,4 +120,23 @@ EOF;
return $autoloads; 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;
}
} }

View File

@ -153,8 +153,8 @@ EOT
$localRepo->write(); $localRepo->write();
$output->writeln('> Generating autoload files'); $output->writeln('> Generating autoload files');
$generator = new AutoloadGenerator($localRepo, $composer->getPackage(), $installationManager); $generator = new AutoloadGenerator;
$generator->dump('vendor/.composer/'); $generator->dump($localRepo, $composer->getPackage(), $installationManager, 'vendor/.composer/');
$output->writeln('> Done'); $output->writeln('> Done');
} }

View File

@ -0,0 +1,94 @@
<?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;
/**
* @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);
}
}

View File

@ -25,9 +25,9 @@ use Composer\Package\PackageInterface;
*/ */
class LibraryInstaller implements InstallerInterface class LibraryInstaller implements InstallerInterface
{ {
private $directory; protected $directory;
private $downloadManager; protected $downloadManager;
private $repository; protected $repository;
private $type; private $type;
/** /**