Passed
Branch wip_sessions (2e0cc8)
by Nils
04:59
created

SplClassLoader::loadClass()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 13
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SplClassLoader implementation that implements the technical interoperability
7
 * standards for PHP 5.3 namespaces and class names.
8
 *
9
 * http://groups.google.com/group/php-standards/web/final-proposal
10
 *
11
 *     // Example which loads classes for the Doctrine Common package in the
12
 *     // Doctrine\Common namespace.
13
 *     $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
14
 *     $classLoader->register();
15
 *
16
 * @author Jonathan H. Wage <[email protected]>
17
 * @author Roman S. Borschel <[email protected]>
18
 * @author Matthew Weier O'Phinney <[email protected]>
19
 * @author Kris Wallsmith <[email protected]>
20
 * @author Fabien Potencier <[email protected]>
21
 */
22
23
24
class SplClassLoader
25
{
26
    private $_fileExtension = '.php';
27
    private $_namespace;
28
    private $_includePath;
29
    private $_namespaceSeparator = '\\';
30
31
    /**
32
     * Creates a new <tt>SplClassLoader</tt> that loads classes of the
33
     * specified namespace.
34
     *
35
     * @param string $namespace The namespace to use.
36
     */
37
    public function __construct($namespace = null, $includePath = null)
38
    {
39
        $this->_namespace = $namespace;
40
        $this->_includePath = $includePath;
41
    }
42
43
    /**
44
     * Sets the namespace separator used by classes in the namespace of this class loader.
45
     *
46
     * @param string $sep The separator to use.
47
     */
48
    public function setNamespaceSeparator($sep)
49
    {
50
        $this->_namespaceSeparator = $sep;
51
    }
52
53
    /**
54
     * Gets the namespace seperator used by classes in the namespace of this class loader.
55
     *
56
     * @return string
57
     */
58
    public function getNamespaceSeparator()
59
    {
60
        return $this->_namespaceSeparator;
61
    }
62
63
    /**
64
     * Sets the base include path for all class files in the namespace of this class loader.
65
     *
66
     * @param string $includePath
67
     */
68
    public function setIncludePath($includePath)
69
    {
70
        $this->_includePath = $includePath;
71
    }
72
73
    /**
74
     * Gets the base include path for all class files in the namespace of this class loader.
75
     *
76
     * @return string $includePath
77
     */
78
    public function getIncludePath()
79
    {
80
        return $this->_includePath;
81
    }
82
83
    /**
84
     * Sets the file extension of class files in the namespace of this class loader.
85
     *
86
     * @param string $fileExtension
87
     */
88
    public function setFileExtension($fileExtension)
89
    {
90
        $this->_fileExtension = $fileExtension;
91
    }
92
93
    /**
94
     * Gets the file extension of class files in the namespace of this class loader.
95
     *
96
     * @return string $fileExtension
97
     */
98
    public function getFileExtension()
99
    {
100
        return $this->_fileExtension;
101
    }
102
103
    /**
104
     * Installs this class loader on the SPL autoload stack.
105
     */
106
    public function register()
107
    {
108
        spl_autoload_register(array($this, 'loadClass'));
109
    }
110
111
    /**
112
     * Uninstalls this class loader from the SPL autoloader stack.
113
     */
114
    public function unregister()
115
    {
116
        spl_autoload_unregister(array($this, 'loadClass'));
117
    }
118
119
    /**
120
     * Loads the given class or interface.
121
     *
122
     * @param  string $className The name of the class to load.
123
     * @return void
124
     */
125
    public function loadClass($className)
126
    {
127
        if (null === $this->_namespace || $this->_namespace === substr($className, 0, strlen($this->_namespace))) {
128
            $fileName = '';
129
            $namespace = '';
130
            if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
131
                $namespace = substr($className, 0, $lastNsPos);
132
                $className = substr($className, $lastNsPos + 1);
133
                $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
134
            }
135
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).$this->_fileExtension;
136
137
            require ($this->_includePath !== null ? $this->_includePath.DIRECTORY_SEPARATOR : '').$fileName;
138
        }
139
    }
140
}
141