-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoloader.php
More file actions
164 lines (152 loc) · 4.67 KB
/
Autoloader.php
File metadata and controls
164 lines (152 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Definition of PSR-0 compliant autoloader class
*
* @copyright 2013-today Justso GmbH
* @author j.schirrmacher@justso.de
* @package justso
*/
namespace justso\justapi;
/**
* Autoloader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names.
*
* Taken from https://gist.github.com/221634
*
* http://groups.google.com/group/php-standards/web/final-proposal
*
* // Example which loads classes for the Doctrine Common package in the
* // Doctrine\Common namespace.
* $classLoader = new Autoloader('Doctrine\Common', '/path/to/doctrine');
* $classLoader->register();
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class Autoloader
{
private $fileExtension = '.php';
private $namespace;
private $includePath;
private $namespaceSeparator = '\\';
/**
* Creates a new <tt>Autoloader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
* @param string $includePath Path to classes
* @codeCoverageIgnore
*/
public function __construct($ns = null, $includePath = null)
{
$this->namespace = $ns;
$this->includePath = $includePath;
}
/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
* @codeCoverageIgnore
*/
public function setNamespaceSeparator($sep)
{
$this->namespaceSeparator = $sep;
}
/**
* Gets the namespace separator used by classes in the namespace of this class loader.
*
* @return string
* @codeCoverageIgnore
*/
public function getNamespaceSeparator()
{
return $this->namespaceSeparator;
}
/**
* Sets the base include path for all class files in the namespace of this class loader.
*
* @param string $includePath
* @codeCoverageIgnore
*/
public function setIncludePath($includePath)
{
$this->includePath = $includePath;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
* @codeCoverageIgnore
*/
public function getIncludePath()
{
return $this->includePath;
}
/**
* Sets the file extension of class files in the namespace of this class loader.
*
* @param string $fileExtension
* @codeCoverageIgnore
*/
public function setFileExtension($fileExtension)
{
$this->fileExtension = $fileExtension;
}
/**
* Gets the file extension of class files in the namespace of this class loader.
*
* @return string $fileExtension
* @codeCoverageIgnore
*/
public function getFileExtension()
{
return $this->fileExtension;
}
/**
* Installs this class loader on the SPL autoload stack.
* @codeCoverageIgnore
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
* @codeCoverageIgnore
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return bool
*/
public function loadClass($className)
{
$found = false;
$nsSep = $this->namespace.$this->namespaceSeparator;
if (null === $this->namespace || $nsSep === substr($className, 0, strlen($nsSep))) {
$fileName = '';
if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace);
$fileName .= DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
$filePath = stream_resolve_include_path($this->includePath . DIRECTORY_SEPARATOR . $fileName);
if ($filePath) {
/** @noinspection PhpIncludeInspection */
require $filePath;
$found = true;
}
}
return $found;
}
}