Piwik\Plugin::findComponent PHP Метод

findComponent() публичный Метод

Tries to find a component such as a Menu or Tasks within this plugin.
public findComponent ( string $componentName, string $expectedSubclass ) : string | null
$componentName string The name of the component you want to look for. In case you request a component named 'Menu' it'll look for a file named 'Menu.php' within the root of the plugin folder that implements a class named Piwik\Plugin\$PluginName\Menu . If such a file exists but does not implement this class it'll silently ignored.
$expectedSubclass string If not empty, a check will be performed whether a found file extends the given subclass. If the requested file exists but does not extend this class a warning will be shown to advice a developer to extend this certain class.
Результат string | null Null if the requested component does not exist or an instance of the found component.
    public function findComponent($componentName, $expectedSubclass)
    {
        $this->createCacheIfNeeded();
        $cacheId = 'Plugin' . $this->pluginName . $componentName . $expectedSubclass;
        $componentFile = sprintf('%s/plugins/%s/%s.php', PIWIK_INCLUDE_PATH, $this->pluginName, $componentName);
        if ($this->cache->contains($cacheId)) {
            $classname = $this->cache->fetch($cacheId);
            if (empty($classname)) {
                return null;
                // might by "false" in case has no menu, widget, ...
            }
            if (file_exists($componentFile)) {
                include_once $componentFile;
            }
        } else {
            $this->cache->save($cacheId, false);
            // prevent from trying to load over and over again for instance if there is no Menu for a plugin
            if (!file_exists($componentFile)) {
                return null;
            }
            require_once $componentFile;
            $classname = sprintf('Piwik\\Plugins\\%s\\%s', $this->pluginName, $componentName);
            if (!class_exists($classname)) {
                return null;
            }
            if (!empty($expectedSubclass) && !is_subclass_of($classname, $expectedSubclass)) {
                Log::warning(sprintf('Cannot use component %s for plugin %s, class %s does not extend %s', $componentName, $this->pluginName, $classname, $expectedSubclass));
                return null;
            }
            $this->cache->save($cacheId, $classname);
        }
        return $classname;
    }