Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoload-dev support #47

Merged
merged 1 commit into from Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions src/MergePlugin.php
Expand Up @@ -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);
Expand Down Expand Up @@ -352,21 +353,55 @@ 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(),
$autoload
));
}

/**
* @param RootPackage $root
* @param CompletePackage $package
* @param string $path
*/
protected function mergeDevAutoload(
RootPackage $root,
CompletePackage $package,
$path
) {
$autoload = $package->getDevAutoload();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you are duplicating quite some code from the mergeAutoload function. How about factoring out the common part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've done that.

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.
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/MergePluginTest.php
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -523,6 +546,7 @@ protected function rootFromJson($file)
'suggest' => array(),
'extra' => array(),
'autoload' => array(),
'autoload-dev' => array(),
),
$json
);
Expand All @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions tests/phpunit/fixtures/testMergedAutoload/composer.json
Expand Up @@ -4,6 +4,11 @@
"Kittens\\": "everywhere/"
}
},
"autoload-dev": {
"psr-4": {
"Dev\\Kittens\\": "everywhere/"
}
},
"extra": {
"merge-plugin": {
"include": "extensions/*/composer.json"
Expand Down
Expand Up @@ -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/"
]
}
}