Completed
Pull Request — master (#572)
by Frédéric
01:32
created

AutoLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 20 3
A register() 0 4 1
A unregister() 0 4 1
1
<?php
2
3
namespace OAuth\Common;
4
5
/**
6
 * PSR-0 Autoloader
7
 *
8
 * @author ieter Hordijk <[email protected]>
9
 */
10
class AutoLoader
11
{
12
    /**
13
     * @var string The namespace prefix for this instance.
14
     */
15
    protected $namespace = '';
16
17
    /**
18
     * @var string The filesystem prefix to use for this instance
19
     */
20
    protected $path = '';
21
22
    /**
23
     * Build the instance of the autoloader
24
     *
25
     * @param string $namespace The prefixed namespace this instance will load
26
     * @param string $path      The filesystem path to the root of the namespace
27
     */
28
    public function __construct($namespace, $path)
29
    {
30
        $this->namespace = ltrim($namespace, '\\');
31
        $this->path      = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
32
    }
33
34
    /**
35
     * Try to load a class
36
     *
37
     * @param string $class The class name to load
38
     *
39
     * @return boolean If the loading was successful
40
     */
41
    public function load($class)
42
    {
43
        $class = ltrim($class, '\\');
44
45
        if (strpos($class, $this->namespace) === 0) {
46
            $nsparts   = explode('\\', $class);
47
            $class     = array_pop($nsparts);
48
            $nsparts[] = '';
49
            $path      = $this->path . implode(DIRECTORY_SEPARATOR, $nsparts);
50
            $path     .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
51
52
            if (file_exists($path)) {
53
                require $path;
54
55
                return true;
56
            }
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * Register the autoloader to PHP
64
     *
65
     * @return boolean The status of the registration
66
     */
67
    public function register()
68
    {
69
        return spl_autoload_register(array($this, 'load'));
70
    }
71
72
    /**
73
     * Unregister the autoloader to PHP
74
     *
75
     * @return boolean The status of the unregistration
76
     */
77
    public function unregister()
78
    {
79
        return spl_autoload_unregister(array($this, 'load'));
80
    }
81
}
82