blob: 208f24d4e6c412fe5097e5a7b274503869f614ab [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3//http://www.leaseweblabs.com/2014/04/psr-0-psr-4-autoloading-classes-php/
4class Loader
5{
6 protected static $parentPath = null;
7 protected static $paths = null;
8 protected static $files = null;
9 protected static $nsChar = '\\';
10 protected static $initialized = false;
11
12 protected static function initialize()
13 {
14 if (static::$initialized) return;
15 static::$initialized = true;
16 static::$parentPath = __FILE__;
17 for ($i=substr_count(get_class(), static::$nsChar);$i>=0;$i--) {
18 static::$parentPath = dirname(static::$parentPath);
19 }
20 static::$paths = array();
21 static::$files = array(__FILE__);
22 }
23
24 public static function register($path,$namespace) {
25 if (!static::$initialized) static::initialize();
26 static::$paths[$namespace] = trim($path,DIRECTORY_SEPARATOR);
27 }
28
29 public static function load($class) {
30 if (class_exists($class,false)) return;
31 if (!static::$initialized) static::initialize();
32
33 foreach (static::$paths as $namespace => $path) {
34 if (!$namespace || $namespace.static::$nsChar === substr($class, 0, strlen($namespace.static::$nsChar))) {
35
36 $fileName = substr($class,strlen($namespace.static::$nsChar)-1);
37 $fileName = str_replace(static::$nsChar, DIRECTORY_SEPARATOR, ltrim($fileName,static::$nsChar));
38 $fileName = static::$parentPath.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$fileName.'.php';
39
40 if (file_exists($fileName)) {
41 include $fileName;
42 return true;
43 }
44 }
45 }
46 return false;
47 }
48}
49
50spl_autoload_register(array('Loader', 'load'));