From c9a2416b45477df723f04ac4d9ab9ecb436df068 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 23 Jul 2015 09:45:31 +0100 Subject: [PATCH] Add autoload-dev support --- src/MergePlugin.php | 51 ++++++++++++++++--- tests/phpunit/MergePluginTest.php | 25 +++++++++ .../fixtures/testMergedAutoload/composer.json | 5 ++ .../extensions/Foo/composer.json | 17 +++++++ 4 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/MergePlugin.php b/src/MergePlugin.php index 783133d..fcc9859 100644 --- a/src/MergePlugin.php +++ b/src/MergePlugin.php @@ -251,6 +251,7 @@ protected function loadFile(RootPackage $root, $path) $this->mergeRequires($root, $package); $this->mergeDevRequires($root, $package); $this->mergeAutoload($root, $package, $path); + $this->mergeDevAutoload($root, $package, $path); if (isset($json['repositories'])) { $this->addRepositories($json['repositories'], $root); @@ -352,14 +353,7 @@ protected function mergeAutoload( return; } - $packagePath = substr($path, 0, strrpos($path, '/') + 1); - - array_walk_recursive( - $autoload, - function(&$path) use ($packagePath) { - $path = $packagePath . $path; - } - ); + $this->prependPath($path, $autoload); $root->setAutoload(array_merge_recursive( $root->getAutoload(), @@ -367,6 +361,47 @@ function(&$path) use ($packagePath) { )); } + /** + * @param RootPackage $root + * @param CompletePackage $package + * @param string $path + */ + protected function mergeDevAutoload( + RootPackage $root, + CompletePackage $package, + $path + ) { + $autoload = $package->getDevAutoload(); + if (empty($autoload)) { + return; + } + + $this->prependPath($path, $autoload); + + $root->setDevAutoload(array_merge_recursive( + $root->getDevAutoload(), + $autoload + )); + } + + /** + * Prepend a path to a collection of paths. + * + * @param string $basePath + * @param array $paths + */ + protected function prependPath($basePath, array &$paths) + { + $basePath = substr($basePath, 0, strrpos($basePath, '/') + 1); + + array_walk_recursive( + $paths, + function (&$localPath) use ($basePath) { + $localPath = $basePath . $localPath; + } + ); + } + /** * Extract and merge stability flags from the given collection of * requires. diff --git a/tests/phpunit/MergePluginTest.php b/tests/phpunit/MergePluginTest.php index fad7c4f..8aaddb6 100644 --- a/tests/phpunit/MergePluginTest.php +++ b/tests/phpunit/MergePluginTest.php @@ -349,6 +349,7 @@ public function testMergedAutoload() $root = $this->rootFromJson("{$dir}/composer.json"); $root->getAutoload()->shouldBeCalled(); + $root->getDevAutoload()->shouldBeCalled(); $root->getRequires()->shouldNotBeCalled(); $root->setAutoload(Argument::type('array'))->will( function ($args) use ($that) { @@ -369,6 +370,28 @@ function ($args) use ($that) { ); } ); + $root->setDevAutoload(Argument::type('array'))->will( + function ($args) use ($that) { + $that->assertEquals( + array( + 'psr-4' => array( + 'Dev\\Kittens\\' => array( 'everywhere/', 'extensions/Foo/a/', 'extensions/Foo/b/' ), + 'Dev\\Cats\\' => 'extensions/Foo/src/' + ), + 'psr-0' => array( + 'DevUniqueGlobalClass' => 'extensions/Foo/', + '' => 'extensions/Foo/dev/fallback/' + ), + 'files' => array( 'extensions/Foo/DevSemanticMediaWiki.php' ), + 'classmap' => array( + 'extensions/Foo/DevSemanticMediaWiki.hooks.php', + 'extensions/Foo/dev/includes/', + ), + ), + $args[0] + ); + } + ); $extraInstalls = $this->triggerPlugin($root->reveal(), $dir); @@ -523,6 +546,7 @@ protected function rootFromJson($file) 'suggest' => array(), 'extra' => array(), 'autoload' => array(), + 'autoload-dev' => array(), ), $json ); @@ -534,6 +558,7 @@ protected function rootFromJson($file) $root->getSuggests()->willReturn($data['suggest']); $root->getExtra()->willReturn($data['extra'])->shouldBeCalled(); $root->getAutoload()->willReturn($data['autoload']); + $root->getDevAutoload()->willReturn($data['autoload-dev']); $root->getStabilityFlags()->willReturn(array()); $root->setStabilityFlags(Argument::type('array'))->will( diff --git a/tests/phpunit/fixtures/testMergedAutoload/composer.json b/tests/phpunit/fixtures/testMergedAutoload/composer.json index b8e9265..2720715 100644 --- a/tests/phpunit/fixtures/testMergedAutoload/composer.json +++ b/tests/phpunit/fixtures/testMergedAutoload/composer.json @@ -4,6 +4,11 @@ "Kittens\\": "everywhere/" } }, + "autoload-dev": { + "psr-4": { + "Dev\\Kittens\\": "everywhere/" + } + }, "extra": { "merge-plugin": { "include": "extensions/*/composer.json" diff --git a/tests/phpunit/fixtures/testMergedAutoload/extensions/Foo/composer.json b/tests/phpunit/fixtures/testMergedAutoload/extensions/Foo/composer.json index 8e4a861..b9c0433 100644 --- a/tests/phpunit/fixtures/testMergedAutoload/extensions/Foo/composer.json +++ b/tests/phpunit/fixtures/testMergedAutoload/extensions/Foo/composer.json @@ -15,5 +15,22 @@ "SemanticMediaWiki.hooks.php", "includes/" ] + }, + "autoload-dev": { + "psr-4": { + "Dev\\Kittens\\": [ "a/", "b/" ], + "Dev\\Cats\\": "src/" + }, + "psr-0": { + "DevUniqueGlobalClass": "", + "": "dev/fallback/" + }, + "files" : [ + "DevSemanticMediaWiki.php" + ], + "classmap": [ + "DevSemanticMediaWiki.hooks.php", + "dev/includes/" + ] } }