Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
46.55% |
27 / 58 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
helpers_ExtensionHelper | |
46.55% |
27 / 58 |
|
16.67% |
1 / 6 |
147.71 | |
0.00% |
0 / 1 |
getMissingExtensionIds | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
sortByDependencies | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
10 | |||
sortById | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
isRequired | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
mustBeEnabled | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
checkRequiredExtensions | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | * @author Joel Bout, <joel@taotesting.com> |
21 | * @license GPLv2 |
22 | * @package generis |
23 | * |
24 | */ |
25 | |
26 | use oat\oatbox\extension\exception\ManifestNotFoundException; |
27 | |
28 | class helpers_ExtensionHelper |
29 | { |
30 | /** |
31 | * Based on a list of extensions we generate an array of missing extensions |
32 | * |
33 | * @param common_ext_Extension[] $extensions |
34 | * @return array array of missing extensions ids |
35 | */ |
36 | public static function getMissingExtensionIds($extensions) |
37 | { |
38 | $inList = []; |
39 | foreach ($extensions as $ext) { |
40 | $inList[] = $ext->getId(); |
41 | } |
42 | $missing = []; |
43 | foreach ($extensions as $ext) { |
44 | foreach ($ext->getDependencies() as $extId => $version) { |
45 | if (!in_array($extId, $inList) && !in_array($extId, $missing)) { |
46 | $missing[] = $extId; |
47 | } |
48 | } |
49 | } |
50 | return $missing; |
51 | } |
52 | |
53 | /** |
54 | * Sorts a list of extensions by dependencies, |
55 | * starting with independent extensions |
56 | * |
57 | * @param array $extensions |
58 | * @throws common_exception_Error |
59 | * @return common_ext_Extension[] |
60 | */ |
61 | public static function sortByDependencies($extensions) |
62 | { |
63 | $sorted = []; |
64 | $unsorted = []; |
65 | foreach ($extensions as $ext) { |
66 | $unsorted[$ext->getId()] = array_keys($ext->getDependencies()); |
67 | } |
68 | while (!empty($unsorted)) { |
69 | $before = count($unsorted); |
70 | foreach (array_keys($unsorted) as $id) { |
71 | $missing = array_diff($unsorted[$id], $sorted); |
72 | if (empty($missing)) { |
73 | $sorted[] = $id; |
74 | unset($unsorted[$id]); |
75 | } |
76 | } |
77 | if (count($unsorted) == $before) { |
78 | $notfound = array_diff($missing, array_keys($unsorted)); |
79 | if (!empty($notfound)) { |
80 | throw new common_exception_Error( |
81 | 'Missing extensions ' . implode(',', $notfound) . ' for: ' |
82 | . implode(',', array_keys($unsorted)) |
83 | ); |
84 | } else { |
85 | throw new common_exception_Error( |
86 | 'Cyclic extension dependencies for: ' . implode(',', array_keys($unsorted)) |
87 | ); |
88 | } |
89 | } |
90 | } |
91 | |
92 | $returnValue = []; |
93 | foreach ($sorted as $id) { |
94 | foreach ($extensions as $ext) { |
95 | if ($ext->getId() == $id) { |
96 | $returnValue[$id] = $ext; |
97 | } |
98 | } |
99 | } |
100 | return $returnValue; |
101 | } |
102 | |
103 | public static function sortById($extensions) |
104 | { |
105 | usort($extensions, function ($a, $b) { |
106 | return strcasecmp($a->getId(), $b->getId()); |
107 | }); |
108 | return $extensions; |
109 | } |
110 | |
111 | /** |
112 | * Whenever or not the extension is required by other installed extensions |
113 | * |
114 | * @param common_ext_Extension $extension |
115 | * @return boolean |
116 | */ |
117 | public static function isRequired(common_ext_Extension $extension) |
118 | { |
119 | foreach (common_ext_ExtensionsManager::singleton()->getInstalledExtensions() as $ext) { |
120 | foreach ($ext->getDependencies() as $extId => $version) { |
121 | if ($extId == $extension->getId()) { |
122 | return true; |
123 | } |
124 | } |
125 | } |
126 | return false; |
127 | } |
128 | |
129 | /** |
130 | * Whenever or not the extension is required to be enabled |
131 | * by other enabled extensions |
132 | * |
133 | * @param common_ext_Extension $extension |
134 | * @return boolean |
135 | */ |
136 | public static function mustBeEnabled(common_ext_Extension $extension) |
137 | { |
138 | foreach (common_ext_ExtensionsManager::singleton()->getEnabledExtensions() as $ext) { |
139 | foreach ($ext->getDependencies() as $extId => $version) { |
140 | if ($extId == $extension->getId()) { |
141 | return true; |
142 | } |
143 | } |
144 | } |
145 | return false; |
146 | } |
147 | |
148 | /** |
149 | * Check if extension requirements are met |
150 | * Always returns true, but throws exception on error |
151 | * |
152 | * @param common_ext_Extension $extension |
153 | * @return boolean |
154 | * @throws ManifestNotFoundException |
155 | * @throws common_ext_MissingExtensionException |
156 | */ |
157 | public static function checkRequiredExtensions(common_ext_Extension $extension) |
158 | { |
159 | $extensionManager = common_ext_ExtensionsManager::singleton(); |
160 | // read direct dependencies from manifest, do not check recursively |
161 | foreach ($extension->getManifest()->getDependencies() as $requiredExt => $requiredVersion) { |
162 | if (!$extensionManager->isInstalled($requiredExt)) { |
163 | throw new common_ext_MissingExtensionException( |
164 | 'Extension ' . $requiredExt . ' is needed by the extension to be installed but is missing.', |
165 | 'GENERIS' |
166 | ); |
167 | } |
168 | } |
169 | // always return true, or throws an exception |
170 | return true; |
171 | } |
172 | } |