ConsoleCommand
2 years ago
Dimension
2 years ago
API.php
2 years ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
2 years ago
Archiver.php
2 years ago
Categories.php
2 years ago
ComponentFactory.php
2 years ago
ComputedMetric.php
2 years ago
ConsoleCommand.php
2 years ago
Controller.php
2 years ago
ControllerAdmin.php
2 years ago
Dependency.php
2 years ago
LogTablesProvider.php
2 years ago
Manager.php
2 years ago
Menu.php
2 years ago
MetadataLoader.php
2 years ago
Metric.php
2 years ago
PluginException.php
2 years ago
ProcessedMetric.php
2 years ago
ReleaseChannels.php
2 years ago
Report.php
2 years ago
ReportsProvider.php
2 years ago
RequestProcessors.php
2 years ago
Segment.php
2 years ago
SettingsProvider.php
2 years ago
Tasks.php
2 years ago
ThemeStyles.php
2 years ago
ViewDataTable.php
2 years ago
Visualization.php
2 years ago
WidgetsProvider.php
2 years ago
MetadataLoader.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Plugin; |
| 11 | |
| 12 | use Exception; |
| 13 | use Piwik\Piwik; |
| 14 | use Piwik\Url; |
| 15 | use Piwik\Version; |
| 16 | /** |
| 17 | * @see core/Version.php |
| 18 | */ |
| 19 | require_once PIWIK_INCLUDE_PATH . '/core/Version.php'; |
| 20 | /** |
| 21 | * Loads plugin metadata found in the following files: |
| 22 | * - piwik.json |
| 23 | */ |
| 24 | class MetadataLoader |
| 25 | { |
| 26 | const PLUGIN_JSON_FILENAME = 'plugin.json'; |
| 27 | /** |
| 28 | * The name of the plugin whose metadata will be loaded. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $pluginName; |
| 33 | /** |
| 34 | * Constructor. |
| 35 | * |
| 36 | * @param string $pluginName Name of the plugin to load metadata. |
| 37 | */ |
| 38 | public function __construct($pluginName) |
| 39 | { |
| 40 | $this->pluginName = $pluginName; |
| 41 | } |
| 42 | /** |
| 43 | * Loads plugin metadata. @see Plugin::getInformation. |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | public function load() |
| 48 | { |
| 49 | $defaults = $this->getDefaultPluginInformation(); |
| 50 | $plugin = $this->loadPluginInfoJson(); |
| 51 | // use translated plugin description if available |
| 52 | if ($defaults['description'] != Piwik::translate($defaults['description'])) { |
| 53 | unset($plugin['description']); |
| 54 | } |
| 55 | // look for a license file |
| 56 | $licenseFile = $this->getPathToLicenseFile(); |
| 57 | if (!empty($licenseFile)) { |
| 58 | $plugin['license_file'] = $licenseFile; |
| 59 | } |
| 60 | return array_merge($defaults, $plugin); |
| 61 | } |
| 62 | public function hasPluginJson() |
| 63 | { |
| 64 | $hasJson = $this->loadPluginInfoJson(); |
| 65 | return !empty($hasJson); |
| 66 | } |
| 67 | private function getDefaultPluginInformation() |
| 68 | { |
| 69 | $descriptionKey = $this->pluginName . '_PluginDescription'; |
| 70 | return ['description' => $descriptionKey, 'homepage' => Url::addCampaignParametersToMatomoLink('https://matomo.org/'), 'authors' => [['name' => 'Matomo', 'homepage' => Url::addCampaignParametersToMatomoLink('https://matomo.org/')]], 'license' => 'GPL v3+', 'version' => Version::VERSION, 'theme' => false, 'require' => []]; |
| 71 | } |
| 72 | /** |
| 73 | * It is important that this method works without using anything from DI |
| 74 | * @return array|mixed |
| 75 | */ |
| 76 | public function loadPluginInfoJson() |
| 77 | { |
| 78 | $path = $this->getPathToPluginJson(); |
| 79 | return $this->loadJsonMetadata($path); |
| 80 | } |
| 81 | public function getPathToPluginJson() |
| 82 | { |
| 83 | $path = $this->getPathToPluginFolder() . '/' . self::PLUGIN_JSON_FILENAME; |
| 84 | return $path; |
| 85 | } |
| 86 | private function loadJsonMetadata($path) |
| 87 | { |
| 88 | if (!file_exists($path)) { |
| 89 | return array(); |
| 90 | } |
| 91 | $json = file_get_contents($path); |
| 92 | if (!$json) { |
| 93 | return array(); |
| 94 | } |
| 95 | $info = json_decode($json, $assoc = true); |
| 96 | if (!is_array($info) || empty($info)) { |
| 97 | throw new Exception("Invalid JSON file: {$path}"); |
| 98 | } |
| 99 | return $info; |
| 100 | } |
| 101 | /** |
| 102 | * @return string |
| 103 | */ |
| 104 | private function getPathToPluginFolder() |
| 105 | { |
| 106 | return \Piwik\Plugin\Manager::getPluginDirectory($this->pluginName); |
| 107 | } |
| 108 | /** |
| 109 | * @return null|string |
| 110 | */ |
| 111 | public function getPathToLicenseFile() |
| 112 | { |
| 113 | $prefixPath = $this->getPathToPluginFolder() . '/'; |
| 114 | $licenseFiles = array('LICENSE', 'LICENSE.md', 'LICENSE.txt'); |
| 115 | foreach ($licenseFiles as $licenseFile) { |
| 116 | $pathToLicense = $prefixPath . $licenseFile; |
| 117 | if (is_file($pathToLicense) && is_readable($pathToLicense)) { |
| 118 | return $pathToLicense; |
| 119 | } |
| 120 | } |
| 121 | return null; |
| 122 | } |
| 123 | } |
| 124 |