| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugin; |
|
|
| use Exception; |
| use Piwik\Piwik; |
| use Piwik\Url; |
| use Piwik\Version; |
|
|
| |
| |
| |
| require_once PIWIK_INCLUDE_PATH . '/core/Version.php'; |
|
|
| |
| |
| |
| |
| class MetadataLoader |
| { |
| public const PLUGIN_JSON_FILENAME = 'plugin.json'; |
|
|
| |
| |
| |
| |
| |
| private $pluginName; |
|
|
| |
| |
| |
| public function __construct($pluginName) |
| { |
| $this->pluginName = $pluginName; |
| } |
|
|
| |
| |
| |
| |
| |
| public function load() |
| { |
| $defaults = $this->getDefaultPluginInformation(); |
| $plugin = $this->loadPluginInfoJson(); |
|
|
| |
| if ($defaults['description'] != Piwik::translate($defaults['description'])) { |
| unset($plugin['description']); |
| } |
|
|
| |
| $licenseFile = $this->getPathToLicenseFile(); |
| if (!empty($licenseFile)) { |
| $plugin['license_file'] = $licenseFile; |
| } |
|
|
| return array_merge( |
| $defaults, |
| $plugin |
| ); |
| } |
|
|
| public function hasPluginJson() |
| { |
| $hasJson = $this->loadPluginInfoJson(); |
|
|
| return !empty($hasJson); |
| } |
|
|
| private function getDefaultPluginInformation() |
| { |
| $descriptionKey = $this->pluginName . '_PluginDescription'; |
| 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' => [], |
| ]; |
| } |
|
|
| |
| |
| |
| |
| public function loadPluginInfoJson() |
| { |
| $path = $this->getPathToPluginJson(); |
| return $this->loadJsonMetadata($path); |
| } |
|
|
| public function getPathToPluginJson() |
| { |
| $path = $this->getPathToPluginFolder() . '/' . self::PLUGIN_JSON_FILENAME; |
| return $path; |
| } |
|
|
| private function loadJsonMetadata($path) |
| { |
| if (!file_exists($path)) { |
| return array(); |
| } |
|
|
| $json = file_get_contents($path); |
| if (!$json) { |
| return array(); |
| } |
|
|
| $info = json_decode($json, $assoc = true); |
| if ( |
| !is_array($info) |
| || empty($info) |
| ) { |
| throw new Exception("Invalid JSON file: $path"); |
| } |
|
|
| return $info; |
| } |
|
|
| |
| |
| |
| private function getPathToPluginFolder() |
| { |
| return \Piwik\Plugin\Manager::getPluginDirectory($this->pluginName); |
| } |
|
|
| |
| |
| |
| public function getPathToLicenseFile() |
| { |
| $prefixPath = $this->getPathToPluginFolder() . '/'; |
| $licenseFiles = array( |
| 'LICENSE', |
| 'LICENSE.md', |
| 'LICENSE.txt', |
| ); |
| foreach ($licenseFiles as $licenseFile) { |
| $pathToLicense = $prefixPath . $licenseFile; |
| if (is_file($pathToLicense) && is_readable($pathToLicense)) { |
| return $pathToLicense; |
| } |
| } |
| return null; |
| } |
| } |
|
|