SplClassLoader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

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