1
0
Fork 0

added an autoload-dev section

pull/1344/head
Bilal Amarni 2014-02-27 10:39:33 +01:00
parent e1e48b28f7
commit db91454a13
16 changed files with 205 additions and 3 deletions

View File

@ -39,6 +39,9 @@
"autoload": {
"psr-0": { "Composer": "src/" }
},
"autoload-dev": {
"psr-0": { "Composer\\Test": "tests/" }
},
"bin": ["bin/composer"],
"extra": {
"branch-alias": {

View File

@ -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

View File

@ -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.",

View File

@ -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);
@ -569,6 +576,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])) {

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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();

View File

@ -31,6 +31,7 @@ class ArrayDumper
'extra',
'installationSource' => 'installation-source',
'autoload',
'devAutoload' => 'autoload-dev',
'notificationUrl' => 'notification-url',
'includePaths' => 'include-path',
);

View File

@ -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']);
}

View File

@ -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.
*

View File

@ -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.

View File

@ -170,7 +170,75 @@ class AutoloadGeneratorTest extends TestCase
// Assert that autoload_classmap.php was correctly generated.
$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;

View File

@ -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',
);

View File

@ -0,0 +1,10 @@
<?php
// autoload_files.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
$baseDir . '/devfiles/foo.php',
);

View File

@ -0,0 +1,10 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Main' => array($baseDir . '/src'),
);

View File

@ -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';