Passed
Push — master ( 068bff...27cbbc )
by Kirill
03:24
created

Manifest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 80
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A loadDependencyExtras() 0 13 3
A readExtra() 0 4 1
A jsonSerialize() 0 4 1
A loadRootExtras() 0 4 1
A discover() 0 9 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Console;
11
12
use Composer\Composer;
13
use Composer\Installer\InstallationManager;
14
use Composer\Package\Link;
15
use Composer\Package\RootPackageInterface;
16
use Composer\Script\Event;
17
18
/**
19
 * Class Manifest
20
 */
21
class Manifest implements \JsonSerializable
22
{
23
    /**
24
     * @var array
25
     */
26
    private $manifest;
27
28
    /**
29
     * Composer constructor.
30
     * @param Composer $app
31
     */
32
    public function __construct(Composer $app)
33
    {
34
        $this->manifest = $this->loadRootExtras($app->getPackage());
35
36
        foreach ($this->loadDependencyExtras($app) as $extra) {
37
            $this->manifest = \array_merge_recursive($this->manifest, $extra);
38
        }
39
    }
40
41
    /**
42
     * @param Composer $app
43
     * @return iterable
44
     * @throws \InvalidArgumentException
45
     */
46
    private function loadDependencyExtras(Composer $app): iterable
47
    {
48
        $manager = $app->getInstallationManager();
49
        $repository = $app->getRepositoryManager()->getLocalRepository();
50
51
        foreach ($repository->getPackages() as $package) {
52
            if ($manager->isPackageInstalled($repository, $package)) {
53
                $path = $manager->getInstallPath($package) . '/composer.json';
54
55
                yield $this->readExtra(\json_decode(\file_get_contents($path), true));
56
            }
57
        }
58
    }
59
60
    /**
61
     * @param array $dep
62
     * @return array
63
     */
64
    private function readExtra(array $dep): array
65
    {
66
        return $dep['extra']['railt'] ?? [];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function jsonSerialize(): array
73
    {
74
        return $this->manifest;
75
    }
76
77
    /**
78
     * @param RootPackageInterface $app
79
     * @return array
80
     */
81
    private function loadRootExtras(RootPackageInterface $app): array
82
    {
83
        return (array)($app->getExtra()['railt'] ?? []);
84
    }
85
86
    /**
87
     * @param Event $event
88
     * @throws \InvalidArgumentException
89
     * @throws \RuntimeException
90
     */
91
    public static function discover(Event $event): void
92
    {
93
        $composer = $event->getComposer();
94
        $manifest = new static($composer);
95
96
        $dir = $composer->getConfig()->get('bin-dir');
97
98
        \file_put_contents($dir . '/railt.json', \json_encode($manifest));
99
    }
100
}
101
102