1
0
Fork 0

Rename include-paths to include-path, add deprecated warnings, only generate file if it is needed

pull/540/merge
Jordi Boggiano 2012-04-08 17:42:57 +02:00
parent 91614af365
commit 3c07b4338a
5 changed files with 76 additions and 50 deletions

View File

@ -225,14 +225,16 @@ Example:
} }
} }
### include-paths ### include-path
> **DEPRECATED**: This is only present to support legacy projects, and all new code should preferably use autoloading.
A list of paths which should get appended to PHP's `include_path`. A list of paths which should get appended to PHP's `include_path`.
Example: Example:
{ {
"include-paths": ["lib/"] "include-path": ["lib/"]
} }
### target-dir ### target-dir

View File

@ -146,9 +146,9 @@
"type": "string" "type": "string"
} }
}, },
"include-paths": { "include-path": {
"type": ["array"], "type": ["array"],
"description": "A list of directories which should get added to PHP's include path.", "description": "DEPRECATED: A list of directories which should get added to PHP's include path. This is only present to support legacy projects, and all new code should preferably use autoloading.",
"items": { "items": {
"type": "string" "type": "string"
} }

View File

@ -27,42 +27,6 @@ class AutoloadGenerator
{ {
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
{ {
$autoloadFile = <<<'EOF'
<?php
// autoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
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() {
$loader = new \Composer\Autoload\ClassLoader();
$map = require __DIR__.'/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$classMap = require __DIR__.'/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
$loader->register();
return $loader;
});
EOF;
$filesystem = new Filesystem(); $filesystem = new Filesystem();
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
@ -138,10 +102,12 @@ EOF;
} }
$classmapFile .= ");\n"; $classmapFile .= ");\n";
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)); if ($includePathFile = $this->getIncludePathsFile($packageMap)) {
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
}
file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
} }
@ -230,8 +196,69 @@ EOF;
} }
} }
if (!$includePaths) {
return;
}
return sprintf( return sprintf(
"<?php\nreturn %s;\n", var_export($includePaths, true) "<?php\nreturn %s;\n", var_export($includePaths, true)
); );
} }
protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
{
$file = <<<'HEADER'
<?php
// autoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__.'/ClassLoader.php';
}
return call_user_func(function() {
$loader = new \Composer\Autoload\ClassLoader();
HEADER;
if ($useIncludePath) {
$file .= <<<'INCLUDE_PATH'
$includePaths = require __DIR__.'/include_paths.php';
array_unshift($includePaths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $includePaths));
INCLUDE_PATH;
}
if ($usePSR0) {
$file .= <<<'PSR0'
$map = require __DIR__.'/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
PSR0;
}
if ($useClassMap) {
$file .= <<<'CLASSMAP'
$classMap = require __DIR__.'/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
CLASSMAP;
}
return $file . <<<'FOOTER'
$loader->register();
return $loader;
});
FOOTER;
}
} }

View File

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

View File

@ -283,7 +283,7 @@ class AutoloadGeneratorTest extends TestCase
require($this->vendorDir."/.composer/include_paths.php") require($this->vendorDir."/.composer/include_paths.php")
); );
} }
public function testIncludePathsAreAppendedInAutoloadFile() public function testIncludePathsAreAppendedInAutoloadFile()
{ {
$package = new MemoryPackage('a', '1.0', '1.0'); $package = new MemoryPackage('a', '1.0', '1.0');
@ -314,7 +314,7 @@ class AutoloadGeneratorTest extends TestCase
set_include_path($oldIncludePath); set_include_path($oldIncludePath);
} }
public function testIncludePathFileGenerationWithoutPaths() public function testIncludePathFileWithoutPathsIsSkipped()
{ {
$package = new MemoryPackage('a', '1.0', '1.0'); $package = new MemoryPackage('a', '1.0', '1.0');
$packages = array(); $packages = array();
@ -330,10 +330,7 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer"); $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
$this->assertEquals( $this->assertFalse(file_exists($this->vendorDir."/.composer/include_paths.php"));
array(),
require($this->vendorDir."/.composer/include_paths.php")
);
} }
private function createClassFile($basedir) private function createClassFile($basedir)