Use the config object to define the vendor dir and not the installation manager

main
Jordi Boggiano 12 years ago
parent fd776853a4
commit b876dcbafb

@ -12,6 +12,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
use Composer\Config;
use Composer\Installer\InstallationManager; use Composer\Installer\InstallationManager;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
@ -24,12 +25,14 @@ use Composer\Util\Filesystem;
*/ */
class AutoloadGenerator class AutoloadGenerator
{ {
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
{ {
$filesystem = new Filesystem(); $filesystem = new Filesystem();
$filesystem->ensureDirectoryExists($installationManager->getVendorPath()); $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
$vendorPath = strtr(realpath($config->get('vendor-dir')), '\\', '/');
$targetDir = $vendorPath.'/'.$targetDir;
$filesystem->ensureDirectoryExists($targetDir); $filesystem->ensureDirectoryExists($targetDir);
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
$vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true); $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);

@ -105,7 +105,7 @@ class Config
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
$env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
return $this->process(getenv($env) ?: $this->config[$key]); return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
case 'home': case 'home':
return rtrim($this->process($this->config[$key]), '/\\'); return rtrim($this->process($this->config[$key]), '/\\');

@ -21,6 +21,7 @@ use Composer\DependencyResolver\Solver;
use Composer\DependencyResolver\SolverProblemsException; use Composer\DependencyResolver\SolverProblemsException;
use Composer\Downloader\DownloadManager; use Composer\Downloader\DownloadManager;
use Composer\Installer\InstallationManager; use Composer\Installer\InstallationManager;
use Composer\Config;
use Composer\Installer\NoopInstaller; use Composer\Installer\NoopInstaller;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
@ -48,6 +49,11 @@ class Installer
*/ */
protected $io; protected $io;
/**
* @var Config
*/
protected $config;
/** /**
* @var PackageInterface * @var PackageInterface
*/ */
@ -105,6 +111,7 @@ class Installer
* Constructor * Constructor
* *
* @param IOInterface $io * @param IOInterface $io
* @param Config $config
* @param PackageInterface $package * @param PackageInterface $package
* @param DownloadManager $downloadManager * @param DownloadManager $downloadManager
* @param RepositoryManager $repositoryManager * @param RepositoryManager $repositoryManager
@ -113,9 +120,10 @@ class Installer
* @param EventDispatcher $eventDispatcher * @param EventDispatcher $eventDispatcher
* @param AutoloadGenerator $autoloadGenerator * @param AutoloadGenerator $autoloadGenerator
*/ */
public function __construct(IOInterface $io, PackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher, AutoloadGenerator $autoloadGenerator) public function __construct(IOInterface $io, Config $config, PackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher, AutoloadGenerator $autoloadGenerator)
{ {
$this->io = $io; $this->io = $io;
$this->config = $config;
$this->package = $package; $this->package = $package;
$this->downloadManager = $downloadManager; $this->downloadManager = $downloadManager;
$this->repositoryManager = $repositoryManager; $this->repositoryManager = $repositoryManager;
@ -201,7 +209,7 @@ class Installer
// write autoloader // write autoloader
$this->io->write('<info>Generating autoload files</info>'); $this->io->write('<info>Generating autoload files</info>');
$localRepos = new CompositeRepository($this->repositoryManager->getLocalRepositories()); $localRepos = new CompositeRepository($this->repositoryManager->getLocalRepositories());
$this->autoloadGenerator->dump($localRepos, $this->package, $this->installationManager, $this->installationManager->getVendorPath() . '/composer'); $this->autoloadGenerator->dump($this->config, $localRepos, $this->package, $this->installationManager, 'composer');
if ($this->runScripts) { if ($this->runScripts) {
// dispatch post event // dispatch post event
@ -561,6 +569,7 @@ class Installer
return new static( return new static(
$io, $io,
$composer->getConfig(),
$composer->getPackage(), $composer->getPackage(),
$composer->getDownloadManager(), $composer->getDownloadManager(),
$composer->getRepositoryManager(), $composer->getRepositoryManager(),
@ -643,6 +652,19 @@ class Installer
return $this; return $this;
} }
/**
* set the config instance
*
* @param Config $config
* @return Installer
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
/** /**
* run in verbose mode * run in verbose mode
* *

@ -35,29 +35,6 @@ class InstallationManager
{ {
private $installers = array(); private $installers = array();
private $cache = array(); private $cache = array();
private $vendorPath;
/**
* Creates an instance of InstallationManager
*
* @param string $vendorDir Relative path to the vendor directory
* @throws \InvalidArgumentException
*/
public function __construct($vendorDir = 'vendor')
{
$fs = new Filesystem();
if ($fs->isAbsolutePath($vendorDir)) {
$basePath = getcwd();
$relativePath = $fs->findShortestPath($basePath.'/file', $vendorDir);
if ($fs->isAbsolutePath($relativePath)) {
throw new \InvalidArgumentException("Vendor dir ($vendorDir) must be accessible from the directory ($basePath).");
}
$this->vendorPath = $relativePath;
} else {
$this->vendorPath = rtrim($vendorDir, '/');
}
}
/** /**
* Adds installer * Adds installer
@ -217,21 +194,6 @@ class InstallationManager
return $installer->getInstallPath($package); return $installer->getInstallPath($package);
} }
/**
* Returns the vendor path
*
* @param boolean $absolute Whether or not to return an absolute path
* @return string path
*/
public function getVendorPath($absolute = false)
{
if (!$absolute) {
return $this->vendorPath;
}
return getcwd().DIRECTORY_SEPARATOR.$this->vendorPath;
}
private function notifyInstall(PackageInterface $package) private function notifyInstall(PackageInterface $package)
{ {
if ($package->getRepository() instanceof NotifiableRepositoryInterface) { if ($package->getRepository() instanceof NotifiableRepositoryInterface) {

@ -21,6 +21,7 @@ use Composer\Test\TestCase;
class AutoloadGeneratorTest extends TestCase class AutoloadGeneratorTest extends TestCase
{ {
public $vendorDir; public $vendorDir;
private $config;
private $workingDir; private $workingDir;
private $im; private $im;
private $repository; private $repository;
@ -36,6 +37,14 @@ class AutoloadGeneratorTest extends TestCase
$this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload'; $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
$this->ensureDirectoryExistsAndClear($this->vendorDir); $this->ensureDirectoryExistsAndClear($this->vendorDir);
$this->config = $this->getMock('Composer\Config');
$this->config->expects($this->any())
->method('get')
->with($this->equalTo('vendor-dir'))
->will($this->returnCallback(function () use ($that) {
return $that->vendorDir;
}));
$this->dir = getcwd(); $this->dir = getcwd();
chdir($this->workingDir); chdir($this->workingDir);
@ -47,12 +56,6 @@ class AutoloadGeneratorTest extends TestCase
->will($this->returnCallback(function ($package) use ($that) { ->will($this->returnCallback(function ($package) use ($that) {
return $that->vendorDir.'/'.$package->getName(); return $that->vendorDir.'/'.$package->getName();
})); }));
$this->im->expects($this->any())
->method('getVendorPath')
->will($this->returnCallback(function () use ($that) {
return $that->vendorDir;
}));
$this->repository = $this->getMock('Composer\Repository\RepositoryInterface'); $this->repository = $this->getMock('Composer\Repository\RepositoryInterface');
$this->generator = new AutoloadGenerator(); $this->generator = new AutoloadGenerator();
@ -92,7 +95,7 @@ class AutoloadGeneratorTest extends TestCase
$this->createClassFile($this->workingDir); $this->createClassFile($this->workingDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertAutoloadFiles('main', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
} }
@ -117,7 +120,7 @@ class AutoloadGeneratorTest extends TestCase
$this->createClassFile($this->vendorDir); $this->createClassFile($this->vendorDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertAutoloadFiles('main3', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
} }
@ -137,7 +140,7 @@ class AutoloadGeneratorTest extends TestCase
$this->vendorDir .= '/subdir'; $this->vendorDir .= '/subdir';
mkdir($this->vendorDir.'/composer', 0777, true); mkdir($this->vendorDir.'/composer', 0777, true);
$this->createClassFile($this->workingDir); $this->createClassFile($this->workingDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap'); $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
} }
@ -154,7 +157,7 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
} }
@ -174,7 +177,7 @@ class AutoloadGeneratorTest extends TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
mkdir($this->vendorDir.'/composer', 0777, true); mkdir($this->vendorDir.'/composer', 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty."); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
} }
@ -201,7 +204,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}'); file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}'); file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated."); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
array( array(
@ -238,7 +241,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}'); file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}'); file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated."); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals( $this->assertEquals(
array( array(
@ -270,7 +273,7 @@ class AutoloadGeneratorTest extends TestCase
file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}'); file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}'); file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
include $this->vendorDir . '/autoload.php'; include $this->vendorDir . '/autoload.php';
@ -294,7 +297,7 @@ class AutoloadGeneratorTest extends TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
mkdir($this->vendorDir.'/composer', 0777, true); mkdir($this->vendorDir.'/composer', 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer');
$this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer'); $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer');
} }
@ -322,7 +325,7 @@ class AutoloadGeneratorTest extends TestCase
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer"); $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer");
$this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php'); $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
$this->assertEquals( $this->assertEquals(
@ -351,7 +354,7 @@ class AutoloadGeneratorTest extends TestCase
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer"); $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer");
$oldIncludePath = get_include_path(); $oldIncludePath = get_include_path();
@ -379,7 +382,7 @@ class AutoloadGeneratorTest extends TestCase
mkdir($this->vendorDir."/composer", 0777, true); mkdir($this->vendorDir."/composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/composer"); $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer");
$this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php")); $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
} }

@ -24,21 +24,6 @@ class InstallationManagerTest extends \PHPUnit_Framework_TestCase
$this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface'); $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
} }
public function testVendorDirOutsideTheWorkingDir()
{
$manager = new InstallationManager(realpath(getcwd().'/../'));
$this->assertSame('../', $manager->getVendorPath());
}
/**
* @expectedException InvalidArgumentException
*/
public function testVendorDirNotAccessible()
{
$manager = new InstallationManager('/oops');
$this->assertSame('../', $manager->getVendorPath());
}
public function testAddGetInstaller() public function testAddGetInstaller()
{ {
$installer = $this->createInstallerMock(); $installer = $this->createInstallerMock();
@ -63,7 +48,6 @@ class InstallationManagerTest extends \PHPUnit_Framework_TestCase
{ {
$manager = $this->getMockBuilder('Composer\Installer\InstallationManager') $manager = $this->getMockBuilder('Composer\Installer\InstallationManager')
->setMethods(array('install', 'update', 'uninstall')) ->setMethods(array('install', 'update', 'uninstall'))
->setConstructorArgs(array('vendor'))
->getMock(); ->getMock();
$installOperation = new InstallOperation($this->createPackageMock()); $installOperation = new InstallOperation($this->createPackageMock());
@ -226,18 +210,6 @@ class InstallationManagerTest extends \PHPUnit_Framework_TestCase
$manager->uninstall($this->repository, $operation); $manager->uninstall($this->repository, $operation);
} }
public function testGetVendorPathAbsolute()
{
$manager = new InstallationManager('vendor');
$this->assertEquals(getcwd().DIRECTORY_SEPARATOR.'vendor', $manager->getVendorPath(true));
}
public function testGetVendorPathRelative()
{
$manager = new InstallationManager('vendor');
$this->assertEquals('vendor', $manager->getVendorPath());
}
private function createInstallerMock() private function createInstallerMock()
{ {
return $this->getMockBuilder('Composer\Installer\InstallerInterface') return $this->getMockBuilder('Composer\Installer\InstallerInterface')

@ -55,7 +55,7 @@ class InstallerTest extends TestCase
$eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock(); $eventDispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock();
$autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator'); $autoloadGenerator = $this->getMock('Composer\Autoload\AutoloadGenerator');
$installer = new Installer($io, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator); $installer = new Installer($io, $config, clone $rootPackage, $downloadManager, $repositoryManager, $locker, $installationManager, $eventDispatcher, $autoloadGenerator);
$result = $installer->run(); $result = $installer->run();
$this->assertTrue($result); $this->assertTrue($result);

Loading…
Cancel
Save