Dimension
6 years ago
API.php
6 years ago
AggregatedMetric.php
6 years ago
ArchivedMetric.php
6 years ago
Archiver.php
6 years ago
Categories.php
6 years ago
ComponentFactory.php
6 years ago
ComputedMetric.php
6 years ago
ConsoleCommand.php
6 years ago
Controller.php
6 years ago
ControllerAdmin.php
6 years ago
Dependency.php
6 years ago
LogTablesProvider.php
6 years ago
Manager.php
6 years ago
Menu.php
6 years ago
MetadataLoader.php
6 years ago
Metric.php
6 years ago
PluginException.php
6 years ago
ProcessedMetric.php
6 years ago
ReleaseChannels.php
6 years ago
Report.php
6 years ago
ReportsProvider.php
6 years ago
RequestProcessors.php
6 years ago
Segment.php
6 years ago
SettingsProvider.php
6 years ago
Tasks.php
6 years ago
ThemeStyles.php
6 years ago
ViewDataTable.php
6 years ago
Visualization.php
6 years ago
WidgetsProvider.php
6 years ago
Dependency.php
171 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Composer\Semver\VersionParser; |
| 12 | use Piwik\Plugin\Manager as PluginManager; |
| 13 | use Piwik\Plugins\Marketplace\Environment; |
| 14 | use Piwik\Version; |
| 15 | |
| 16 | /** |
| 17 | * |
| 18 | */ |
| 19 | class Dependency |
| 20 | { |
| 21 | private $piwikVersion; |
| 22 | private $phpVersion; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | $this->setPiwikVersion(Version::VERSION); |
| 27 | $this->setPhpVersion(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION); |
| 28 | } |
| 29 | |
| 30 | public function setEnvironment(Environment $environment) |
| 31 | { |
| 32 | $this->setPiwikVersion($environment->getPiwikVersion()); |
| 33 | $this->setPhpVersion($environment->getPhpVersion()); |
| 34 | } |
| 35 | |
| 36 | public function getMissingDependencies($requires) |
| 37 | { |
| 38 | $missingRequirements = array(); |
| 39 | |
| 40 | if (empty($requires)) { |
| 41 | return $missingRequirements; |
| 42 | } |
| 43 | |
| 44 | foreach ($requires as $name => $requiredVersion) { |
| 45 | $currentVersion = $this->getCurrentVersion($name); |
| 46 | $missingVersions = $this->getMissingVersions($currentVersion, $requiredVersion); |
| 47 | |
| 48 | if (!empty($missingVersions)) { |
| 49 | $missingRequirements[] = array( |
| 50 | 'requirement' => $name, |
| 51 | 'actualVersion' => $currentVersion, |
| 52 | 'requiredVersion' => $requiredVersion, |
| 53 | 'causedBy' => implode(', ', $missingVersions) |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return $missingRequirements; |
| 59 | } |
| 60 | |
| 61 | public function getMissingVersions($currentVersion, $requiredVersion) |
| 62 | { |
| 63 | $currentVersion = trim($currentVersion); |
| 64 | |
| 65 | $missingVersions = array(); |
| 66 | |
| 67 | if (empty($currentVersion)) { |
| 68 | if (!empty($requiredVersion)) { |
| 69 | $missingVersions[] = (string) $requiredVersion; |
| 70 | } |
| 71 | |
| 72 | return $missingVersions; |
| 73 | } |
| 74 | |
| 75 | $requiredVersion = $this->makeVersionBackwardsCompatibleIfNoComparisonDefined($requiredVersion); |
| 76 | |
| 77 | $version = new VersionParser(); |
| 78 | $constraintsExisting = $version->parseConstraints($currentVersion); |
| 79 | |
| 80 | $requiredVersions = explode(',', (string) $requiredVersion); |
| 81 | |
| 82 | foreach ($requiredVersions as $required) { |
| 83 | $required = trim($required); |
| 84 | |
| 85 | if (empty($required)) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | $required = $this->makeVersionBackwardsCompatibleIfNoComparisonDefined($required); |
| 90 | $constraintRequired = $version->parseConstraints($required); |
| 91 | |
| 92 | if (!$constraintRequired->matches($constraintsExisting)) { |
| 93 | $missingVersions[] = $required; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $missingVersions; |
| 98 | } |
| 99 | |
| 100 | private function makeVersionBackwardsCompatibleIfNoComparisonDefined($version) |
| 101 | { |
| 102 | if (!empty($version) && preg_match('/^(\d+)\.(\d+)/', $version)) { |
| 103 | // TODO: we should remove this from piwik 3. To stay BC we add >= if no >= is defined yet |
| 104 | $version = '>=' . $version; |
| 105 | } |
| 106 | |
| 107 | return $version; |
| 108 | } |
| 109 | |
| 110 | public function setPiwikVersion($piwikVersion) |
| 111 | { |
| 112 | $this->piwikVersion = $piwikVersion; |
| 113 | } |
| 114 | |
| 115 | public function setPhpVersion($phpVersion) |
| 116 | { |
| 117 | $this->phpVersion = $phpVersion; |
| 118 | } |
| 119 | |
| 120 | public function hasDependencyToDisabledPlugin($requires) |
| 121 | { |
| 122 | if (empty($requires)) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | foreach ($requires as $name => $requiredVersion) { |
| 127 | $nameLower = strtolower($name); |
| 128 | $isPluginRequire = !in_array($nameLower, array('piwik', 'php', 'matomo')); |
| 129 | if ($isPluginRequire) { |
| 130 | // we do not check version, only whether it's activated. Everything that is not piwik or php is assumed |
| 131 | // a plugin so far. |
| 132 | if (!PluginManager::getInstance()->isPluginActivated($name)) { |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | private function getCurrentVersion($name) |
| 142 | { |
| 143 | switch (strtolower($name)) { |
| 144 | case 'matomo': |
| 145 | case 'piwik': |
| 146 | return $this->piwikVersion; |
| 147 | case 'php': |
| 148 | return $this->phpVersion; |
| 149 | default: |
| 150 | try { |
| 151 | $pluginNames = PluginManager::getAllPluginsNames(); |
| 152 | |
| 153 | if (!in_array($name, $pluginNames) || !PluginManager::getInstance()->isPluginLoaded($name)) { |
| 154 | return ''; |
| 155 | } |
| 156 | |
| 157 | $plugin = PluginManager::getInstance()->loadPlugin(ucfirst($name)); |
| 158 | |
| 159 | if (!empty($plugin)) { |
| 160 | return $plugin->getVersion(); |
| 161 | } |
| 162 | } catch (\Exception $e) { |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return ''; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | } |
| 171 |