Test Failed
Push — trunk ( db071f...8d464a )
by SuperNova.WS
06:33
created

Autoloader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B autoloader() 0 12 5
A register() 0 18 4
A registerRelative() 0 3 1
A reset() 0 3 1
1
<?php
2
/**
3
 * Created by Gorlum 18.06.2017 13:24
4
 */
5
6
namespace Core;
7
8
9
class Autoloader {
10
11
  /**
12
   * @var string[] $folders
13
   */
14
  protected static $folders = [];
15
16
  protected static $autoloaderRegistered = false;
17
18
  /**
19
   * @param string $class - Fully-qualified path with namespaces
20
   */
21
  public static function autoloader($class) {
22
    $classFile = str_replace('\\', '/', $class);
23
    foreach(static::$folders as $folder) {
24
      $classFullFileName = str_replace('\\', '/', $folder . $classFile) . DOT_PHP_EX;
25
      if(file_exists($classFullFileName) && is_file($classFullFileName)) {
26
        require_once($classFullFileName);
27
        if(method_exists($class, '_constructorStatic')) {
28
          $class::_constructorStatic();
29
        }
30
      }
31
    }
32
  }
33
34
  /**
35
   * @param string $absoluteClassRoot - absolute path to root class folder
36
   */
37
  public static function register($absoluteClassRoot) {
38
    if(!static::$autoloaderRegistered) {
39
      spl_autoload_register(array(__CLASS__, 'autoloader'));
40
      static::$autoloaderRegistered = true;
41
    }
42
43
    $absoluteClassRoot = str_replace('\\', '/', $absoluteClassRoot);
44
45
    if(!($absoluteClassRoot = realpath($absoluteClassRoot))) {
46
      return;
47
    }
48
49
    $absoluteClassRoot = str_replace('\\', '/', $absoluteClassRoot) . '/';
50
51
    if(!isset(static::$folders[$absoluteClassRoot])) {
52
      static::$folders[$absoluteClassRoot] = $absoluteClassRoot;
53
    }
54
  }
55
56
  /**
57
   * @param string $relativeClassRoot - relative path to root class folder from game root (where index.php lies)
58
   */
59
  public static function registerRelative($relativeClassRoot) {
60
    static::register(SN_ROOT_PHYSICAL . $relativeClassRoot);
61
  }
62
63
  public static function reset() {
64
    static::$folders = [];
65
  }
66
67
}
68