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
MetadataLoader.php
159 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 Exception; |
| 12 | use Piwik\Piwik; |
| 13 | use Piwik\Version; |
| 14 | use Piwik\Plugin; |
| 15 | |
| 16 | /** |
| 17 | * @see core/Version.php |
| 18 | */ |
| 19 | require_once PIWIK_INCLUDE_PATH . '/core/Version.php'; |
| 20 | |
| 21 | /** |
| 22 | * Loads plugin metadata found in the following files: |
| 23 | * - piwik.json |
| 24 | */ |
| 25 | class MetadataLoader |
| 26 | { |
| 27 | const PLUGIN_JSON_FILENAME = 'plugin.json'; |
| 28 | |
| 29 | /** |
| 30 | * The name of the plugin whose metadata will be loaded. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $pluginName; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. |
| 38 | * |
| 39 | * @param string $pluginName Name of the plugin to load metadata. |
| 40 | */ |
| 41 | public function __construct($pluginName) |
| 42 | { |
| 43 | $this->pluginName = $pluginName; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Loads plugin metadata. @see Plugin::getInformation. |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | public function load() |
| 52 | { |
| 53 | $defaults = $this->getDefaultPluginInformation(); |
| 54 | $plugin = $this->loadPluginInfoJson(); |
| 55 | |
| 56 | // use translated plugin description if available |
| 57 | if ($defaults['description'] != Piwik::translate($defaults['description'])) { |
| 58 | unset($plugin['description']); |
| 59 | } |
| 60 | |
| 61 | // look for a license file |
| 62 | $licenseFile = $this->getPathToLicenseFile(); |
| 63 | if(!empty($licenseFile)) { |
| 64 | $plugin['license_file'] = $licenseFile; |
| 65 | } |
| 66 | |
| 67 | return array_merge( |
| 68 | $defaults, |
| 69 | $plugin |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | public function hasPluginJson() |
| 74 | { |
| 75 | $hasJson = $this->loadPluginInfoJson(); |
| 76 | |
| 77 | return !empty($hasJson); |
| 78 | } |
| 79 | |
| 80 | private function getDefaultPluginInformation() |
| 81 | { |
| 82 | $descriptionKey = $this->pluginName . '_PluginDescription'; |
| 83 | return array( |
| 84 | 'description' => $descriptionKey, |
| 85 | 'homepage' => 'https://matomo.org/', |
| 86 | 'authors' => array(array('name' => 'Matomo', 'homepage' => 'https://matomo.org/')), |
| 87 | 'license' => 'GPL v3+', |
| 88 | 'version' => Version::VERSION, |
| 89 | 'theme' => false, |
| 90 | 'require' => array() |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * It is important that this method works without using anything from DI |
| 96 | * @return array|mixed |
| 97 | */ |
| 98 | public function loadPluginInfoJson() |
| 99 | { |
| 100 | $path = $this->getPathToPluginJson(); |
| 101 | return $this->loadJsonMetadata($path); |
| 102 | } |
| 103 | |
| 104 | public function getPathToPluginJson() |
| 105 | { |
| 106 | $path = $this->getPathToPluginFolder() . '/' . self::PLUGIN_JSON_FILENAME; |
| 107 | return $path; |
| 108 | } |
| 109 | |
| 110 | private function loadJsonMetadata($path) |
| 111 | { |
| 112 | if (!file_exists($path)) { |
| 113 | return array(); |
| 114 | } |
| 115 | |
| 116 | $json = file_get_contents($path); |
| 117 | if (!$json) { |
| 118 | return array(); |
| 119 | } |
| 120 | |
| 121 | $info = json_decode($json, $assoc = true); |
| 122 | if (!is_array($info) |
| 123 | || empty($info) |
| 124 | ) { |
| 125 | throw new Exception("Invalid JSON file: $path"); |
| 126 | } |
| 127 | |
| 128 | return $info; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return string |
| 133 | */ |
| 134 | private function getPathToPluginFolder() |
| 135 | { |
| 136 | return \Piwik\Plugin\Manager::getPluginDirectory($this->pluginName); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return null|string |
| 141 | */ |
| 142 | public function getPathToLicenseFile() |
| 143 | { |
| 144 | $prefixPath = $this->getPathToPluginFolder() . '/'; |
| 145 | $licenseFiles = array( |
| 146 | 'LICENSE', |
| 147 | 'LICENSE.md', |
| 148 | 'LICENSE.txt' |
| 149 | ); |
| 150 | foreach ($licenseFiles as $licenseFile) { |
| 151 | $pathToLicense = $prefixPath . $licenseFile; |
| 152 | if (is_file($pathToLicense) && is_readable($pathToLicense)) { |
| 153 | return $pathToLicense; |
| 154 | } |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | } |
| 159 |