1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 08:32:56 +00:00

CS tweaks and cleanups, allow "" to set fallback dirs, refs #2459

This commit is contained in:
Jordi Boggiano 2014-01-03 16:31:05 +01:00
parent b23742e30c
commit 3c5000ad7f
6 changed files with 59 additions and 76 deletions

View file

@ -209,7 +209,7 @@
}, },
"psr-4": { "psr-4": {
"type": "object", "type": "object",
"description": "This is a hash of namespaces (keys) and the PSR-4 directories they can be found into (values, can be arrays of paths) by the autoloader.", "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 "additionalProperties": true
}, },
"classmap": { "classmap": {

View file

@ -99,9 +99,6 @@ EOF;
// Process the 'psr-4' base directories. // Process the 'psr-4' base directories.
foreach ($autoloads['psr-4'] as $namespace => $paths) { foreach ($autoloads['psr-4'] as $namespace => $paths) {
if ('\\' !== $namespace[strlen($namespace) - 1]) {
throw new \Exception("PSR-4 namespaces must end with a namespace separator. '$namespace' does not.");
}
$exportedPaths = array(); $exportedPaths = array();
foreach ($paths as $path) { foreach ($paths as $path) {
$exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
@ -160,47 +157,25 @@ EOF;
// flatten array // flatten array
$classMap = array(); $classMap = array();
if ($scanPsr0Packages) { if ($scanPsr0Packages) {
// Scan the PSR-0 directories for class files, and add them to the // Scan the PSR-0/4 directories for class files, and add them to the class map
// class map. foreach (array('psr-0', 'psr-4') as $psrType) {
foreach ($autoloads['psr-0'] as $namespace => $paths) { foreach ($autoloads[$psrType] as $namespace => $paths) {
foreach ($paths as $dir) { foreach ($paths as $dir) {
$dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir); $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir);
if (!is_dir($dir)) { if (!is_dir($dir)) {
continue; continue;
}
$whitelist = sprintf(
'{%s/%s.+(?<!(?<!/)Test\.php)$}',
preg_quote($dir),
strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
);
foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
if ('' === $namespace || 0 === strpos($class, $namespace)) {
if (!isset($classMap[$class])) {
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
$classMap[$class] = $path.",\n";
}
} }
} $whitelist = sprintf(
} '{%s/%s.+(?<!(?<!/)Test\.php)$}',
} preg_quote($dir),
// Scan the PSR-4 directories for class files, and add them to the ($psrType === 'psr-4' || strpos($namespace, '_') === false) ? preg_quote(strtr($namespace, '\\', '/')) : ''
// class map. );
foreach ($autoloads['psr-4'] as $namespace => $paths) { foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
foreach ($paths as $dir) { if ('' === $namespace || 0 === strpos($class, $namespace)) {
$dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir); if (!isset($classMap[$class])) {
if (!is_dir($dir)) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
continue; $classMap[$class] = $path.",\n";
} }
$whitelist = sprintf(
'{%s/%s.+(?<!(?<!/)Test\.php)$}',
preg_quote($dir),
strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
);
foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
if ('' === $namespace || 0 === strpos($class, $namespace)) {
if (!isset($classMap[$class])) {
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
$classMap[$class] = $path.",\n";
} }
} }
} }
@ -273,15 +248,22 @@ EOF;
/** /**
* @param PackageInterface $package * @param PackageInterface $package
* *
* @throws \Exception * @throws \InvalidArgumentException Throws an exception, if the package has illegal settings.
* Throws an exception, if the package has illegal settings.
*/ */
protected function validatePackage(PackageInterface $package) { protected function validatePackage(PackageInterface $package)
{
$autoload = $package->getAutoload(); $autoload = $package->getAutoload();
if (!empty($autoload['psr-4']) && null !== $package->getTargetDir()) { if (!empty($autoload['psr-4']) && null !== $package->getTargetDir()) {
$name = $package->getName(); $name = $package->getName();
$package->getTargetDir(); $package->getTargetDir();
throw new \Exception("The ['autoload']['psr-4'] setting is incompatible with the ['target-dir'] setting, in package '$name'."); throw new \InvalidArgumentException("PSR-4 autoloading is incompatible with the target-dir property, remove the target-dir in package '$name'.");
}
if (!empty($autoload['psr-4'])) {
foreach ($autoload['psr-4'] as $namespace => $dirs) {
if ($namespace !== '' && '\\' !== substr($namespace, -1)) {
throw new \InvalidArgumentException("psr-4 namespaces must end with a namespace separator, '$namespace' does not, use '$namespace\\'.");
}
}
} }
} }

View file

@ -163,7 +163,7 @@ class ClassLoader
// Register directories for a new namespace. // Register directories for a new namespace.
$length = strlen($prefix); $length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) { if ('\\' !== $prefix[$length - 1]) {
throw new \Exception("A non-empty PSR-4 prefix must end with a namespace separator."); throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
} }
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths; $this->prefixDirsPsr4[$prefix] = (array) $paths;
@ -211,7 +211,7 @@ class ClassLoader
} else { } else {
$length = strlen($prefix); $length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) { if ('\\' !== $prefix[$length - 1]) {
throw new \Exception("A non-empty PSR-4 prefix must end with a namespace separator."); throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
} }
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths; $this->prefixDirsPsr4[$prefix] = (array) $paths;
@ -317,10 +317,8 @@ class ClassLoader
// PSR-0 lookup // PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) { if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name // namespaced class name
$logicalPathPsr0 $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
= substr($logicalPathPsr4, 0, $pos + 1) . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR)
;
} else { } else {
// PEAR-like class name // PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php'; $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php';

View file

@ -186,11 +186,18 @@ class ValidatingArrayLoader implements LoaderInterface
$this->errors[] = 'autoload : invalid value ('.$type.'), must be one of '.implode(', ', $types); $this->errors[] = 'autoload : invalid value ('.$type.'), must be one of '.implode(', ', $types);
unset($this->config['autoload'][$type]); unset($this->config['autoload'][$type]);
} }
if ($type === 'psr-4') {
foreach ($typeConfig as $namespace => $dirs) {
if ($namespace !== '' && '\\' !== substr($namespace, -1)) {
$this->errors[] = 'autoload.psr-4 : invalid value ('.$namespace.'), namespaces must end with a namespace separator, should be '.$namespace.'\\';
}
}
}
} }
} }
if (!empty($this->config['autoload']['psr-4']) && !empty($this->config['target-dir'])) { if (!empty($this->config['autoload']['psr-4']) && !empty($this->config['target-dir'])) {
$this->errors[] = "The ['autoload']['psr-4'] setting is incompatible with the ['target-dir'] setting."; $this->errors[] = 'target-dir : this can not be used together with the autoload.psr-4 setting, remove target-dir to upgrade to psr-4';
// Unset the psr-4 setting, since unsetting target-dir might // Unset the psr-4 setting, since unsetting target-dir might
// interfere with other settings. // interfere with other settings.
unset($this->config['autoload']['psr-4']); unset($this->config['autoload']['psr-4']);

View file

@ -252,11 +252,14 @@ class AutoloadGeneratorTest extends TestCase
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty."); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
} }
public function testPSR0ToClassMapIgnoresNonExistingDir() public function testPSRToClassMapIgnoresNonExistingDir()
{ {
$package = new Package('a', '1.0', '1.0'); $package = new Package('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('foo/bar/non/existing/'))); $package->setAutoload(array(
'psr-0' => array('Prefix' => 'foo/bar/non/existing/'),
'psr-4' => array('Prefix\\' => 'foo/bar/non/existing2/')
));
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('getCanonicalPackages') ->method('getCanonicalPackages')

View file

@ -14,13 +14,9 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
* *
* @dataProvider getLoadClassTests * @dataProvider getLoadClassTests
* *
* @param string $class * @param string $class The fully-qualified class name to test, without preceding namespace separator.
* The fully-qualified class name to test, * @param bool $prependSeparator Whether to call ->loadClass() with a class name with preceding
* without preceding namespace separator. * namespace separator, as it happens in PHP 5.3.0 - 5.3.2. See https://bugs.php.net/50731
* @param bool $prependSeparator
* Whether to call ->loadClass() with a class name with preceding
* namespace separator, as it happens in PHP 5.3.0 - 5.3.2.
* See https://bugs.php.net/50731
*/ */
public function testLoadClass($class, $prependSeparator = FALSE) public function testLoadClass($class, $prependSeparator = FALSE)
{ {
@ -30,12 +26,11 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
$loader->addPsr4('ShinyVendor\\ShinyPackage\\', __DIR__ . '/Fixtures'); $loader->addPsr4('ShinyVendor\\ShinyPackage\\', __DIR__ . '/Fixtures');
if ($prependSeparator) { if ($prependSeparator) {
$prepend = '\\'; $prepend = '\\';
$message = "->loadClass() loads '$class'."; $message = "->loadClass() loads '$class'.";
} } else {
else { $prepend = '';
$prepend = ''; $message = "->loadClass() loads '\\$class', as required in PHP 5.3.0 - 5.3.2.";
$message = "->loadClass() loads '\\$class', as required in PHP 5.3.0 - 5.3.2.";
} }
$loader->loadClass($prepend . $class); $loader->loadClass($prepend . $class);
@ -45,8 +40,7 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
/** /**
* Provides arguments for ->testLoadClass(). * Provides arguments for ->testLoadClass().
* *
* @return array * @return array Array of parameter sets to test with.
* Array of parameter sets to test with.
*/ */
public function getLoadClassTests() public function getLoadClassTests()
{ {
@ -56,10 +50,9 @@ class ClassLoaderTest extends \PHPUnit_Framework_TestCase
array('ShinyVendor\\ShinyPackage\\SubNamespace\\Foo'), array('ShinyVendor\\ShinyPackage\\SubNamespace\\Foo'),
// "Bar" would not work here, since it is defined in a ".inc" file, // "Bar" would not work here, since it is defined in a ".inc" file,
// instead of a ".php" file. So, use "Baz" instead. // instead of a ".php" file. So, use "Baz" instead.
array('Namespaced\\Baz', '\\'), array('Namespaced\\Baz', true),
array('Pearlike_Bar', '\\'), array('Pearlike_Bar', true),
array('ShinyVendor\\ShinyPackage\\SubNamespace\\Bar', '\\'), array('ShinyVendor\\ShinyPackage\\SubNamespace\\Bar', true),
); );
} }
} }