PageRenderTime 1294ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Plugin/Dependency.php

https://github.com/CodeYellowBV/piwik
PHP | 106 lines | 75 code | 21 blank | 10 comment | 8 complexity | 93352a7a2010e5697b6b75291861407c MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugin;
  10. use Piwik\Plugin\Manager as PluginManager;
  11. use Piwik\Version;
  12. /**
  13. *
  14. */
  15. class Dependency
  16. {
  17. private $piwikVersion;
  18. public function __construct()
  19. {
  20. $this->setPiwikVersion(Version::VERSION);
  21. }
  22. public function getMissingDependencies($requires)
  23. {
  24. $missingRequirements = array();
  25. if (empty($requires)) {
  26. return $missingRequirements;
  27. }
  28. foreach ($requires as $name => $requiredVersion) {
  29. $currentVersion = $this->getCurrentVersion($name);
  30. $missingVersions = $this->getMissingVersions($currentVersion, $requiredVersion);
  31. if (!empty($missingVersions)) {
  32. $missingRequirements[] = array(
  33. 'requirement' => $name,
  34. 'actualVersion' => $currentVersion,
  35. 'requiredVersion' => $requiredVersion,
  36. 'causedBy' => implode(', ', $missingVersions)
  37. );
  38. }
  39. }
  40. return $missingRequirements;
  41. }
  42. public function getMissingVersions($currentVersion, $requiredVersion)
  43. {
  44. $currentVersion = trim($currentVersion);
  45. $requiredVersions = explode(',' , (string) $requiredVersion);
  46. $missingVersions = array();
  47. foreach ($requiredVersions as $required) {
  48. $comparison = '>=';
  49. $required = trim($required);
  50. if (preg_match('{^(<>|!=|>=?|<=?|==?)\s*(.*)}', $required, $matches)) {
  51. $required = $matches[2];
  52. $comparison = trim($matches[1]);
  53. }
  54. if (false === version_compare($currentVersion, $required, $comparison)) {
  55. $missingVersions[] = $comparison . $required;
  56. }
  57. }
  58. return $missingVersions;
  59. }
  60. public function setPiwikVersion($piwikVersion)
  61. {
  62. $this->piwikVersion = $piwikVersion;
  63. }
  64. private function getCurrentVersion($name)
  65. {
  66. switch (strtolower($name)) {
  67. case 'piwik':
  68. return $this->piwikVersion;
  69. case 'php':
  70. return PHP_VERSION;
  71. default:
  72. try {
  73. $pluginNames = PluginManager::getAllPluginsNames();
  74. if (!in_array($name, $pluginNames) || !PluginManager::getInstance()->isPluginLoaded($name)) {
  75. return '';
  76. }
  77. $plugin = PluginManager::getInstance()->loadPlugin(ucfirst($name));
  78. if (!empty($plugin)) {
  79. return $plugin->getVersion();
  80. }
  81. } catch (\Exception $e) {}
  82. }
  83. return '';
  84. }
  85. }