Skip to content

Commit c9a2416

Browse files
Add autoload-dev support
1 parent c455fb5 commit c9a2416

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

src/MergePlugin.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected function loadFile(RootPackage $root, $path)
251251
$this->mergeRequires($root, $package);
252252
$this->mergeDevRequires($root, $package);
253253
$this->mergeAutoload($root, $package, $path);
254+
$this->mergeDevAutoload($root, $package, $path);
254255

255256
if (isset($json['repositories'])) {
256257
$this->addRepositories($json['repositories'], $root);
@@ -352,21 +353,55 @@ protected function mergeAutoload(
352353
return;
353354
}
354355

355-
$packagePath = substr($path, 0, strrpos($path, '/') + 1);
356-
357-
array_walk_recursive(
358-
$autoload,
359-
function(&$path) use ($packagePath) {
360-
$path = $packagePath . $path;
361-
}
362-
);
356+
$this->prependPath($path, $autoload);
363357

364358
$root->setAutoload(array_merge_recursive(
365359
$root->getAutoload(),
366360
$autoload
367361
));
368362
}
369363

364+
/**
365+
* @param RootPackage $root
366+
* @param CompletePackage $package
367+
* @param string $path
368+
*/
369+
protected function mergeDevAutoload(
370+
RootPackage $root,
371+
CompletePackage $package,
372+
$path
373+
) {
374+
$autoload = $package->getDevAutoload();
375+
if (empty($autoload)) {
376+
return;
377+
}
378+
379+
$this->prependPath($path, $autoload);
380+
381+
$root->setDevAutoload(array_merge_recursive(
382+
$root->getDevAutoload(),
383+
$autoload
384+
));
385+
}
386+
387+
/**
388+
* Prepend a path to a collection of paths.
389+
*
390+
* @param string $basePath
391+
* @param array $paths
392+
*/
393+
protected function prependPath($basePath, array &$paths)
394+
{
395+
$basePath = substr($basePath, 0, strrpos($basePath, '/') + 1);
396+
397+
array_walk_recursive(
398+
$paths,
399+
function (&$localPath) use ($basePath) {
400+
$localPath = $basePath . $localPath;
401+
}
402+
);
403+
}
404+
370405
/**
371406
* Extract and merge stability flags from the given collection of
372407
* requires.

tests/phpunit/MergePluginTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ public function testMergedAutoload()
349349
$root = $this->rootFromJson("{$dir}/composer.json");
350350

351351
$root->getAutoload()->shouldBeCalled();
352+
$root->getDevAutoload()->shouldBeCalled();
352353
$root->getRequires()->shouldNotBeCalled();
353354
$root->setAutoload(Argument::type('array'))->will(
354355
function ($args) use ($that) {
@@ -369,6 +370,28 @@ function ($args) use ($that) {
369370
);
370371
}
371372
);
373+
$root->setDevAutoload(Argument::type('array'))->will(
374+
function ($args) use ($that) {
375+
$that->assertEquals(
376+
array(
377+
'psr-4' => array(
378+
'Dev\\Kittens\\' => array( 'everywhere/', 'extensions/Foo/a/', 'extensions/Foo/b/' ),
379+
'Dev\\Cats\\' => 'extensions/Foo/src/'
380+
),
381+
'psr-0' => array(
382+
'DevUniqueGlobalClass' => 'extensions/Foo/',
383+
'' => 'extensions/Foo/dev/fallback/'
384+
),
385+
'files' => array( 'extensions/Foo/DevSemanticMediaWiki.php' ),
386+
'classmap' => array(
387+
'extensions/Foo/DevSemanticMediaWiki.hooks.php',
388+
'extensions/Foo/dev/includes/',
389+
),
390+
),
391+
$args[0]
392+
);
393+
}
394+
);
372395

373396
$extraInstalls = $this->triggerPlugin($root->reveal(), $dir);
374397

@@ -523,6 +546,7 @@ protected function rootFromJson($file)
523546
'suggest' => array(),
524547
'extra' => array(),
525548
'autoload' => array(),
549+
'autoload-dev' => array(),
526550
),
527551
$json
528552
);
@@ -534,6 +558,7 @@ protected function rootFromJson($file)
534558
$root->getSuggests()->willReturn($data['suggest']);
535559
$root->getExtra()->willReturn($data['extra'])->shouldBeCalled();
536560
$root->getAutoload()->willReturn($data['autoload']);
561+
$root->getDevAutoload()->willReturn($data['autoload-dev']);
537562

538563
$root->getStabilityFlags()->willReturn(array());
539564
$root->setStabilityFlags(Argument::type('array'))->will(

tests/phpunit/fixtures/testMergedAutoload/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"Kittens\\": "everywhere/"
55
}
66
},
7+
"autoload-dev": {
8+
"psr-4": {
9+
"Dev\\Kittens\\": "everywhere/"
10+
}
11+
},
712
"extra": {
813
"merge-plugin": {
914
"include": "extensions/*/composer.json"

tests/phpunit/fixtures/testMergedAutoload/extensions/Foo/composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,22 @@
1515
"SemanticMediaWiki.hooks.php",
1616
"includes/"
1717
]
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Dev\\Kittens\\": [ "a/", "b/" ],
22+
"Dev\\Cats\\": "src/"
23+
},
24+
"psr-0": {
25+
"DevUniqueGlobalClass": "",
26+
"": "dev/fallback/"
27+
},
28+
"files" : [
29+
"DevSemanticMediaWiki.php"
30+
],
31+
"classmap": [
32+
"DevSemanticMediaWiki.hooks.php",
33+
"dev/includes/"
34+
]
1835
}
1936
}

0 commit comments

Comments
 (0)