xref: /dokuwiki/lib/plugins/extension/Local.php (revision 6a8e48eda246f872402bf5b85763f276cd4c319d)
17c9966a5SAndreas Gohr<?php
27c9966a5SAndreas Gohr
37c9966a5SAndreas Gohrnamespace dokuwiki\plugin\extension;
47c9966a5SAndreas Gohr
5*6a8e48edSAndreas Gohruse dokuwiki\Logger;
6*6a8e48edSAndreas Gohruse RuntimeException;
7*6a8e48edSAndreas Gohr
87c9966a5SAndreas Gohrclass Local
97c9966a5SAndreas Gohr{
107c9966a5SAndreas Gohr    /**
117c9966a5SAndreas Gohr     * Glob the given directory and init each subdirectory as an Extension
127c9966a5SAndreas Gohr     *
13*6a8e48edSAndreas Gohr     * Directories that cannot be initialized as an extension (e.g. leftover
14*6a8e48edSAndreas Gohr     * directories with an invalid base name like "myplugin (copy)") are skipped
15*6a8e48edSAndreas Gohr     * so a single stray directory does not break the whole listing.
16*6a8e48edSAndreas Gohr     *
177c9966a5SAndreas Gohr     * @param string $directory
187c9966a5SAndreas Gohr     * @return Extension[]
197c9966a5SAndreas Gohr     */
207c9966a5SAndreas Gohr    protected function readExtensionsFromDirectory($directory)
217c9966a5SAndreas Gohr    {
227c9966a5SAndreas Gohr        $extensions = [];
237c9966a5SAndreas Gohr        $directory = rtrim($directory, '/');
247c9966a5SAndreas Gohr        $dirs = glob($directory . '/*', GLOB_ONLYDIR);
257c9966a5SAndreas Gohr        foreach ($dirs as $dir) {
26*6a8e48edSAndreas Gohr            try {
277c9966a5SAndreas Gohr                $ext = Extension::createFromDirectory($dir);
28*6a8e48edSAndreas Gohr            } catch (RuntimeException $e) {
29*6a8e48edSAndreas Gohr                Logger::debug('Skipping invalid extension directory: ' . $dir, $e->getMessage());
30*6a8e48edSAndreas Gohr                continue;
31*6a8e48edSAndreas Gohr            }
327c9966a5SAndreas Gohr            $extensions[$ext->getId()] = $ext;
337c9966a5SAndreas Gohr        }
347c9966a5SAndreas Gohr        return $extensions;
357c9966a5SAndreas Gohr    }
367c9966a5SAndreas Gohr
377c9966a5SAndreas Gohr    /**
387c9966a5SAndreas Gohr     * Get all locally installed templates
397c9966a5SAndreas Gohr     *
407c9966a5SAndreas Gohr     * @return Extension[]
417c9966a5SAndreas Gohr     */
427c9966a5SAndreas Gohr    public function getTemplates()
437c9966a5SAndreas Gohr    {
44176901c2SAndreas Gohr        $templates = $this->readExtensionsFromDirectory(DOKU_INC . 'lib/tpl/');
457c9966a5SAndreas Gohr        ksort($templates);
467c9966a5SAndreas Gohr        return $templates;
477c9966a5SAndreas Gohr    }
487c9966a5SAndreas Gohr
497c9966a5SAndreas Gohr    /**
507c9966a5SAndreas Gohr     * Get all locally installed plugins
517c9966a5SAndreas Gohr     *
527c9966a5SAndreas Gohr     * Note this skips the PluginController and just iterates over the plugin dir,
537c9966a5SAndreas Gohr     * it's basically the same as what the PluginController does, but allows us to
547c9966a5SAndreas Gohr     * directly initialize Extension objects.
557c9966a5SAndreas Gohr     *
567c9966a5SAndreas Gohr     * @return Extension[]
577c9966a5SAndreas Gohr     */
587c9966a5SAndreas Gohr    public function getPlugins()
597c9966a5SAndreas Gohr    {
607c9966a5SAndreas Gohr        $plugins = $this->readExtensionsFromDirectory(DOKU_PLUGIN);
617c9966a5SAndreas Gohr        ksort($plugins);
627c9966a5SAndreas Gohr        return $plugins;
637c9966a5SAndreas Gohr    }
647c9966a5SAndreas Gohr
657c9966a5SAndreas Gohr    /**
667c9966a5SAndreas Gohr     * Get all locally installed extensions
677c9966a5SAndreas Gohr     *
687c9966a5SAndreas Gohr     * @return Extension[]
697c9966a5SAndreas Gohr     */
707c9966a5SAndreas Gohr    public function getExtensions()
717c9966a5SAndreas Gohr    {
727c9966a5SAndreas Gohr        return array_merge($this->getPlugins(), $this->getTemplates());
737c9966a5SAndreas Gohr    }
747c9966a5SAndreas Gohr}
75