Skip to content
Extraits de code Groupes Projets
MetadataLoader.php 3,74 ko
Newer Older
  • Learn to ignore specific revisions
  •  * Piwik - free/libre analytics platform
    
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     *
     */
    
    Benaka Moorthi's avatar
    Benaka Moorthi a validé
    /**
     * @see core/Version.php
     */
    require_once PIWIK_INCLUDE_PATH . '/core/Version.php';
    
    
    /**
     * Loads plugin metadata found in the following files:
    
        const PLUGIN_JSON_FILENAME = 'plugin.json';
    
        /**
         * The name of the plugin whose metadata will be loaded.
    
         * @param string $pluginName Name of the plugin to load metadata.
         */
        public function __construct($pluginName)
        {
            $this->pluginName = $pluginName;
        }
    
        /**
    
         * Loads plugin metadata. @see Plugin::getInformation.
    
            $defaults = $this->getDefaultPluginInformation();
            $plugin   = $this->loadPluginInfoJson();
    
            // use translated plugin description if available
            if ($defaults['description'] != Piwik::translate($defaults['description'])) {
                unset($plugin['description']);
            }
    
    
            // look for a license file
            $licenseFile = $this->getPathToLicenseFile();
            if(!empty($licenseFile)) {
                $plugin['license_file'] = $licenseFile;
            }
    
    
        public function hasPluginJson()
        {
            $hasJson = $this->loadPluginInfoJson();
    
            return !empty($hasJson);
        }
    
    
        private function getDefaultPluginInformation()
        {
            $descriptionKey = $this->pluginName . '_PluginDescription';
            return array(
    
                'description'      => $descriptionKey,
    
                'authors'          => array(array('name' => 'Piwik', 'homepage'  => 'http://piwik.org/')),
    
        /**
         * It is important that this method works without using anything from DI
         * @return array|mixed
         */
        public function loadPluginInfoJson()
    
        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 string
         */
        private function getPathToPluginFolder()
        {
            return \Piwik\Plugin\Manager::getPluginsDirectory() . $this->pluginName;
        }
    
        /**
         * @return null|string
         */
        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;
        }