]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Common/ClassLoader.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Common / ClassLoader.php
1 <?php
2 /**
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 *
19 * @category Libraries
20 * @package ClassLoader
21 * @author Doctrine project <contact@doctrine-project.org>
22 * @author Johan Cwiklinski <johan@x-tnd.be>
23 * @license LGPL https://www.gnu.org/licenses/lgpl-3.0.fr.html
24 * @link http://www.doctrine-project.org - http://galette.tuxfamily.org
25 * @since ?
26 */
27
28 namespace Galette\Common;
29
30 /**
31 * A <tt>ClassLoader</tt> is an autoloader for class files that can be
32 * installed on the SPL autoload stack. It is a class loader that either loads only classes
33 * of a specific namespace or all namespaces and it is suitable for working together
34 * with other autoloaders in the SPL autoload stack.
35 *
36 * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
37 * relies on the PHP <code>include_path</code>.
38 *
39 * @category Libraries
40 * @package ClassLoader
41 * @author Roman Borschel <roman@code-factory.org>
42 * @license LGPL https://www.gnu.org/licenses/lgpl-3.0.fr.html
43 * @link http://www.doctrine-project.org - http://galette.tuxfamily.org
44 * @since 2.0
45 */
46 class ClassLoader
47 {
48 /**
49 * @var string PHP file extension
50 */
51 protected $fileExtension = '.php';
52
53 /**
54 * @var string Current namespace
55 */
56 protected $namespace;
57
58 /**
59 * @var string Current include path
60 */
61 protected $includePath;
62
63 /**
64 * @var string PHP namespace separator
65 */
66 protected $namespaceSeparator = '\\';
67
68 /**
69 * Creates a new <tt>ClassLoader</tt> that loads classes of the
70 * specified namespace from the specified include path.
71 *
72 * If no include path is given, the ClassLoader relies on the PHP include_path.
73 * If neither a namespace nor an include path is given, the ClassLoader will
74 * be responsible for loading all classes, thereby relying on the PHP include_path.
75 *
76 * @param string $ns The namespace of the classes to load.
77 * @param string $includePath The base include path to use.
78 */
79 public function __construct($ns = null, $includePath = null)
80 {
81 if (!file_exists($includePath)) {
82 throw new \RuntimeException('Include path "' . $includePath . '" doesn\'t exists');
83 }
84
85 $this->namespace = $ns;
86 $this->includePath = $includePath;
87 }
88
89 /**
90 * Sets the namespace separator used by classes in the namespace of this ClassLoader.
91 *
92 * @param string $sep The separator to use.
93 *
94 * @return void
95 */
96 public function setNamespaceSeparator($sep)
97 {
98 $this->namespaceSeparator = $sep;
99 }
100
101 /**
102 * Gets the namespace separator used by classes in the namespace of this ClassLoader.
103 *
104 * @return string
105 */
106 public function getNamespaceSeparator()
107 {
108 return $this->namespaceSeparator;
109 }
110
111 /**
112 * Sets the base include path for all class files in the namespace of this ClassLoader.
113 *
114 * @param string $includePath Include path
115 *
116 * @return void
117 */
118 public function setIncludePath($includePath)
119 {
120 $this->includePath = $includePath;
121 }
122
123 /**
124 * Gets the base include path for all class files in the namespace of this ClassLoader.
125 *
126 * @return string
127 */
128 public function getIncludePath()
129 {
130 return $this->includePath;
131 }
132
133 /**
134 * Sets the file extension of class files in the namespace of this ClassLoader.
135 *
136 * @param string $fileExtension File extension
137 *
138 * @return void
139 */
140 public function setFileExtension($fileExtension)
141 {
142 $this->fileExtension = $fileExtension;
143 }
144
145 /**
146 * Gets the file extension of class files in the namespace of this ClassLoader.
147 *
148 * @return string
149 */
150 public function getFileExtension()
151 {
152 return $this->fileExtension;
153 }
154
155 /**
156 * Registers this ClassLoader on the SPL autoload stack.
157 *
158 * @return void
159 */
160 public function register()
161 {
162 spl_autoload_register(array($this, 'loadClass'));
163 }
164
165 /**
166 * Removes this ClassLoader from the SPL autoload stack.
167 *
168 * @return void
169 */
170 public function unregister()
171 {
172 spl_autoload_unregister(array($this, 'loadClass'));
173 }
174
175 /**
176 * Loads the given class or interface.
177 *
178 * @param string $className The name of the class to load.
179 *
180 * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
181 */
182 public function loadClass($className)
183 {
184 if ($this->namespace !== null && strpos($className, $this->namespace . $this->namespaceSeparator) !== 0) {
185 return false;
186 }
187
188 if ($this->namespace !== null) {
189 $className = str_replace(
190 $this->namespaceSeparator,
191 DIRECTORY_SEPARATOR,
192 $className
193 );
194 }
195
196 $path = ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
197 . $className
198 . $this->fileExtension;
199
200 if (file_exists($path)) {
201 require $path;
202 return true;
203 } else {
204 return false;
205 }
206 }
207
208 /**
209 * Asks this ClassLoader whether it can potentially load the class (file) with
210 * the given name.
211 *
212 * @param string $className The fully-qualified name of the class.
213 * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
214 */
215 public function canLoadClass($className)
216 {
217 if ($this->namespace !== null && strpos($className, $this->namespace . $this->namespaceSeparator) !== 0) {
218 return false;
219 }
220
221 $file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
222
223 if ($this->includePath !== null) {
224 return file_exists($this->includePath . DIRECTORY_SEPARATOR . $file);
225 }
226
227 return (false !== stream_resolve_include_path($file));
228 }
229
230 /**
231 * Checks whether a class with a given name exists. A class "exists" if it is either
232 * already defined in the current request or if there is an autoloader on the SPL
233 * autoload stack that is a) responsible for the class in question and b) is able to
234 * load a class file in which the class definition resides.
235 *
236 * If the class is not already defined, each autoloader in the SPL autoload stack
237 * is asked whether it is able to tell if the class exists. If the autoloader is
238 * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
239 * function of the autoloader is invoked and expected to return a value that
240 * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
241 * that the class exists, TRUE is returned.
242 *
243 * Note that, depending on what kinds of autoloaders are installed on the SPL
244 * autoload stack, the class (file) might already be loaded as a result of checking
245 * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
246 * these responsibilities.
247 *
248 * @param string $className The fully-qualified name of the class.
249 * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
250 */
251 public static function classExists($className)
252 {
253 if (class_exists($className, false) || interface_exists($className, false)) {
254 return true;
255 }
256
257 foreach (spl_autoload_functions() as $loader) {
258 if (is_array($loader)) { // array(???, ???)
259 if (is_object($loader[0])) {
260 if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
261 if ($loader[0]->canLoadClass($className)) {
262 return true;
263 }
264 } elseif ($loader[0]->{$loader[1]}($className)) {
265 return true;
266 }
267 } elseif ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
268 return true;
269 }
270 } elseif ($loader instanceof \Closure) { // function($className) {..}
271 if ($loader($className)) {
272 return true;
273 }
274 } elseif (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
275 return true;
276 }
277 }
278
279 return class_exists($className, false) || interface_exists($className, false);
280 }
281
282 /**
283 * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
284 * for (and is able to load) the class with the given name.
285 *
286 * @param string $className The name of the class.
287 * @return The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
288 */
289 public static function getClassLoader($className)
290 {
291 foreach (spl_autoload_functions() as $loader) {
292 if (is_array($loader)
293 && $loader[0] instanceof ClassLoader
294 && $loader[0]->canLoadClass($className)
295 ) {
296 return $loader[0];
297 }
298 }
299
300 return null;
301 }
302 }