Passed
Branch development (e0e718)
by Nils
04:45
created

SplClassLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setIncludePath() 0 3 1
B loadClass() 0 13 5
A unregister() 0 3 1
A setNamespaceSeparator() 0 3 1
A setFileExtension() 0 3 1
A getNamespaceSeparator() 0 3 1
A __construct() 0 4 1
A getIncludePath() 0 3 1
A register() 0 3 1
A getFileExtension() 0 3 1
1
<?php
2
3
/**
4
 * SplClassLoader implementation that implements the technical interoperability
5
 * standards for PHP 5.3 namespaces and class names.
6
 *
7
 * http://groups.google.com/group/php-standards/web/final-proposal
8
 *
9
 *     // Example which loads classes for the Doctrine Common package in the
10
 *     // Doctrine\Common namespace.
11
 *     $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
12
 *     $classLoader->register();
13
 *
14
 * @author Jonathan H. Wage <[email protected]>
15
 * @author Roman S. Borschel <[email protected]>
16
 * @author Matthew Weier O'Phinney <[email protected]>
17
 * @author Kris Wallsmith <[email protected]>
18
 * @author Fabien Potencier <[email protected]>
19
 */
20
class SplClassLoader
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
21
{
22
    private $_fileExtension = '.php';
23
    private $_namespace;
24
    private $_includePath;
25
    private $_namespaceSeparator = '\\';
26
27
    /**
28
     * Creates a new <tt>SplClassLoader</tt> that loads classes of the
29
     * specified namespace.
30
     *
31
     * @param string $namespace The namespace to use.
32
     */
33
    public function __construct($namespace = null, $includePath = null)
34
    {
35
        $this->_namespace = $namespace;
36
        $this->_includePath = $includePath;
37
    }
38
39
    /**
40
     * Sets the namespace separator used by classes in the namespace of this class loader.
41
     *
42
     * @param string $sep The separator to use.
43
     */
44
    public function setNamespaceSeparator($sep)
45
    {
46
        $this->_namespaceSeparator = $sep;
47
    }
48
49
    /**
50
     * Gets the namespace seperator used by classes in the namespace of this class loader.
51
     *
52
     * @return void
53
     */
54
    public function getNamespaceSeparator()
55
    {
56
        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...
57
    }
58
59
    /**
60
     * Sets the base include path for all class files in the namespace of this class loader.
61
     *
62
     * @param string $includePath
63
     */
64
    public function setIncludePath($includePath)
65
    {
66
        $this->_includePath = $includePath;
67
    }
68
69
    /**
70
     * Gets the base include path for all class files in the namespace of this class loader.
71
     *
72
     * @return string $includePath
73
     */
74
    public function getIncludePath()
75
    {
76
        return $this->_includePath;
77
    }
78
79
    /**
80
     * Sets the file extension of class files in the namespace of this class loader.
81
     *
82
     * @param string $fileExtension
83
     */
84
    public function setFileExtension($fileExtension)
85
    {
86
        $this->_fileExtension = $fileExtension;
87
    }
88
89
    /**
90
     * Gets the file extension of class files in the namespace of this class loader.
91
     *
92
     * @return string $fileExtension
93
     */
94
    public function getFileExtension()
95
    {
96
        return $this->_fileExtension;
97
    }
98
99
    /**
100
     * Installs this class loader on the SPL autoload stack.
101
     */
102
    public function register()
103
    {
104
        spl_autoload_register(array($this, 'loadClass'));
105
    }
106
107
    /**
108
     * Uninstalls this class loader from the SPL autoloader stack.
109
     */
110
    public function unregister()
111
    {
112
        spl_autoload_unregister(array($this, 'loadClass'));
113
    }
114
115
    /**
116
     * Loads the given class or interface.
117
     *
118
     * @param  string $className The name of the class to load.
119
     * @return void
120
     */
121
    public function loadClass($className)
122
    {
123
        if (null === $this->_namespace || $this->_namespace === substr($className, 0, strlen($this->_namespace))) {
124
            $fileName = '';
125
            $namespace = '';
126
            if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
127
                $namespace = substr($className, 0, $lastNsPos);
128
                $className = substr($className, $lastNsPos + 1);
129
                $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
130
            }
131
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).$this->_fileExtension;
132
133
            require ($this->_includePath !== null ? $this->_includePath.DIRECTORY_SEPARATOR : '').$fileName;
134
        }
135
    }
136
}
137