Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
36 / 42 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
ComposerInfo | |
85.71% |
36 / 42 |
|
62.50% |
5 / 8 |
21.17 | |
0.00% |
0 / 1 |
__construct | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
getAvailableTaoExtensions | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
extractExtensionDependencies | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getPackageId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEncodedFileContent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getComposerJson | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getComposerLock | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
5.02 | |||
getTaoRoot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace oat\oatbox\extension; |
25 | |
26 | use common_ext_ExtensionException; |
27 | |
28 | /** |
29 | * Class ComposerInfo |
30 | * @package oat\oatbox\extension |
31 | */ |
32 | class ComposerInfo |
33 | { |
34 | private static $jsons = []; |
35 | private static $locks = []; |
36 | private static $availablePackages; |
37 | |
38 | /** @var null|string */ |
39 | private $rootDir; |
40 | |
41 | private const COMPOSER_JSON = 'composer.json'; |
42 | private const COMPOSER_LOCK = 'composer.lock'; |
43 | private const COMPOSER_LOCK_PACKAGES = 'packages'; |
44 | private const COMPOSER_LOCK_EXTRA = 'extra'; |
45 | private const COMPOSER_LOCK_EXTENSION_NAME = 'tao-extension-name'; |
46 | private const COMPOSER_LOCK_PACKAGE_NAME = 'name'; |
47 | |
48 | /** |
49 | * ComposerInfo constructor. |
50 | * @param string|null $rootDir directory where composer file located |
51 | * @throws common_ext_ExtensionException |
52 | */ |
53 | public function __construct(string $rootDir = null) |
54 | { |
55 | if ($rootDir === null) { |
56 | $this->rootDir = defined('ROOT_PATH') ? ROOT_PATH : realpath(__DIR__ . '/../../../../'); |
57 | } else { |
58 | $this->rootDir = $rootDir; |
59 | } |
60 | $composerJsonPath = realpath($this->rootDir) . DIRECTORY_SEPARATOR . self::COMPOSER_JSON; |
61 | |
62 | if (!file_exists($composerJsonPath)) { |
63 | throw new common_ext_ExtensionException(sprintf('Composer file missed at %s', $this->rootDir)); |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * @return array |
69 | * @throws common_ext_ExtensionException |
70 | */ |
71 | public function getAvailableTaoExtensions(): array |
72 | { |
73 | if (self::$availablePackages !== null) { |
74 | return self::$availablePackages; |
75 | } |
76 | |
77 | self::$availablePackages = []; |
78 | $composerLock = $this->getComposerLock(); |
79 | |
80 | $extensionPackages = array_filter($composerLock[self::COMPOSER_LOCK_PACKAGES], function ($package) { |
81 | return isset($package[self::COMPOSER_LOCK_EXTRA][self::COMPOSER_LOCK_EXTENSION_NAME]); |
82 | }); |
83 | foreach ($extensionPackages as $package) { |
84 | $extId = $package[self::COMPOSER_LOCK_EXTRA][self::COMPOSER_LOCK_EXTENSION_NAME]; |
85 | self::$availablePackages[$package[self::COMPOSER_LOCK_PACKAGE_NAME]] = $extId; |
86 | } |
87 | |
88 | return self::$availablePackages; |
89 | } |
90 | |
91 | /** |
92 | * Get dependant tao extensions |
93 | * @return array |
94 | * @throws common_ext_ExtensionException |
95 | */ |
96 | public function extractExtensionDependencies() |
97 | { |
98 | $result = []; |
99 | $availableTaoExtensions = $this->getAvailableTaoExtensions(); |
100 | $composerJson = $this->getComposerJson(); |
101 | foreach ($composerJson['require'] as $packageId => $packageVersion) { |
102 | if (isset($availableTaoExtensions[$packageId])) { |
103 | $result[$availableTaoExtensions[$packageId]] = $packageVersion; |
104 | } |
105 | } |
106 | return $result; |
107 | } |
108 | |
109 | /** |
110 | * @return string |
111 | */ |
112 | public function getPackageId(): string |
113 | { |
114 | return $this->getComposerJson()['name']; |
115 | } |
116 | |
117 | /** |
118 | * @param $path |
119 | * @return mixed |
120 | */ |
121 | private function getEncodedFileContent($path) |
122 | { |
123 | $content = file_get_contents($path); |
124 | return json_decode($content, true); |
125 | } |
126 | |
127 | /** |
128 | * @return array |
129 | */ |
130 | private function getComposerJson(): array |
131 | { |
132 | if (!isset(self::$jsons[$this->rootDir])) { |
133 | $file = realpath($this->rootDir) . DIRECTORY_SEPARATOR . self::COMPOSER_JSON; |
134 | self::$jsons[$this->rootDir] = $this->getEncodedFileContent($file); |
135 | } |
136 | return self::$jsons[$this->rootDir]; |
137 | } |
138 | |
139 | |
140 | /** |
141 | * @return array |
142 | * @throws common_ext_ExtensionException |
143 | */ |
144 | private function getComposerLock(): array |
145 | { |
146 | if (isset(self::$locks[$this->rootDir])) { |
147 | return self::$locks[$this->rootDir]; |
148 | } |
149 | |
150 | $composerLockPath = realpath($this->rootDir) . DIRECTORY_SEPARATOR . self::COMPOSER_LOCK; |
151 | if (!file_exists($composerLockPath)) { |
152 | $composerLockPath = rtrim($this->getTaoRoot(), DIRECTORY_SEPARATOR) . |
153 | DIRECTORY_SEPARATOR . self::COMPOSER_LOCK; |
154 | } |
155 | |
156 | if (!file_exists($composerLockPath)) { |
157 | throw new common_ext_ExtensionException(sprintf('Composer lock file missed at %s', $composerLockPath)); |
158 | } |
159 | |
160 | self::$locks[$this->rootDir] = $this->getEncodedFileContent($composerLockPath); |
161 | |
162 | return self::$locks[$this->rootDir]; |
163 | } |
164 | |
165 | /** |
166 | * @return false|string |
167 | */ |
168 | private function getTaoRoot(): string |
169 | { |
170 | return defined('ROOT_PATH') ? ROOT_PATH : realpath(__DIR__ . '/../../../../'); |
171 | } |
172 | } |