1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Enalean, 2013. All Rights Reserved. |
4
|
|
|
* |
5
|
|
|
* This file is a part of Tuleap. |
6
|
|
|
* |
7
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This class is responsible of detecting if there are unmet dependencies in plugin |
23
|
|
|
*/ |
24
|
|
|
class PluginDependencySolver { |
25
|
|
|
|
26
|
|
|
/** @var PluginManager */ |
27
|
|
|
private $plugin_manager; |
28
|
|
|
|
29
|
|
|
public function __construct(PluginManager $plugin_manager) { |
30
|
|
|
$this->plugin_manager = $plugin_manager; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get plugin names that are still installed and which depends on the given plugin |
35
|
|
|
* |
36
|
|
|
* @return array of strings |
37
|
|
|
*/ |
38
|
|
|
public function getInstalledDependencies(Plugin $plugin) { |
39
|
|
|
return $this->getMissingDependencies($plugin, $this->plugin_manager->getAllPlugins()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get plugin names that are still available and which depends on the given plugin |
44
|
|
|
* |
45
|
|
|
* @return array of strings |
46
|
|
|
*/ |
47
|
|
|
public function getAvailableDependencies(Plugin $plugin) { |
48
|
|
|
return $this->getMissingDependencies($plugin, $this->plugin_manager->getAvailablePlugins()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get plugin names that should already be installed for the given plugin name |
53
|
|
|
* |
54
|
|
|
* @return array of strings |
55
|
|
|
*/ |
56
|
|
|
public function getUnmetInstalledDependencies($plugin_name) { |
57
|
|
|
$plugin = $this->plugin_manager->getPluginDuringInstall($plugin_name); |
58
|
|
|
return $this->getUnmetMissingDependencies($plugin, 'getPluginByName'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get plugin names that should already be available for the given plugin name |
63
|
|
|
* |
64
|
|
|
* @return array of strings |
65
|
|
|
*/ |
66
|
|
|
public function getUnmetAvailableDependencies(Plugin $plugin) { |
67
|
|
|
return $this->getUnmetMissingDependencies($plugin, 'getAvailablePluginByName'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function getUnmetMissingDependencies(Plugin $plugin, $method) { |
71
|
|
|
$unmet_dependencies = array(); |
72
|
|
|
foreach ($plugin->getDependencies() as $dependency_name) { |
73
|
|
|
$dependency_plugin = $this->plugin_manager->$method($dependency_name); |
74
|
|
|
if (! $dependency_plugin) { |
75
|
|
|
$unmet_dependencies[] = $dependency_name; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $unmet_dependencies; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getMissingDependencies(Plugin $plugin, array $plugins_collection) { |
82
|
|
|
$missing_dependencies = array(); |
83
|
|
|
foreach ($plugins_collection as $candidate_plugin) { |
84
|
|
|
if (in_array($plugin->getName(), $candidate_plugin->getDependencies())) { |
85
|
|
|
$missing_dependencies[] = $candidate_plugin->getName(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $missing_dependencies; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
?> |
92
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: