Merge remote-tracking branch 'bamarni/autoload-dev'
commit
023ff131aa
|
@ -39,6 +39,9 @@
|
|||
"autoload": {
|
||||
"psr-0": { "Composer": "src/" }
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-0": { "Composer\\Test": "tests/" }
|
||||
},
|
||||
"bin": ["bin/composer"],
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
|
|
|
@ -516,6 +516,27 @@ Example:
|
|||
}
|
||||
}
|
||||
|
||||
### autoload-dev <span>(root-only)</span>
|
||||
|
||||
This section allows to define autoload rules for development purpose.
|
||||
|
||||
If you're generating classmaps from your PSR-0 namespaces, you're probably concerned
|
||||
about performance, if so, you'll also don't want your test classes to be mixed up
|
||||
with your regular classes in those classmaps.
|
||||
|
||||
Therefore, it is a good idea to rely on a dedicated path for your unit tests.
|
||||
|
||||
Example:
|
||||
|
||||
{
|
||||
"autoload": {
|
||||
"psr-0": { "MyLibrary": "src/" }
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-0": { "MyLibrary\\Tests": "tests/" }
|
||||
}
|
||||
}
|
||||
|
||||
### include-path
|
||||
|
||||
> **DEPRECATED**: This is only present to support legacy projects, and all new code
|
||||
|
|
|
@ -226,6 +226,30 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"type": "object",
|
||||
"description": "Description of additional autoload rules for development purpose (eg. a test suite).",
|
||||
"properties": {
|
||||
"psr-0": {
|
||||
"type": "object",
|
||||
"description": "This is a hash of namespaces (keys) and the directories they can be found into (values, can be arrays of paths) by the autoloader.",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"psr-4": {
|
||||
"type": "object",
|
||||
"description": "This is a hash of namespaces (keys) and the PSR-4 directories they can map to (values, can be arrays of paths) by the autoloader.",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"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."
|
||||
}
|
||||
}
|
||||
},
|
||||
"archive": {
|
||||
"type": ["object"],
|
||||
"description": "Options for creating package archives for distribution.",
|
||||
|
|
|
@ -32,11 +32,18 @@ class AutoloadGenerator
|
|||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
private $devMode = false;
|
||||
|
||||
public function __construct(EventDispatcher $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function setDevMode($devMode = true)
|
||||
{
|
||||
$this->devMode = (boolean) $devMode;
|
||||
}
|
||||
|
||||
public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
|
||||
{
|
||||
$this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP);
|
||||
|
@ -567,6 +574,9 @@ FOOTER;
|
|||
list($package, $installPath) = $item;
|
||||
|
||||
$autoload = $package->getAutoload();
|
||||
if ($this->devMode && $package === $mainPackage) {
|
||||
$autoload = array_merge_recursive($autoload, $package->getDevAutoload());
|
||||
}
|
||||
|
||||
// skip misconfigured packages
|
||||
if (!isset($autoload[$type]) || !is_array($autoload[$type])) {
|
||||
|
|
|
@ -31,6 +31,7 @@ class DumpAutoloadCommand extends Command
|
|||
->setDescription('Dumps the autoloader')
|
||||
->setDefinition(array(
|
||||
new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 packages to be loaded with classmaps too, good for production.'),
|
||||
new InputOption('dev', null, InputOption::VALUE_NONE, 'Enables dev autoload.'),
|
||||
))
|
||||
->setHelp(<<<EOT
|
||||
<info>php composer.phar dump-autoload</info>
|
||||
|
@ -59,6 +60,8 @@ EOT
|
|||
$output->writeln('<info>Generating autoload files</info>');
|
||||
}
|
||||
|
||||
$composer->getAutoloadGenerator()->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize);
|
||||
$generator = $composer->getAutoloadGenerator();
|
||||
$generator->setDevMode($input->getOption('dev'));
|
||||
$generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -290,6 +290,7 @@ class Installer
|
|||
$this->io->write('<info>Generating autoload files</info>');
|
||||
}
|
||||
|
||||
$this->autoloadGenerator->setDevMode($this->devMode);
|
||||
$this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
|
||||
|
||||
if ($this->runScripts) {
|
||||
|
|
|
@ -245,6 +245,10 @@ class AliasPackage extends BasePackage implements CompletePackageInterface
|
|||
{
|
||||
return $this->aliasOf->getAutoload();
|
||||
}
|
||||
public function getDevAutoload()
|
||||
{
|
||||
return $this->aliasOf->getDevAutoload();
|
||||
}
|
||||
public function getIncludePaths()
|
||||
{
|
||||
return $this->aliasOf->getIncludePaths();
|
||||
|
|
|
@ -31,6 +31,7 @@ class ArrayDumper
|
|||
'extra',
|
||||
'installationSource' => 'installation-source',
|
||||
'autoload',
|
||||
'devAutoload' => 'autoload-dev',
|
||||
'notificationUrl' => 'notification-url',
|
||||
'includePaths' => 'include-path',
|
||||
);
|
||||
|
|
|
@ -130,6 +130,10 @@ class ArrayLoader implements LoaderInterface
|
|||
$package->setAutoload($config['autoload']);
|
||||
}
|
||||
|
||||
if (isset($config['autoload-dev'])) {
|
||||
$package->setDevAutoload($config['autoload-dev']);
|
||||
}
|
||||
|
||||
if (isset($config['include-path'])) {
|
||||
$package->setIncludePaths($config['include-path']);
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ class Package extends BasePackage
|
|||
protected $devRequires = array();
|
||||
protected $suggests = array();
|
||||
protected $autoload = array();
|
||||
protected $devAutoload = array();
|
||||
protected $includePaths = array();
|
||||
protected $archiveExcludes = array();
|
||||
|
||||
|
@ -440,6 +441,24 @@ class Package extends BasePackage
|
|||
return $this->autoload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the dev autoload mapping
|
||||
*
|
||||
* @param array $autoload Mapping of dev autoloading rules
|
||||
*/
|
||||
public function setDevAutoload(array $devAutoload)
|
||||
{
|
||||
$this->devAutoload = $devAutoload;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDevAutoload()
|
||||
{
|
||||
return $this->devAutoload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the list of paths added to PHP's include path.
|
||||
*
|
||||
|
|
|
@ -231,13 +231,25 @@ interface PackageInterface
|
|||
*
|
||||
* {"<type>": {"<namespace": "<directory>"}}
|
||||
*
|
||||
* Type is either "psr-0" or "pear". Namespaces are mapped to directories
|
||||
* for autoloading using the type specified.
|
||||
* Type is either "psr-4", "psr-0", "classmap" or "files". Namespaces are mapped to
|
||||
* directories for autoloading using the type specified.
|
||||
*
|
||||
* @return array Mapping of autoloading rules
|
||||
*/
|
||||
public function getAutoload();
|
||||
|
||||
/**
|
||||
* Returns an associative array of dev autoloading rules
|
||||
*
|
||||
* {"<type>": {"<namespace": "<directory>"}}
|
||||
*
|
||||
* Type is either "psr-4", "psr-0", "classmap" or "files". Namespaces are mapped to
|
||||
* directories for autoloading using the type specified.
|
||||
*
|
||||
* @return array Mapping of dev autoloading rules
|
||||
*/
|
||||
public function getDevAutoload();
|
||||
|
||||
/**
|
||||
* Returns a list of directories which should get added to PHP's
|
||||
* include path.
|
||||
|
|
|
@ -171,6 +171,74 @@ class AutoloadGeneratorTest extends TestCase
|
|||
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
|
||||
}
|
||||
|
||||
public function testMainPackageDevAutoloading()
|
||||
{
|
||||
$package = new Package('a', '1.0', '1.0');
|
||||
$package->setAutoload(array(
|
||||
'psr-0' => array(
|
||||
'Main' => 'src/',
|
||||
),
|
||||
));
|
||||
$package->setDevAutoload(array(
|
||||
'files' => array('devfiles/foo.php'),
|
||||
));
|
||||
|
||||
$this->repository->expects($this->once())
|
||||
->method('getCanonicalPackages')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/composer');
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
|
||||
file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
|
||||
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
|
||||
file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
|
||||
|
||||
// generate autoload files with the dev mode set to true
|
||||
$this->generator->setDevMode(true);
|
||||
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
|
||||
|
||||
// check standard autoload
|
||||
$this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
|
||||
$this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
|
||||
|
||||
// make sure dev autoload is correctly dumped
|
||||
$this->assertAutoloadFiles('files2', $this->vendorDir.'/composer', 'files');
|
||||
}
|
||||
|
||||
public function testMainPackageDevAutoloadingDisabledByDefault()
|
||||
{
|
||||
$package = new Package('a', '1.0', '1.0');
|
||||
$package->setAutoload(array(
|
||||
'psr-0' => array(
|
||||
'Main' => 'src/',
|
||||
),
|
||||
));
|
||||
$package->setDevAutoload(array(
|
||||
'files' => array('devfiles/foo.php'),
|
||||
));
|
||||
|
||||
$this->repository->expects($this->once())
|
||||
->method('getCanonicalPackages')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/composer');
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/src/Main');
|
||||
file_put_contents($this->workingDir.'/src/Main/ClassMain.php', '<?php namespace Main; class ClassMain {}');
|
||||
|
||||
$this->fs->ensureDirectoryExists($this->workingDir.'/devfiles');
|
||||
file_put_contents($this->workingDir.'/devfiles/foo.php', '<?php function foo() { echo "foo"; }');
|
||||
|
||||
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_1');
|
||||
|
||||
// check standard autoload
|
||||
$this->assertAutoloadFiles('main4', $this->vendorDir.'/composer');
|
||||
$this->assertAutoloadFiles('classmap7', $this->vendorDir.'/composer', 'classmap');
|
||||
|
||||
// make sure dev autoload is disabled when dev mode is set to false
|
||||
$this->assertFalse(is_file($this->vendorDir.'/composer/autoload_files.php'));
|
||||
}
|
||||
|
||||
public function testVendorDirSameAsWorkingDir()
|
||||
{
|
||||
$this->vendorDir = $this->workingDir;
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
// autoload_classmap.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Main\\ClassMain' => $baseDir . '/src/Main/ClassMain.php',
|
||||
);
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
// autoload_files.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
$baseDir . '/devfiles/foo.php',
|
||||
);
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
// autoload_namespaces.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Main' => array($baseDir . '/src'),
|
||||
);
|
|
@ -13,6 +13,8 @@
|
|||
error_reporting(E_ALL);
|
||||
|
||||
$loader = require __DIR__.'/../src/bootstrap.php';
|
||||
|
||||
// to be removed
|
||||
$loader->add('Composer\Test', __DIR__);
|
||||
|
||||
require __DIR__.'/Composer/TestCase.php';
|
||||
|
|
Loading…
Reference in New Issue