Passed
Pull Request — master (#117)
by Spuds
07:11
created

SplClassLoader::loadClass()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 7
eloc 11
c 2
b 0
f 1
nc 13
nop 1
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
/**
22
 * SplClassLoader implementation that implements the technical interoperability
23
 * standards for PHP 5.3 namespaces and class names.
24
 *
25
 * http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
26
 *
27
 *     // Example which loads classes for the Doctrine Common package in the
28
 *     // Doctrine\Common namespace.
29
 *     $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
30
 *     $classLoader->register();
31
 *
32
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
33
 * @author Jonathan H. Wage <[email protected]>
34
 * @author Roman S. Borschel <[email protected]>
35
 * @author Matthew Weier O'Phinney <[email protected]>
36
 * @author Kris Wallsmith <[email protected]>
37
 * @author Fabien Potencier <[email protected]>
38
 */
39
class SplClassLoader
40
{
41
    private $_fileExtension = '.php';
42
    private $_namespace;
43
    private $_includePath;
44
    private $_namespaceSeparator = '\\';
45
46
    /**
47
     * Creates a new <tt>SplClassLoader</tt> that loads classes of the
48
     * specified namespace.
49
     *
50
     * @param string $ns The namespace to use.
51
     */
52
    public function __construct($ns = null, $includePath = null)
53
    {
54
        $this->_namespace = $ns;
55
        $this->_includePath = $includePath;
56
    }
57
58
    /**
59
     * Sets the namespace separator used by classes in the namespace of this class loader.
60
     *
61
     * @param string $sep The separator to use.
62
     */
63
    public function setNamespaceSeparator($sep)
64
    {
65
        $this->_namespaceSeparator = $sep;
66
    }
67
68
    /**
69
     * Gets the namespace seperator used by classes in the namespace of this class loader.
70
     *
71
     * @return void
72
     */
73
    public function getNamespaceSeparator()
74
    {
75
        return $this->_namespaceSeparator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_namespaceSeparator returns the type string which is incompatible with the documented return type void.
Loading history...
76
    }
77
78
    /**
79
     * Sets the base include path for all class files in the namespace of this class loader.
80
     *
81
     * @param string $includePath
82
     */
83
    public function setIncludePath($includePath)
84
    {
85
        $this->_includePath = $includePath;
86
    }
87
88
    /**
89
     * Gets the base include path for all class files in the namespace of this class loader.
90
     *
91
     * @return string $includePath
92
     */
93
    public function getIncludePath()
94
    {
95
        return $this->_includePath;
96
    }
97
98
    /**
99
     * Sets the file extension of class files in the namespace of this class loader.
100
     *
101
     * @param string $fileExtension
102
     */
103
    public function setFileExtension($fileExtension)
104
    {
105
        $this->_fileExtension = $fileExtension;
106
    }
107
108
    /**
109
     * Gets the file extension of class files in the namespace of this class loader.
110
     *
111
     * @return string $fileExtension
112
     */
113
    public function getFileExtension()
114
    {
115
        return $this->_fileExtension;
116
    }
117
118
    /**
119
     * Installs this class loader on the SPL autoload stack.
120
     */
121
    public function register()
122
    {
123
        spl_autoload_register(array($this, 'loadClass'));
124
    }
125
126
    /**
127
     * Uninstalls this class loader from the SPL autoloader stack.
128
     */
129
    public function unregister()
130
    {
131
        spl_autoload_unregister(array($this, 'loadClass'));
132
    }
133
134
    /**
135
     * Loads the given class or interface.
136
     *
137
     * @param string $className The name of the class to load.
138
     * @return void
139
     */
140 6
    public function loadClass($className)
141
    {
142 6
        if (null === $this->_namespace || strpos($className, $this->_namespace . $this->_namespaceSeparator) === 0) {
143 6
            $fileName = '';
144 6
            $namespace = '';
145 6
            if (false !== ($lastNsPos = strrpos($className, $this->_namespaceSeparator))) {
146 6
                $namespace = substr($className, 0, $lastNsPos);
147 6
                $className = substr($className, $lastNsPos + 1);
148 6
                $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
149
            }
150 6
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
151
152 6
            $full_path = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
153 6
            if (file_exists($full_path))
154
			{
155 6
				require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
156
			}
157
        }
158
    }
159
}