4 * Load all internal libraries and setup class autoloader
6 * @author Andreas Gohr <andi@splitbrain.org>
11 use dokuwiki\Extension\PluginController
;
14 /** @var string[] Common libraries that are always loaded */
15 protected array $commonLibs = [
43 /** @var string[] Classname to file mappings */
44 protected array $fixedClassNames = [
45 'Diff' => 'DifferenceEngine.php',
46 'UnifiedDiffFormatter' => 'DifferenceEngine.php',
47 'TableDiffFormatter' => 'DifferenceEngine.php',
48 'cache' => 'cache.php',
49 'cache_parser' => 'cache.php',
50 'cache_instructions' => 'cache.php',
51 'cache_renderer' => 'cache.php',
52 'Input' => 'Input.class.php',
53 'JpegMeta' => 'JpegMeta.php',
54 'SimplePie' => 'SimplePie.php',
55 'FeedParser' => 'FeedParser.php',
56 'SafeFN' => 'SafeFN.class.php',
57 'Mailer' => 'Mailer.class.php',
58 'Doku_Handler' => 'parser/handler.php',
59 'Doku_Renderer' => 'parser/renderer.php',
60 'Doku_Renderer_xhtml' => 'parser/xhtml.php',
61 'Doku_Renderer_code' => 'parser/code.php',
62 'Doku_Renderer_xhtmlsummary' => 'parser/xhtmlsummary.php',
63 'Doku_Renderer_metadata' => 'parser/metadata.php'
67 * Load common libs and register autoloader
69 public function __construct()
71 require_once(DOKU_INC
. 'vendor/autoload.php');
72 spl_autoload_register([$this, 'autoload']);
73 $this->loadCommonLibs();
77 * require all the common libraries
81 public function loadCommonLibs()
83 foreach ($this->commonLibs
as $lib) {
84 require_once(DOKU_INC
. 'inc/' . $lib);
90 * spl_autoload_register callback
92 * @param string $className
95 public function autoload($className)
97 // namespace to directory conversion
98 $classPath = str_replace('\\', '/', $className);
100 return $this->autoloadFixedClass($className)
101 ||
$this->autoloadTestMockClass($classPath)
102 ||
$this->autoloadTestClass($classPath)
103 ||
$this->autoloadPluginClass($classPath)
104 ||
$this->autoloadTemplateClass($classPath)
105 ||
$this->autoloadCoreClass($classPath)
106 ||
$this->autoloadNamedPluginClass($className);
110 * Check if the class is one of the fixed names
112 * @param string $className
113 * @return bool true if the class was loaded, false otherwise
115 protected function autoloadFixedClass($className)
117 if (isset($this->fixedClassNames
[$className])) {
118 require($this->fixedClassNames
[$className]);
125 * Check if the class is a test mock class
127 * @param string $classPath The class name using forward slashes as namespace separators
128 * @return bool true if the class was loaded, false otherwise
130 protected function autoloadTestMockClass($classPath)
132 if ($this->prefixStrip($classPath, 'dokuwiki/test/mock/')) {
133 $file = DOKU_INC
. '_test/mock/' . $classPath . '.php';
134 if (file_exists($file)) {
143 * Check if the class is a test mock class
145 * @param string $classPath The class name using forward slashes as namespace separators
146 * @return bool true if the class was loaded, false otherwise
148 protected function autoloadTestClass($classPath)
150 if ($this->prefixStrip($classPath, 'dokuwiki/test/')) {
151 $file = DOKU_INC
. '_test/tests/' . $classPath . '.php';
152 if (file_exists($file)) {
161 * Check if the class is a namespaced plugin class
163 * @param string $classPath The class name using forward slashes as namespace separators
164 * @return bool true if the class was loaded, false otherwise
166 protected function autoloadPluginClass($classPath)
168 global $plugin_controller;
170 if ($this->prefixStrip($classPath, 'dokuwiki/plugin/')) {
171 $classPath = str_replace('/test/', '/_test/', $classPath); // no underscore in test namespace
172 $file = DOKU_PLUGIN
. $classPath . '.php';
173 if (file_exists($file)) {
174 $plugin = substr($classPath, 0, strpos($classPath, '/'));
175 // don't load disabled plugin classes (only if plugin controller is available)
176 if (!defined('DOKU_UNITTEST') && $plugin_controller && plugin_isdisabled($plugin)) return false;
180 } catch (\Throwable
$e) {
181 ErrorHandler
::showExceptionMsg($e, "Error loading plugin $plugin");
190 * Check if the class is a namespaced template class
192 * @param string $classPath The class name using forward slashes as namespace separators
193 * @return bool true if the class was loaded, false otherwise
195 protected function autoloadTemplateClass($classPath)
197 // template namespace
198 if ($this->prefixStrip($classPath, 'dokuwiki/template/')) {
199 $classPath = str_replace('/test/', '/_test/', $classPath); // no underscore in test namespace
200 $file = DOKU_INC
. 'lib/tpl/' . $classPath . '.php';
201 if (file_exists($file)) {
202 $template = substr($classPath, 0, strpos($classPath, '/'));
206 } catch (\Throwable
$e) {
207 ErrorHandler
::showExceptionMsg($e, "Error loading template $template");
216 * Check if the class is a namespaced DokuWiki core class
218 * @param string $classPath The class name using forward slashes as namespace separators
219 * @return bool true if the class was loaded, false otherwise
221 protected function autoloadCoreClass($classPath)
223 if ($this->prefixStrip($classPath, 'dokuwiki/')) {
224 $file = DOKU_INC
. 'inc/' . $classPath . '.php';
225 if (file_exists($file)) {
234 * Check if the class is a un-namespaced plugin class following our naming scheme
236 * @param string $className
237 * @return bool true if the class was loaded, false otherwise
239 protected function autoloadNamedPluginClass($className)
241 global $plugin_controller;
245 '/^(' . implode('|', PluginController
::PLUGIN_TYPES
) . ')_plugin_(' .
246 DOKU_PLUGIN_NAME_REGEX
.
252 $c = ((count($m) === 4) ?
"/{$m[3]}" : '');
253 $plg = DOKU_PLUGIN
. "{$m[2]}/{$m[1]}$c.php";
254 if (file_exists($plg)) {
255 // don't load disabled plugin classes (only if plugin controller is available)
256 if (!defined('DOKU_UNITTEST') && $plugin_controller && plugin_isdisabled($m[2])) return false;
259 } catch (\Throwable
$e) {
260 ErrorHandler
::showExceptionMsg($e, "Error loading plugin {$m[2]}");
269 * Check if the given string starts with the given prefix and strip it
271 * @param string $string
272 * @param string $prefix
273 * @return bool true if the prefix was found and stripped, false otherwise
275 protected function prefixStrip(&$string, $prefix)
277 if (str_starts_with($string, $prefix)) {
278 $string = substr($string, strlen($prefix));