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