1
0
Fork 0

Add Files Autoloading key. This is a necessary feature to allow purely or partly functional libraries to be "autoloadable" by composer by calling require_once on every file in the "autoload"."files" list.

pull/762/head
Benjamin Eberlei 2012-06-02 18:18:33 +02:00
parent 639aba6f99
commit 5c5e962b5a
5 changed files with 62 additions and 4 deletions

View File

@ -344,7 +344,7 @@ Example:
Autoload mapping for a PHP autoloader.
Currently [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
autoloading and classmap generation are supported. PSR-0 is the recommended way though
autoloading, classmap generation and files are supported. PSR-0 is the recommended way though
since it offers greater flexibility (no need to regenerate the autoloader when you add
classes).
@ -393,6 +393,18 @@ Example:
}
}
If you want to require certain files explicitly on every request then you can use
the 'files' autoloading mechanism. This is useful if your package includes PHP functions
that cannot be autoloaded by PHP.
Example:
{
"autoload": {
"files": ["src/MyLibrary/functions.php"]
}
}
### include-path
> **DEPRECATED**: This is only present to support legacy projects, and all new code

View File

@ -135,6 +135,10 @@
"classmap": {
"type": "array",
"description": "This is an array of directories that contain classes to be included in the class-map generation process."
},
"files": {
"type": "array",
"description": "This is an array of files that are always required on every request."
}
}
},

View File

@ -121,12 +121,19 @@ EOF;
}
$classmapFile .= ");\n";
$filesCode = "";
$autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
foreach ($autoloads['files'] as $functionFile) {
$filesCode .= 'require "' . $filesystem->findShortestPath(getcwd(), $functionFile) .'";' . "\n";
}
$filesCode = rtrim($filesCode);
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)) {
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
}
file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, true, true, (Boolean) $includePathFile, $targetDirLoader));
file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, true, true, (Boolean) $includePathFile, $targetDirLoader, $filesCode));
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
// TODO BC feature, remove after June 15th
@ -173,7 +180,7 @@ EOF;
*/
public function parseAutoloads(array $packageMap)
{
$autoloads = array('classmap' => array(), 'psr-0' => array());
$autoloads = array('classmap' => array(), 'psr-0' => array(), 'files' => array());
foreach ($packageMap as $item) {
list($package, $installPath) = $item;
@ -281,7 +288,7 @@ EOF;
return $baseDir.var_export($path, true);
}
protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader)
protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode)
{
$file = <<<HEADER
<?php
@ -291,6 +298,8 @@ if (!class_exists('Composer\\\\Autoload\\\\ClassLoader', false)) {
require $vendorPathToTargetDirCode . '/ClassLoader.php';
}
$filesCode
return call_user_func(function() {
\$loader = new \\Composer\\Autoload\\ClassLoader();
\$composerDir = $vendorPathToTargetDirCode;

View File

@ -251,6 +251,37 @@ class AutoloadGeneratorTest extends TestCase
$this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap');
}
public function testFilesAutoloadGeneration()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$packages = array();
$packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
$packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0');
$a->setAutoload(array('files' => array('test.php')));
$b->setAutoload(array('files' => array('test2.php')));
$this->repository->expects($this->once())
->method('getPackages')
->will($this->returnValue($packages));
mkdir($this->vendorDir.'/a/a', 0777, true);
mkdir($this->vendorDir.'/b/b', 0777, true);
file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer');
$code = file_get_contents($this->vendorDir.'/autoload.php');
$this->assertContains('a/a/test.php', $code);
$this->assertContains('b/b/test2.php', $code);
include $this->vendorDir . '/autoload.php';
$this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
$this->assertTrue(function_exists('testFilesAutoloadGeneration2'));
}
public function testOverrideVendorsAutoloading()
{
$package = new MemoryPackage('a', '1.0', '1.0');

View File

@ -5,6 +5,8 @@ if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__ . '/composer' . '/ClassLoader.php';
}
return call_user_func(function() {
$loader = new \Composer\Autoload\ClassLoader();
$composerDir = __DIR__ . '/composer';