Yaffs site version 1.1
[yaffs-website] / vendor / symfony / class-loader / DebugClassLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ClassLoader;
13
14 @trigger_error('The '.__NAMESPACE__.'\DebugClassLoader class is deprecated since version 2.4 and will be removed in 3.0. Use the Symfony\Component\Debug\DebugClassLoader class instead.', E_USER_DEPRECATED);
15
16 /**
17  * Autoloader checking if the class is really defined in the file found.
18  *
19  * The DebugClassLoader will wrap all registered autoloaders providing a
20  * findFile method and will throw an exception if a file is found but does
21  * not declare the class.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  * @author Christophe Coevoet <stof@notk.org>
25  *
26  * @deprecated since version 2.4, to be removed in 3.0.
27  *             Use {@link \Symfony\Component\Debug\DebugClassLoader} instead.
28  */
29 class DebugClassLoader
30 {
31     private $classFinder;
32
33     /**
34      * Constructor.
35      *
36      * @param object $classFinder
37      */
38     public function __construct($classFinder)
39     {
40         $this->classFinder = $classFinder;
41     }
42
43     /**
44      * Gets the wrapped class loader.
45      *
46      * @return object a class loader instance
47      */
48     public function getClassLoader()
49     {
50         return $this->classFinder;
51     }
52
53     /**
54      * Replaces all autoloaders implementing a findFile method by a DebugClassLoader wrapper.
55      */
56     public static function enable()
57     {
58         if (!is_array($functions = spl_autoload_functions())) {
59             return;
60         }
61
62         foreach ($functions as $function) {
63             spl_autoload_unregister($function);
64         }
65
66         foreach ($functions as $function) {
67             if (is_array($function) && !$function[0] instanceof self && method_exists($function[0], 'findFile')) {
68                 $function = array(new static($function[0]), 'loadClass');
69             }
70
71             spl_autoload_register($function);
72         }
73     }
74
75     /**
76      * Unregisters this instance as an autoloader.
77      */
78     public function unregister()
79     {
80         spl_autoload_unregister(array($this, 'loadClass'));
81     }
82
83     /**
84      * Finds a file by class name.
85      *
86      * @param string $class A class name to resolve to file
87      *
88      * @return string|null
89      */
90     public function findFile($class)
91     {
92         return $this->classFinder->findFile($class) ?: null;
93     }
94
95     /**
96      * Loads the given class or interface.
97      *
98      * @param string $class The name of the class
99      *
100      * @return bool|null True, if loaded
101      *
102      * @throws \RuntimeException
103      */
104     public function loadClass($class)
105     {
106         if ($file = $this->classFinder->findFile($class)) {
107             require $file;
108
109             if (!class_exists($class, false) && !interface_exists($class, false) && (!function_exists('trait_exists') || !trait_exists($class, false))) {
110                 if (false !== strpos($class, '/')) {
111                     throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
112                 }
113
114                 throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
115             }
116
117             return true;
118         }
119     }
120 }