From b0ec8ee0968c144c2966c8587e09da1c84023347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kerner?= Date: Mon, 27 Jan 2014 11:42:54 +0100 Subject: [PATCH] * added exclude-from-classmap feature * updated to latest master version --- res/composer-schema.json | 4 ++ src/Composer/Autoload/AutoloadGenerator.php | 43 +++++++++++++++---- src/Composer/Autoload/ClassMapGenerator.php | 6 +-- .../Test/Autoload/AutoloadGeneratorTest.php | 30 +++++++++++++ 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/res/composer-schema.json b/res/composer-schema.json index 905199247..6687de280 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -223,6 +223,10 @@ "files": { "type": "array", "description": "This is an array of files that are always required on every request." + }, + "exclude-from-classmap": { + "type": "array", + "description": "This is an array of patterns to exclude from autoload classmap generation. (e.g. \"exclude-from-classmap\": [\"/test/\", \"/tests/\", \"/Tests/\"]" } } }, diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 2359265bd..c65980697 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -27,6 +27,8 @@ use Composer\Script\ScriptEvents; */ class AutoloadGenerator { + const EXCLUDE_PATTERN = '.*%s'; + /** * @var EventDispatcher */ @@ -154,6 +156,11 @@ EOF; EOF; } + $blacklist = ''; + if(!empty($autoloads['exclude-from-classmap'])) { + $blacklist = '{(' . implode('|', $autoloads['exclude-from-classmap']) . ')}'; + } + // flatten array $classMap = array(); if ($scanPsr0Packages) { @@ -165,12 +172,7 @@ EOF; if (!is_dir($dir)) { continue; } - $whitelist = sprintf( - '{%s/%s.+(? $path) { + foreach (ClassMapGenerator::createMap($dir, $blacklist) as $class => $path) { if ('' === $namespace || 0 === strpos($class, $namespace)) { if (!isset($classMap[$class])) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); @@ -185,7 +187,7 @@ EOF; $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { - foreach (ClassMapGenerator::createMap($dir) as $class => $path) { + foreach (ClassMapGenerator::createMap($dir, $blacklist) as $class => $path) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); $classMap[$class] = $path.",\n"; } @@ -285,11 +287,19 @@ EOF; $psr4 = $this->parseAutoloadsType($packageMap, 'psr-4', $mainPackage); $classmap = $this->parseAutoloadsType($sortedPackageMap, 'classmap', $mainPackage); $files = $this->parseAutoloadsType($sortedPackageMap, 'files', $mainPackage); + $exclude = $this->parseAutoloadsType($sortedPackageMap, 'exclude-from-classmap', $mainPackage); krsort($psr0); krsort($psr4); - return array('psr-0' => $psr0, 'psr-4' => $psr4, 'classmap' => $classmap, 'files' => $files); + return + array( + 'psr-0' => $psr0, + 'psr-4' => $psr4, + 'classmap' => $classmap, + 'files' => $files, + 'exclude-from-classmap' => $exclude + ); } /** @@ -597,6 +607,23 @@ FOOTER; $path = $package->getTargetDir() . '/' . $path; } + if ($type === 'exclude-from-classmap') { + // first escape user input + $path = sprintf(self::EXCLUDE_PATTERN, preg_quote($path)); + + if ($package === $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) { + // remove target-dir from classmap entries of the root package + $targetDir = str_replace('\\', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '', $package->getTargetDir()))); + $path = ltrim(preg_replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/'); + } + elseif($package !== $mainPackage && $package->getTargetDir() && !is_readable($installPath.'/'.$path)) { + // add target-dir to exclude entries that don't have it + $path = preg_quote($package->getTargetDir()) . '/' . $path; + } + $autoloads[] = empty($installPath) ? $path : preg_quote($installPath) . '/' . $path; + continue; + } + if (empty($installPath)) { $autoloads[$namespace][] = empty($path) ? '.' : $path; } else { diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index e9142c15f..5d45eb482 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -42,13 +42,13 @@ class ClassMapGenerator * Iterate over all files in the given directory searching for classes * * @param Iterator|string $path The path to search in or an iterator - * @param string $whitelist Regex that matches against the file path + * @param string $blacklist Regex that matches against the file path that exclude from the classmap. * * @return array A class map array * * @throws \RuntimeException When the path is neither an existing file nor directory */ - public static function createMap($path, $whitelist = null) + public static function createMap($path, $blacklist = '') { if (is_string($path)) { if (is_file($path)) { @@ -72,7 +72,7 @@ class ClassMapGenerator continue; } - if ($whitelist && !preg_match($whitelist, strtr($filePath, '\\', '/'))) { + if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) { continue; } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 499f4de0d..4f91903cf 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -1016,6 +1016,36 @@ EOF; $this->assertEquals($expectedPsr4, file_get_contents($this->vendorDir.'/composer/autoload_psr4.php')); } + public function testExcludeFromClassmap() + { + $package = new Package('a', '1.0', '1.0'); + $package->setAutoload(array( + 'psr-0' => array( + 'Main' => 'src/', + ), + 'classmap' => array('composersrc/'), + 'exclude-from-classmap' => array('/tests/'), + )); + + $this->repository->expects($this->once()) + ->method('getCanonicalPackages') + ->will($this->returnValue(array())); + + $this->fs->ensureDirectoryExists($this->workingDir.'/composer'); + $this->fs->ensureDirectoryExists($this->workingDir.'/src'); + + $this->fs->ensureDirectoryExists($this->workingDir.'/composersrc'); + file_put_contents($this->workingDir.'/composersrc/foo.php', 'fs->ensureDirectoryExists($this->workingDir.'/composersrc/tests'); + file_put_contents($this->workingDir.'/composersrc/tests/bar.php', 'generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1'); + + // Assert that autoload_classmap.php was correctly generated. + $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap'); + } + private function assertAutoloadFiles($name, $dir, $type = 'namespaces') { $a = __DIR__.'/Fixtures/autoload_'.$name.'.php';