1
0
Fork 0

Merge remote-tracking branch 'CHH/feature/include-paths'

pull/540/merge
Jordi Boggiano 2012-04-08 17:22:51 +02:00
commit 91614af365
8 changed files with 164 additions and 0 deletions

View File

@ -225,6 +225,16 @@ Example:
} }
} }
### include-paths
A list of paths which should get appended to PHP's `include_path`.
Example:
{
"include-paths": ["lib/"]
}
### target-dir ### target-dir
Defines the installation target. Defines the installation target.

View File

@ -146,6 +146,13 @@
"type": "string" "type": "string"
} }
}, },
"include-paths": {
"type": ["array"],
"description": "A list of directories which should get added to PHP's include path.",
"items": {
"type": "string"
}
},
"scripts": { "scripts": {
"type": ["object"], "type": ["object"],
"description": "Scripts listeners that will be executed before/after some events.", "description": "Scripts listeners that will be executed before/after some events.",

View File

@ -35,6 +35,13 @@ if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__.'/ClassLoader.php'; require __DIR__.'/ClassLoader.php';
} }
$includePaths = require __DIR__.'/include_paths.php';
if ($includePaths) {
array_unshift($includePaths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $includePaths));
}
return call_user_func(function() { return call_user_func(function() {
$loader = new \Composer\Autoload\ClassLoader(); $loader = new \Composer\Autoload\ClassLoader();
@ -134,6 +141,7 @@ EOF;
file_put_contents($targetDir.'/autoload.php', $autoloadFile); file_put_contents($targetDir.'/autoload.php', $autoloadFile);
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
file_put_contents($targetDir.'/include_paths.php', $this->getIncludePathsFile($packageMap));
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
} }
@ -205,4 +213,25 @@ EOF;
return $loader; return $loader;
} }
protected function getIncludePathsFile(array $packageMap)
{
$includePaths = array();
foreach ($packageMap as $item) {
list($package, $installPath) = $item;
if (null !== $package->getTargetDir()) {
$installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
}
foreach ($package->getIncludePaths() as $includePath) {
$includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
}
}
return sprintf(
"<?php\nreturn %s;\n", var_export($includePaths, true)
);
}
} }

View File

@ -238,6 +238,10 @@ class AliasPackage extends BasePackage
{ {
return $this->aliasOf->getAutoload(); return $this->aliasOf->getAutoload();
} }
public function getIncludePaths()
{
return $this->aliasOf->getIncludePaths();
}
public function getRepositories() public function getRepositories()
{ {
return $this->aliasOf->getRepositories(); return $this->aliasOf->getRepositories();

View File

@ -169,6 +169,10 @@ class ArrayLoader
$package->setAutoload($config['autoload']); $package->setAutoload($config['autoload']);
} }
if (isset($config['include-paths'])) {
$package->setIncludePaths($config['include-paths']);
}
return $package; return $package;
} }

View File

@ -56,6 +56,7 @@ class MemoryPackage extends BasePackage
protected $recommends = array(); protected $recommends = array();
protected $suggests = array(); protected $suggests = array();
protected $autoload = array(); protected $autoload = array();
protected $includePaths = array();
/** /**
* Creates a new in memory package. * Creates a new in memory package.
@ -623,4 +624,22 @@ class MemoryPackage extends BasePackage
{ {
return $this->autoload; return $this->autoload;
} }
/**
* Sets the list of paths added to PHP's include path.
*
* @param array $includePaths List of directories.
*/
public function setIncludePaths(array $includePaths)
{
$this->includePaths = $includePaths;
}
/**
* {@inheritDoc}
*/
public function getIncludePaths()
{
return $this->includePaths;
}
} }

View File

@ -249,6 +249,14 @@ interface PackageInterface
*/ */
function getAutoload(); function getAutoload();
/**
* Returns a list of directories which should get added to PHP's
* include path.
*
* @return array
*/
function getIncludePaths();
/** /**
* Returns an array of repositories * Returns an array of repositories
* *

View File

@ -253,6 +253,89 @@ class AutoloadGeneratorTest extends TestCase
$this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
} }
public function testIncludePathFileGeneration()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$packages = array();
$a = new MemoryPackage("a/a", "1.0", "1.0");
$a->setIncludePaths(array("lib/"));
$b = new MemoryPackage("b/b", "1.0", "1.0");
$b->setIncludePaths(array("library"));
$packages[] = $a;
$packages[] = $b;
$this->repository->expects($this->once())
->method("getPackages")
->will($this->returnValue($packages));
mkdir($this->vendorDir."/.composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
$this->assertEquals(
array(
$this->vendorDir."/a/a/lib/",
$this->vendorDir."/b/b/library"
),
require($this->vendorDir."/.composer/include_paths.php")
);
}
public function testIncludePathsAreAppendedInAutoloadFile()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$packages = array();
$a = new MemoryPackage("a/a", "1.0", "1.0");
$a->setIncludePaths(array("lib/"));
$packages[] = $a;
$this->repository->expects($this->once())
->method("getPackages")
->will($this->returnValue($packages));
mkdir($this->vendorDir."/.composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
$oldIncludePath = get_include_path();
require($this->vendorDir."/.composer/autoload.php");
$this->assertEquals(
$oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib/",
get_include_path()
);
set_include_path($oldIncludePath);
}
public function testIncludePathFileGenerationWithoutPaths()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$packages = array();
$a = new MemoryPackage("a/a", "1.0", "1.0");
$packages[] = $a;
$this->repository->expects($this->once())
->method("getPackages")
->will($this->returnValue($packages));
mkdir($this->vendorDir."/.composer", 0777, true);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
$this->assertEquals(
array(),
require($this->vendorDir."/.composer/include_paths.php")
);
}
private function createClassFile($basedir) private function createClassFile($basedir)
{ {
if (!is_dir($basedir.'/.composersrc')) { if (!is_dir($basedir.'/.composersrc')) {