1
0
Fork 0
pull/151/head
digitalkaoz 2012-02-07 20:18:37 +01:00
commit 398c080bd3
11 changed files with 348 additions and 67 deletions

View File

@ -68,19 +68,7 @@ return array(
EOF;
// build package => install path map
$packageMap = array();
// add main package
$packageMap[] = array($mainPackage, '');
foreach ($localRepo->getPackages() as $installedPackage) {
$packageMap[] = array(
$installedPackage,
$installationManager->getInstallPath($installedPackage)
);
}
$packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
$autoloads = $this->parseAutoloads($packageMap);
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
@ -128,6 +116,24 @@ EOF;
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
}
public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
{
// build package => install path map
$packageMap = array();
// add main package
$packageMap[] = array($mainPackage, '');
foreach ($packages as $package) {
$packageMap[] = array(
$package,
$installationManager->getInstallPath($package)
);
}
return $packageMap;
}
/**
* Compiles an ordered list of namespace => path mappings
*

View File

@ -84,6 +84,14 @@ class ClassLoader
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*

View File

@ -18,6 +18,7 @@ use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Operation;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@ -52,7 +53,7 @@ EOT
// create local repo, this contains all packages that are installed in the local project
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
// create installed repo, this contains all local packages + platform packages (php & extensions)
$installedRepo = new PlatformRepository($localRepo);
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
if ($input->getOption('local')) {
foreach ($localRepo->getPackages() as $package) {

View File

@ -15,18 +15,23 @@ namespace Composer\Command;
use Composer\Script\ScriptEvents;
use Composer\Script\EventDispatcher;
use Composer\Autoload\AutoloadGenerator;
use Composer\Composer;
use Composer\DependencyResolver;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Operation;
use Composer\Package\MemoryPackage;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\PackageInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Solver;
use Composer\IO\IOInterface;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
@ -60,17 +65,27 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->install($input, $output);
}
public function install(InputInterface $input, OutputInterface $output, $update = false)
{
$preferSource = (Boolean) $input->getOption('dev');
$dryRun = (Boolean) $input->getOption('dry-run');
$verbose = $dryRun || $input->getOption('verbose');
$composer = $this->getComposer();
$io = $this->getApplication()->getIO();
$dispatcher = new EventDispatcher($this->getComposer(), $io);
$eventDispatcher = new EventDispatcher($composer, $io);
return $this->install(
$io,
$composer,
$eventDispatcher,
(Boolean)$input->getOption('dev'),
(Boolean)$input->getOption('dry-run'),
(Boolean)$input->getOption('verbose'),
(Boolean)$input->getOption('no-install-recommends'),
(Boolean)$input->getOption('install-suggests')
);
}
public function install(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher, $preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null)
{
if ($dryRun) {
$verbose = true;
}
if ($preferSource) {
$composer->getDownloadManager()->setPreferSource(true);
@ -79,7 +94,10 @@ EOT
// create local repo, this contains all packages that are installed in the local project
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
// create installed repo, this contains all local packages + platform packages (php & extensions)
$installedRepo = new PlatformRepository($localRepo);
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
if ($additionalInstalledRepository) {
$installedRepo->addRepository($additionalInstalledRepository);
}
// creating repository pool
$pool = new Pool;
@ -91,15 +109,15 @@ EOT
// dispatch pre event
if (!$dryRun) {
$eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
$dispatcher->dispatchCommandEvent($eventName);
$eventDispatcher->dispatchCommandEvent($eventName);
}
// creating requirements request
$request = new Request($pool);
if ($update) {
$output->writeln('<info>Updating dependencies</info>');
$io->write('<info>Updating dependencies</info>');
$installedPackages = $installedRepo->getPackages();
$links = $this->collectLinks($input, $composer->getPackage());
$links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
foreach ($links as $link) {
foreach ($installedPackages as $package) {
@ -112,10 +130,10 @@ EOT
$request->install($link->getTarget(), $link->getConstraint());
}
} elseif ($composer->getLocker()->isLocked()) {
$output->writeln('<info>Installing from lock file</info>');
$io->write('<info>Installing from lock file</info>');
if (!$composer->getLocker()->isFresh()) {
$output->writeln('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
$io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
}
foreach ($composer->getLocker()->getLockedPackages() as $package) {
@ -123,9 +141,9 @@ EOT
$request->install($package->getName(), $constraint);
}
} else {
$output->writeln('<info>Installing dependencies</info>');
$io->write('<info>Installing dependencies</info>');
$links = $this->collectLinks($input, $composer->getPackage());
$links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
foreach ($links as $link) {
$request->install($link->getTarget(), $link->getConstraint());
@ -170,46 +188,46 @@ EOT
// execute operations
if (!$operations) {
$output->writeln('<info>Nothing to install/update</info>');
$io->write('<info>Nothing to install/update</info>');
}
foreach ($operations as $operation) {
if ($verbose) {
$output->writeln((string) $operation);
$io->write((string) $operation);
}
if (!$dryRun) {
$dispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
$eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
$installationManager->execute($operation);
$dispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
$eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
}
}
if (!$dryRun) {
if ($update || !$composer->getLocker()->isLocked()) {
$composer->getLocker()->lockPackages($localRepo->getPackages());
$output->writeln('<info>Writing lock file</info>');
$io->write('<info>Writing lock file</info>');
}
$localRepo->write();
$output->writeln('<info>Generating autoload files</info>');
$io->write('<info>Generating autoload files</info>');
$generator = new AutoloadGenerator;
$generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer');
// dispatch post event
$eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
$dispatcher->dispatchCommandEvent($eventName);
$eventDispatcher->dispatchCommandEvent($eventName);
}
}
private function collectLinks(InputInterface $input, PackageInterface $package)
private function collectLinks(PackageInterface $package, $noInstallRecommends, $installSuggests)
{
$links = $package->getRequires();
if (!$input->getOption('no-install-recommends')) {
if (!$noInstallRecommends) {
$links = array_merge($links, $package->getRecommends());
}
if ($input->getOption('install-suggests')) {
if ($installSuggests) {
$links = array_merge($links, $package->getSuggests());
}

View File

@ -12,7 +12,6 @@
namespace Composer\Command;
use Composer\Repository\PlatformRepository;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

View File

@ -19,6 +19,7 @@ use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Operation;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Repository\PlatformRepository;
use Composer\Script\EventDispatcher;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@ -54,7 +55,20 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
$installCommand = $this->getApplication()->find('install');
$composer = $this->getComposer();
$io = $this->getApplication()->getIO();
$eventDispatcher = new EventDispatcher($composer, $io);
return $installCommand->install($input, $output, true);
return $installCommand->install(
$io,
$composer,
$eventDispatcher,
(Boolean)$input->getOption('dev'),
(Boolean)$input->getOption('dry-run'),
(Boolean)$input->getOption('verbose'),
(Boolean)$input->getOption('no-install-recommends'),
(Boolean)$input->getOption('install-suggests'),
true
);
}
}

View File

@ -12,7 +12,6 @@
namespace Composer\Command;
use Composer\Repository\PlatformRepository;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

View File

@ -0,0 +1,115 @@
<?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\Repository;
use Composer\Package\PackageInterface;
/**
* Composite repository.
*
* @author Beau Simensen <beau@dflydev.com>
*/
class CompositeRepository implements RepositoryInterface
{
/**
* List of repositories
* @var array
*/
private $repositories;
/**
* Constructor
* @param array $repositories
*/
public function __construct(array $repositories)
{
$this->repositories = $repositories;
}
/**
* {@inheritdoc}
*/
public function hasPackage(PackageInterface $package)
{
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
if ($repository->hasPackage($package)) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function findPackage($name, $version)
{
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$package = $repository->findPackage($name, $version);
if (null !== $package) {
return $package;
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function findPackagesByName($name)
{
$packages = array();
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$packages[] = $repository->findPackagesByName($name);
}
return call_user_func_array('array_merge', $packages);
}
/**
* {@inheritdoc}
*/
public function getPackages()
{
$packages = array();
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$packages[] = $repository->getPackages();
}
return call_user_func_array('array_merge', $packages);
}
/**
* {@inheritdoc}
*/
public function count()
{
$total = 0;
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$total += $repository->count();
}
return $total;
}
/**
* Add a repository.
* @param RepositoryInterface $repository
*/
public function addRepository(RepositoryInterface $repository)
{
$this->repositories[] = $repository;
}
}

View File

@ -20,12 +20,6 @@ use Composer\Package\Version\VersionParser;
*/
class PlatformRepository extends ArrayRepository
{
private $localRepository;
public function __construct(RepositoryInterface $localRepository)
{
$this->localRepository = $localRepository;
}
protected function initialize()
{
@ -62,9 +56,4 @@ class PlatformRepository extends ArrayRepository
parent::addPackage($ext);
}
}
public function getPackages()
{
return array_merge(parent::getPackages(), $this->localRepository->getPackages());
}
}

View File

@ -14,7 +14,7 @@ namespace Composer\Script;
use Composer\Json\JsonFile;
use Composer\Repository\FilesystemRepository;
use Composer\Autoload\ClassLoader;
use Composer\Autoload\AutoloadGenerator;
use Composer\Package\PackageInterface;
use Composer\IO\IOInterface;
use Composer\Composer;
@ -48,8 +48,6 @@ class EventDispatcher
{
$this->composer = $composer;
$this->io = $io;
$this->loader = new ClassLoader();
$this->loader->register();
}
/**
@ -105,16 +103,22 @@ class EventDispatcher
{
$package = $this->composer->getPackage();
$scripts = $package->getScripts();
$autoload = $package->getAutoload();
// get namespaces in composer.json project
if (!$this->loader->getPrefixes() && isset($autoload['psr-0'])) {
krsort($autoload['psr-0']);
foreach ($autoload['psr-0'] as $ns => $path) {
$this->loader->add($ns, rtrim(getcwd().'/'.$path, '/'));
}
if (empty($scripts[$event->getName()])) {
return array();
}
return isset($scripts[$event->getName()]) ? $scripts[$event->getName()] : array();
if ($this->loader) {
$this->loader->unregister();
}
$generator = new AutoloadGenerator;
$packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages();
$packageMap = $generator->buildPackageMap($this->composer->getInstallationManager(), $package, $packages);
$map = $generator->parseAutoloads($packageMap);
$this->loader = $generator->createLoader($map);
$this->loader->register();
return $scripts[$event->getName()];
}
}

View File

@ -0,0 +1,128 @@
<?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\Repository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\ArrayRepository;
use Composer\Test\TestCase;
class CompositeRepositoryTest extends TestCase
{
public function testHasPackage()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')), "Should have package 'foo/1'");
$this->assertTrue($repo->hasPackage($this->getPackage('bar', '1')), "Should have package 'bar/1'");
$this->assertFalse($repo->hasPackage($this->getPackage('foo', '2')), "Should not have package 'foo/2'");
$this->assertFalse($repo->hasPackage($this->getPackage('bar', '2')), "Should not have package 'bar/2'");
}
public function testFindPackage()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertEquals('foo', $repo->findPackage('foo', '1')->getName(), "Should find package 'foo/1' and get name of 'foo'");
$this->assertEquals('1', $repo->findPackage('foo', '1')->getPrettyVersion(), "Should find package 'foo/1' and get pretty version of '1'");
$this->assertEquals('bar', $repo->findPackage('bar', '1')->getName(), "Should find package 'bar/1' and get name of 'bar'");
$this->assertEquals('1', $repo->findPackage('bar', '1')->getPrettyVersion(), "Should find package 'bar/1' and get pretty version of '1'");
$this->assertNull($repo->findPackage('foo', '2'), "Should not find package 'foo/2'");
}
public function testFindPackagesByName()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoOne->addPackage($this->getPackage('foo', '2'));
$arrayRepoOne->addPackage($this->getPackage('bat', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
$arrayRepoTwo->addPackage($this->getPackage('foo', '3'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$bats = $repo->findPackagesByName('bat');
$this->assertCount(1, $bats, "Should find one instance of 'bats' (defined in just one repository)");
$this->assertEquals('bat', $bats[0]->getName(), "Should find packages named 'bat'");
$bars = $repo->findPackagesByName('bar');
$this->assertCount(2, $bars, "Should find two instances of 'bar' (both defined in the same repository)");
$this->assertEquals('bar', $bars[0]->getName(), "Should find packages named 'bar'");
$foos = $repo->findPackagesByName('foo');
$this->assertCount(3, $foos, "Should find three instances of 'foo' (two defined in one repository, the third in the other)");
$this->assertEquals('foo', $foos[0]->getName(), "Should find packages named 'foo'");
}
public function testGetPackages()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$packages = $repo->getPackages();
$this->assertCount(2, $packages, "Should get two packages");
$this->assertEquals("foo", $packages[0]->getName(), "First package should have name of 'foo'");
$this->assertEquals("1", $packages[0]->getPrettyVersion(), "First package should have pretty version of '1'");
$this->assertEquals("bar", $packages[1]->getName(), "Second package should have name of 'bar'");
$this->assertEquals("1", $packages[1]->getPrettyVersion(), "Second package should have pretty version of '1'");
}
public function testAddRepository()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '3'));
$repo = new CompositeRepository(array($arrayRepoOne));
$this->assertCount(1, $repo, "Composite repository should have just one package before addRepository() is called");
$repo->addRepository($arrayRepoTwo);
$this->assertCount(4, $repo, "Composite repository should have four packages after addRepository() is called");
}
public function testCount()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertEquals(2, count($repo), "Should return '2' for count(\$repo)");
}
}