Completed
Push — master ( 6e95cd...8eea37 )
by Bryan
03:18
created

ExtraPackage::mergeExtraArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * This file is part of the Composer Merge plugin.
4
 *
5
 * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
6
 *
7
 * This software may be modified and distributed under the terms of the MIT
8
 * license. See the LICENSE file for details.
9
 */
10
11
namespace Wikimedia\Composer\Merge;
12
13
use Wikimedia\Composer\Logger;
14
15
use Composer\Composer;
16
use Composer\Json\JsonFile;
17
use Composer\Package\BasePackage;
18
use Composer\Package\CompletePackage;
19
use Composer\Package\Link;
20
use Composer\Package\Loader\ArrayLoader;
21
use Composer\Package\RootAliasPackage;
22
use Composer\Package\RootPackage;
23
use Composer\Package\RootPackageInterface;
24
use Composer\Package\Version\VersionParser;
25
use UnexpectedValueException;
26
27
/**
28
 * Processing for a composer.json file that will be merged into
29
 * a RootPackageInterface
30
 *
31
 * @author Bryan Davis <[email protected]>
32
 */
33
class ExtraPackage
34
{
35
36
    /**
37
     * @var Composer $composer
38
     */
39
    protected $composer;
40
41
    /**
42
     * @var Logger $logger
43
     */
44
    protected $logger;
45
46
    /**
47
     * @var string $path
48
     */
49
    protected $path;
50
51
    /**
52
     * @var array $json
53
     */
54
    protected $json;
55
56
    /**
57
     * @var CompletePackage $package
58
     */
59
    protected $package;
60
61
    /**
62
     * @var VersionParser $versionParser
63
     */
64
    protected $versionParser;
65
66
    /**
67
     * @param string $path Path to composer.json file
68
     * @param Composer $composer
69
     * @param Logger $logger
70
     */
71 140
    public function __construct($path, Composer $composer, Logger $logger)
72
    {
73 140
        $this->path = $path;
74 140
        $this->composer = $composer;
75 140
        $this->logger = $logger;
76 140
        $this->json = $this->readPackageJson($path);
77 140
        $this->package = $this->loadPackage($this->json);
78 140
        $this->versionParser = new VersionParser();
79 140
    }
80
81
    /**
82
     * Get list of additional packages to include if precessing recursively.
83
     *
84
     * @return array
85
     */
86 135
    public function getIncludes()
87
    {
88 135
        return isset($this->json['extra']['merge-plugin']['include']) ?
89 135
            $this->json['extra']['merge-plugin']['include'] : array();
90
    }
91
92
    /**
93
     * Get list of additional packages to require if precessing recursively.
94
     *
95
     * @return array
96
     */
97 135
    public function getRequires()
98
    {
99 135
        return isset($this->json['extra']['merge-plugin']['require']) ?
100 135
            $this->json['extra']['merge-plugin']['require'] : array();
101
    }
102
103
    /**
104
     * Read the contents of a composer.json style file into an array.
105
     *
106
     * The package contents are fixed up to be usable to create a Package
107
     * object by providing dummy "name" and "version" values if they have not
108
     * been provided in the file. This is consistent with the default root
109
     * package loading behavior of Composer.
110
     *
111
     * @param string $path
112
     * @return array
113
     */
114 140
    protected function readPackageJson($path)
115
    {
116 140
        $file = new JsonFile($path);
117 140
        $json = $file->read();
118 140
        if (!isset($json['name'])) {
119 130
            $json['name'] = 'merge-plugin/' .
120 130
                strtr($path, DIRECTORY_SEPARATOR, '-');
121 130
        }
122 140
        if (!isset($json['version'])) {
123 140
            $json['version'] = '1.0.0';
124 140
        }
125 140
        return $json;
126
    }
127
128
    /**
129
     * @param array $json
130
     * @return CompletePackage
131
     */
132 140
    protected function loadPackage(array $json)
133
    {
134 140
        $loader = new ArrayLoader();
135 140
        $package = $loader->load($json);
136
        // @codeCoverageIgnoreStart
137
        if (!$package instanceof CompletePackage) {
138
            throw new UnexpectedValueException(
139
                'Expected instance of CompletePackage, got ' .
140
                get_class($package)
141
            );
142
        }
143
        // @codeCoverageIgnoreEnd
144 140
        return $package;
145
    }
146
147
    /**
148
     * Merge this package into a RootPackageInterface
149
     *
150
     * @param RootPackageInterface $root
151
     * @param PluginState $state
152
     */
153 140
    public function mergeInto(RootPackageInterface $root, PluginState $state)
154
    {
155 140
        $this->addRepositories($root, $state->shouldPrependRepositories());
156
157 140
        $this->mergeRequires('require', $root, $state);
158
159 140
        $this->mergePackageLinks('conflict', $root);
160 140
        $this->mergePackageLinks('replace', $root);
161 140
        $this->mergePackageLinks('provide', $root);
162
163 140
        $this->mergeSuggests($root);
164
165 140
        $this->mergeAutoload('autoload', $root);
166
167 140
        $this->mergeExtra($root, $state);
168
169 140
        if ($state->isDevMode()) {
170 75
            $this->mergeDevInto($root, $state);
171 75
        } else {
172 65
            $this->mergeReferences($root);
173
        }
174 140
    }
175
176
    /**
177
     * Merge just the dev portion into a RootPackageInterface
178
     *
179
     * @param RootPackageInterface $root
180
     * @param PluginState $state
181
     */
182 135
    public function mergeDevInto(RootPackageInterface $root, PluginState $state)
183
    {
184 135
        $this->mergeRequires('require-dev', $root, $state);
185 135
        $this->mergeAutoload('devAutoload', $root);
186 135
        $this->mergeReferences($root);
187 135
    }
188
189
    /**
190
     * Add a collection of repositories described by the given configuration
191
     * to the given package and the global repository manager.
192
     *
193
     * @param RootPackageInterface $root
194
     * @param bool $prepend Should repositories be prepended to repository manager.
195
     */
196 140
    protected function addRepositories(RootPackageInterface $root, $prepend)
197
    {
198 140
        if (!isset($this->json['repositories'])) {
199 120
            return;
200
        }
201 20
        $repoManager = $this->composer->getRepositoryManager();
202 20
        $newRepos = array();
203
204 20
        foreach ($this->json['repositories'] as $repoJson) {
205 20
            if (!isset($repoJson['type'])) {
206 15
                continue;
207
            }
208 20
            if ($prepend) {
209 5
                $this->logger->info("Prepending {$repoJson['type']} repository");
210 5
            } else {
211 15
                $this->logger->info("Adding {$repoJson['type']} repository");
212
            }
213 20
            $repo = $repoManager->createRepository(
214 20
                $repoJson['type'],
215
                $repoJson
216 20
            );
217 20
            if ($prepend) {
218 5
                $repoManager->prependRepository($repo);
219 5
            } else {
220 15
                $repoManager->addRepository($repo);
221
            }
222 20
            $newRepos[] = $repo;
223 20
        }
224
225 20
        $unwrapped = self::unwrapIfNeeded($root, 'setRepositories');
226 20
        if ($prepend) {
227 5
            $unwrapped->setRepositories(array_merge(
228 5
                $newRepos,
229 5
                $root->getRepositories()
230 5
            ));
231 5
        } else {
232 15
            $unwrapped->setRepositories(array_merge(
233 15
                $root->getRepositories(),
234
                $newRepos
235 15
            ));
236
        }
237 20
    }
238
239
    /**
240
     * Merge require or require-dev into a RootPackageInterface
241
     *
242
     * @param string $type 'require' or 'require-dev'
243
     * @param RootPackageInterface $root
244
     * @param PluginState $state
245
     */
246 140
    protected function mergeRequires(
247
        $type,
248
        RootPackageInterface $root,
249
        PluginState $state
250
    ) {
251 140
        $linkType = BasePackage::$supportedLinkTypes[$type];
252 140
        $getter = 'get' . ucfirst($linkType['method']);
253 140
        $setter = 'set' . ucfirst($linkType['method']);
254
255 140
        $requires = $this->package->{$getter}();
256 140
        if (empty($requires)) {
257 105
            return;
258
        }
259
260 85
        $this->mergeStabilityFlags($root, $requires);
261
262 85
        $requires = $this->replaceSelfVersionDependencies(
263 85
            $type,
264 85
            $requires,
265
            $root
266 85
        );
267
268 85
        $root->{$setter}($this->mergeOrDefer(
269 85
            $type,
270 85
            $root->{$getter}(),
271 85
            $requires,
272
            $state
273 85
        ));
274 85
    }
275
276
    /**
277
     * Merge two collections of package links and collect duplicates for
278
     * subsequent processing.
279
     *
280
     * @param string $type 'require' or 'require-dev'
281
     * @param array $origin Primary collection
282
     * @param array $merge Additional collection
283
     * @param PluginState $state
284
     * @return array Merged collection
285
     */
286 85
    protected function mergeOrDefer(
287
        $type,
288
        array $origin,
289
        array $merge,
290
        $state
291
    ) {
292 85
        $dups = array();
293 85
        foreach ($merge as $name => $link) {
294 85
            if (!isset($origin[$name]) || $state->replaceDuplicateLinks()) {
295 85
                $this->logger->info("Merging <comment>{$name}</comment>");
296 85
                $origin[$name] = $link;
297 85
            } else {
298
                // Defer to solver.
299 25
                $this->logger->info(
300 25
                    "Deferring duplicate <comment>{$name}</comment>"
301 25
                );
302 25
                $dups[] = $link;
303
            }
304 85
        }
305 85
        $state->addDuplicateLinks($type, $dups);
306 85
        return $origin;
307
    }
308
309
    /**
310
     * Merge autoload or autoload-dev into a RootPackageInterface
311
     *
312
     * @param string $type 'autoload' or 'devAutoload'
313
     * @param RootPackageInterface $root
314
     */
315 140
    protected function mergeAutoload($type, RootPackageInterface $root)
316
    {
317 140
        $getter = 'get' . ucfirst($type);
318 140
        $setter = 'set' . ucfirst($type);
319
320 140
        $autoload = $this->package->{$getter}();
321 140
        if (empty($autoload)) {
322 130
            return;
323
        }
324
325 15
        $unwrapped = self::unwrapIfNeeded($root, $setter);
326 15
        $unwrapped->{$setter}(array_merge_recursive(
327 15
            $root->{$getter}(),
328 15
            $this->fixRelativePaths($autoload)
329 15
        ));
330 15
    }
331
332
    /**
333
     * Fix a collection of paths that are relative to this package to be
334
     * relative to the base package.
335
     *
336
     * @param array $paths
337
     * @return array
338
     */
339 15
    protected function fixRelativePaths(array $paths)
340
    {
341 15
        $base = dirname($this->path);
342 15
        $base = ($base === '.') ? '' : "{$base}/";
343
344 15
        array_walk_recursive(
345 15
            $paths,
346
            function (&$path) use ($base) {
347 15
                $path = "{$base}{$path}";
348 15
            }
349 15
        );
350 15
        return $paths;
351
    }
352
353
    /**
354
     * Extract and merge stability flags from the given collection of
355
     * requires and merge them into a RootPackageInterface
356
     *
357
     * @param RootPackageInterface $root
358
     * @param array $requires
359
     */
360 85
    protected function mergeStabilityFlags(
361
        RootPackageInterface $root,
362
        array $requires
363
    ) {
364 85
        $flags = $root->getStabilityFlags();
365 85
        $sf = new StabilityFlags($flags, $root->getMinimumStability());
366
367 85
        $unwrapped = self::unwrapIfNeeded($root, 'setStabilityFlags');
368 85
        $unwrapped->setStabilityFlags(array_merge(
369 85
            $flags,
370 85
            $sf->extractAll($requires)
371 85
        ));
372 85
    }
373
374
    /**
375
     * Merge package links of the given type  into a RootPackageInterface
376
     *
377
     * @param string $type 'conflict', 'replace' or 'provide'
378
     * @param RootPackageInterface $root
379
     */
380 140
    protected function mergePackageLinks($type, RootPackageInterface $root)
381
    {
382 140
        $linkType = BasePackage::$supportedLinkTypes[$type];
383 140
        $getter = 'get' . ucfirst($linkType['method']);
384 140
        $setter = 'set' . ucfirst($linkType['method']);
385
386 140
        $links = $this->package->{$getter}();
387 140
        if (!empty($links)) {
388 30
            $unwrapped = self::unwrapIfNeeded($root, $setter);
389
            // @codeCoverageIgnoreStart
390
            if ($root !== $unwrapped) {
391
                $this->logger->warning(
392
                    'This Composer version does not support ' .
393
                    "'{$type}' merging for aliased packages."
394
                );
395
            }
396
            // @codeCoverageIgnoreEnd
397 30
            $unwrapped->{$setter}(array_merge(
398 30
                $root->{$getter}(),
399 30
                $this->replaceSelfVersionDependencies($type, $links, $root)
400 30
            ));
401 30
        }
402 140
    }
403
404
    /**
405
     * Merge suggested packages into a RootPackageInterface
406
     *
407
     * @param RootPackageInterface $root
408
     */
409 140
    protected function mergeSuggests(RootPackageInterface $root)
410
    {
411 140
        $suggests = $this->package->getSuggests();
412 140
        if (!empty($suggests)) {
413 15
            $unwrapped = self::unwrapIfNeeded($root, 'setSuggests');
414 15
            $unwrapped->setSuggests(array_merge(
415 15
                $root->getSuggests(),
416
                $suggests
417 15
            ));
418 15
        }
419 140
    }
420
421
    /**
422
     * Merge extra config into a RootPackageInterface
423
     *
424
     * @param RootPackageInterface $root
425
     * @param PluginState $state
426
     */
427 140
    public function mergeExtra(RootPackageInterface $root, PluginState $state)
428
    {
429 140
        $extra = $this->package->getExtra();
430 140
        unset($extra['merge-plugin']);
431 140
        if (!$state->shouldMergeExtra() || empty($extra)) {
432 110
            return;
433
        }
434
435 30
        $rootExtra = $root->getExtra();
436 30
        $unwrapped = self::unwrapIfNeeded($root, 'setExtra');
437
438 30
        if ($state->replaceDuplicateLinks()) {
439 10
            $unwrapped->setExtra(
440 10
                self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra)
441 10
            );
442 10
        } else {
443 20
            if (!$state->shouldMergeExtraDeep()) {
444 15
                foreach (array_intersect(
445 15
                    array_keys($extra),
446 15
                    array_keys($rootExtra)
447 15
                ) as $key) {
448 5
                    $this->logger->info(
449 5
                        "Ignoring duplicate <comment>{$key}</comment> in ".
450 5
                        "<comment>{$this->path}</comment> extra config."
451 5
                    );
452 15
                }
453 15
            }
454 20
            $unwrapped->setExtra(
455 20
                self::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra)
456 20
            );
457
        }
458 30
    }
459
460
    /**
461
     * Merges two arrays either via arrayMergeDeep or via array_merge.
462
     *
463
     * @param bool $mergeDeep
464
     * @param array $array1
465
     * @param array $array2
466
     * @return array
467
     */
468 30
    public static function mergeExtraArray($mergeDeep, $array1, $array2)
469
    {
470 30
        if ($mergeDeep) {
471 10
            return NestedArray::mergeDeep($array1, $array2);
472
        }
473
474 20
        return array_merge($array1, $array2);
475
    }
476
477
    /**
478
     * Update Links with a 'self.version' constraint with the root package's
479
     * version.
480
     *
481
     * @param string $type Link type
482
     * @param array $links
483
     * @param RootPackageInterface $root
484
     * @return array
485
     */
486 100
    protected function replaceSelfVersionDependencies(
487
        $type,
488
        array $links,
489
        RootPackageInterface $root
490
    ) {
491 100
        $linkType = BasePackage::$supportedLinkTypes[$type];
492 100
        $version = $root->getVersion();
493 100
        $prettyVersion = $root->getPrettyVersion();
494 100
        $vp = $this->versionParser;
495
496 100
        $method = 'get' . ucfirst($linkType['method']);
497 100
        $packages = $root->$method();
498
499 100
        return array_map(
500 100
            function ($link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
501 100
                if ('self.version' === $link->getPrettyConstraint()) {
502 15
                    if (isset($packages[$link->getSource()])) {
503
                        /** @var Link $package */
504 10
                        $package = $packages[$link->getSource()];
505 10
                        return new Link(
506 10
                            $link->getSource(),
507 10
                            $link->getTarget(),
508 10
                            $vp->parseConstraints($package->getConstraint()->getPrettyString()),
509 10
                            $linkType['description'],
510 10
                            $package->getPrettyConstraint()
511 10
                        );
512
                    }
513
514 5
                    return new Link(
515 5
                        $link->getSource(),
516 5
                        $link->getTarget(),
517 5
                        $vp->parseConstraints($version),
518 5
                        $linkType['description'],
519
                        $prettyVersion
520 5
                    );
521
                }
522 100
                return $link;
523 100
            },
524
            $links
525 100
        );
526
    }
527
528
    /**
529
     * Get a full featured Package from a RootPackageInterface.
530
     *
531
     * In Composer versions before 599ad77 the RootPackageInterface only
532
     * defines a sub-set of operations needed by composer-merge-plugin and
533
     * RootAliasPackage only implemented those methods defined by the
534
     * interface. Most of the unimplemented methods in RootAliasPackage can be
535
     * worked around because the getter methods that are implemented proxy to
536
     * the aliased package which we can modify by unwrapping. The exception
537
     * being modifying the 'conflicts', 'provides' and 'replaces' collections.
538
     * We have no way to actually modify those collections unfortunately in
539
     * older versions of Composer.
540
     *
541
     * @param RootPackageInterface $root
542
     * @param string $method Method needed
543
     * @return RootPackageInterface|RootPackage
544
     */
545 140
    public static function unwrapIfNeeded(
546
        RootPackageInterface $root,
547
        $method = 'setExtra'
548
    ) {
549
        // @codeCoverageIgnoreStart
550
        if ($root instanceof RootAliasPackage &&
551
            !method_exists($root, $method)
552
        ) {
553
            // Unwrap and return the aliased RootPackage.
554
            $root = $root->getAliasOf();
555
        }
556
        // @codeCoverageIgnoreEnd
557 140
        return $root;
558
    }
559
560
    /**
561
     * Update the root packages reference information.
562
     *
563
     * @param RootPackageInterface $root
564
     */
565 140
    protected function mergeReferences(RootPackageInterface $root)
566
    {
567
        // Merge source reference information for merged packages.
568
        // @see RootPackageLoader::load
569 140
        $references = array();
570 140
        $unwrapped = $this->unwrapIfNeeded($root, 'setReferences');
571 140
        foreach (array('require', 'require-dev') as $linkType) {
572 140
            $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
573 140
            $method = 'get'.ucfirst($linkInfo['method']);
574 140
            $links = array();
575 140
            foreach ($unwrapped->$method() as $link) {
576 55
                $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
577 140
            }
578 140
            $references = $this->extractReferences($links, $references);
579 140
        }
580 140
        $unwrapped->setReferences($references);
581 140
    }
582
583
    /**
584
     * Extract vcs revision from version constraint (dev-master#abc123.
585
     *
586
     * @param array $requires
587
     * @param array $references
588
     * @return array
589
     * @see RootPackageLoader::extractReferences()
590
     */
591 140
    protected function extractReferences(array $requires, array $references)
592
    {
593 140
        foreach ($requires as $reqName => $reqVersion) {
594 55
            $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion);
595 55
            $stabilityName = VersionParser::parseStability($reqVersion);
596
            if (
597 55
                preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) &&
598
                $stabilityName === 'dev'
599 55
            ) {
600 15
                $name = strtolower($reqName);
601 15
                $references[$name] = $match[1];
602 15
            }
603 140
        }
604
605 140
        return $references;
606
    }
607
}
608
// vim:sw=4:ts=4:sts=4:et:
609