Passed
Push — master ( 0b7aa3...1e9d3c )
by Chris
08:25
created

Autoloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A autoload() 0 8 3
A register() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace PHPInsight;
4
5
class Autoloader
6
{
7
    private $directory;
8
    private $prefix;
9
    private $prefixLength;
10
11
    /**
12
     * @param string $baseDirectory Base directory where the source files are located.
13
     */
14
    public function __construct($baseDirectory = __DIR__)
15
    {
16
        $this->directory = $baseDirectory;
17
        $this->prefix = __NAMESPACE__ . '\\';
18
        $this->prefixLength = strlen($this->prefix);
19
    }
20
21
    /**
22
     * Registers the autoloader class with the PHP SPL autoloader.
23
     *
24
     * @param bool $prepend Prepend the autoloader on the stack instead of appending it.
25
     */
26
    public static function register($prepend = false)
27
    {
28
        spl_autoload_register(array(new self, 'autoload'), true, $prepend);
29
    }
30
31
    /**
32
     * Loads a class from a file using its fully qualified name.
33
     *
34
     * @param string $className Fully qualified name of a class.
35
     */
36
    public function autoload($className)
37
    {
38
        if (0 === strpos($className, $this->prefix)) {
39
            $parts = explode('\\', substr($className, $this->prefixLength));
40
            $filepath = $this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php';
41
42
            if (is_file($filepath)) {
43
                require($filepath);
44
            }
45
        }
46
    }
47
}
48