Updated translations (#1517)
[openemr.git] / vendor / symfony / debug / DebugClassLoader.php
blob2e1d71808e132bb5bd3e68b89e5e249fe05bdaf1
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug;
14 /**
15 * Autoloader checking if the class is really defined in the file found.
17 * The ClassLoader will wrap all registered autoloaders
18 * and will throw an exception if a file is found but does
19 * not declare the class.
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Christophe Coevoet <stof@notk.org>
23 * @author Nicolas Grekas <p@tchwork.com>
25 class DebugClassLoader
27 private $classLoader;
28 private $isFinder;
29 private static $caseCheck;
30 private static $final = array();
31 private static $finalMethods = array();
32 private static $deprecated = array();
33 private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
34 private static $darwinCache = array('/' => array('/', array()));
36 /**
37 * Constructor.
39 * @param callable $classLoader A class loader
41 public function __construct(callable $classLoader)
43 $this->classLoader = $classLoader;
44 $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
46 if (!isset(self::$caseCheck)) {
47 $file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), DIRECTORY_SEPARATOR);
48 $i = strrpos($file, DIRECTORY_SEPARATOR);
49 $dir = substr($file, 0, 1 + $i);
50 $file = substr($file, 1 + $i);
51 $test = strtoupper($file) === $file ? strtolower($file) : strtoupper($file);
52 $test = realpath($dir.$test);
54 if (false === $test || false === $i) {
55 // filesystem is case sensitive
56 self::$caseCheck = 0;
57 } elseif (substr($test, -strlen($file)) === $file) {
58 // filesystem is case insensitive and realpath() normalizes the case of characters
59 self::$caseCheck = 1;
60 } elseif (false !== stripos(PHP_OS, 'darwin')) {
61 // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
62 self::$caseCheck = 2;
63 } else {
64 // filesystem case checks failed, fallback to disabling them
65 self::$caseCheck = 0;
70 /**
71 * Gets the wrapped class loader.
73 * @return callable The wrapped class loader
75 public function getClassLoader()
77 return $this->classLoader;
80 /**
81 * Wraps all autoloaders.
83 public static function enable()
85 // Ensures we don't hit https://bugs.php.net/42098
86 class_exists('Symfony\Component\Debug\ErrorHandler');
87 class_exists('Psr\Log\LogLevel');
89 if (!is_array($functions = spl_autoload_functions())) {
90 return;
93 foreach ($functions as $function) {
94 spl_autoload_unregister($function);
97 foreach ($functions as $function) {
98 if (!is_array($function) || !$function[0] instanceof self) {
99 $function = array(new static($function), 'loadClass');
102 spl_autoload_register($function);
107 * Disables the wrapping.
109 public static function disable()
111 if (!is_array($functions = spl_autoload_functions())) {
112 return;
115 foreach ($functions as $function) {
116 spl_autoload_unregister($function);
119 foreach ($functions as $function) {
120 if (is_array($function) && $function[0] instanceof self) {
121 $function = $function[0]->getClassLoader();
124 spl_autoload_register($function);
129 * Loads the given class or interface.
131 * @param string $class The name of the class
133 * @return bool|null True, if loaded
135 * @throws \RuntimeException
137 public function loadClass($class)
139 ErrorHandler::stackErrors();
141 try {
142 if ($this->isFinder) {
143 if ($file = $this->classLoader[0]->findFile($class)) {
144 require_once $file;
146 } else {
147 call_user_func($this->classLoader, $class);
148 $file = false;
150 } finally {
151 ErrorHandler::unstackErrors();
154 $exists = class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
156 if ($class && '\\' === $class[0]) {
157 $class = substr($class, 1);
160 if ($exists) {
161 $refl = new \ReflectionClass($class);
162 $name = $refl->getName();
164 if ($name !== $class && 0 === strcasecmp($name, $class)) {
165 throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".', $class, $name));
168 $parent = get_parent_class($class);
170 // Not an interface nor a trait
171 if (class_exists($name, false)) {
172 if (preg_match('#\n \* @final(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
173 self::$final[$name] = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
176 if ($parent && isset(self::$final[$parent])) {
177 @trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
180 // Inherit @final annotations
181 self::$finalMethods[$name] = $parent && isset(self::$finalMethods[$parent]) ? self::$finalMethods[$parent] : array();
183 foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
184 if ($method->class !== $name) {
185 continue;
188 if ($parent && isset(self::$finalMethods[$parent][$method->name])) {
189 @trigger_error(sprintf('%s It may change without further notice as of its next major version. You should not extend it from "%s".', self::$finalMethods[$parent][$method->name], $name), E_USER_DEPRECATED);
192 $doc = $method->getDocComment();
193 if (false === $doc || false === strpos($doc, '@final')) {
194 continue;
197 if (preg_match('#\n\s+\* @final(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) {
198 $message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
199 self::$finalMethods[$name][$method->name] = sprintf('The "%s::%s()" method is considered final%s.', $name, $method->name, $message);
204 if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
205 @trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
206 } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
207 self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
208 } else {
209 // Don't trigger deprecations for classes in the same vendor
210 if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
211 $len = 0;
212 $ns = '';
213 } else {
214 switch ($ns = substr($name, 0, $len)) {
215 case 'Symfony\Bridge\\':
216 case 'Symfony\Bundle\\':
217 case 'Symfony\Component\\':
218 $ns = 'Symfony\\';
219 $len = strlen($ns);
220 break;
224 if (!$parent || strncmp($ns, $parent, $len)) {
225 if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
226 @trigger_error(sprintf('The "%s" class extends "%s" that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
229 $parentInterfaces = array();
230 $deprecatedInterfaces = array();
231 if ($parent) {
232 foreach (class_implements($parent) as $interface) {
233 $parentInterfaces[$interface] = 1;
237 foreach ($refl->getInterfaceNames() as $interface) {
238 if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len)) {
239 $deprecatedInterfaces[] = $interface;
241 foreach (class_implements($interface) as $interface) {
242 $parentInterfaces[$interface] = 1;
246 foreach ($deprecatedInterfaces as $interface) {
247 if (!isset($parentInterfaces[$interface])) {
248 @trigger_error(sprintf('The "%s" %s "%s" that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
255 if ($file) {
256 if (!$exists) {
257 if (false !== strpos($class, '/')) {
258 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));
261 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));
263 if (self::$caseCheck) {
264 $real = explode('\\', $class.strrchr($file, '.'));
265 $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
267 $i = count($tail) - 1;
268 $j = count($real) - 1;
270 while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
271 --$i;
272 --$j;
275 array_splice($tail, 0, $i + 1);
277 if (self::$caseCheck && $tail) {
278 $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
279 $tailLen = strlen($tail);
280 $real = $refl->getFileName();
282 if (2 === self::$caseCheck) {
283 // realpath() on MacOSX doesn't normalize the case of characters
285 $i = 1 + strrpos($real, '/');
286 $file = substr($real, $i);
287 $real = substr($real, 0, $i);
289 if (isset(self::$darwinCache[$real])) {
290 $kDir = $real;
291 } else {
292 $kDir = strtolower($real);
294 if (isset(self::$darwinCache[$kDir])) {
295 $real = self::$darwinCache[$kDir][0];
296 } else {
297 $dir = getcwd();
298 chdir($real);
299 $real = getcwd().'/';
300 chdir($dir);
302 $dir = $real;
303 $k = $kDir;
304 $i = strlen($dir) - 1;
305 while (!isset(self::$darwinCache[$k])) {
306 self::$darwinCache[$k] = array($dir, array());
307 self::$darwinCache[$dir] = &self::$darwinCache[$k];
309 while ('/' !== $dir[--$i]) {
311 $k = substr($k, 0, ++$i);
312 $dir = substr($dir, 0, $i--);
317 $dirFiles = self::$darwinCache[$kDir][1];
319 if (isset($dirFiles[$file])) {
320 $kFile = $file;
321 } else {
322 $kFile = strtolower($file);
324 if (!isset($dirFiles[$kFile])) {
325 foreach (scandir($real, 2) as $f) {
326 if ('.' !== $f[0]) {
327 $dirFiles[$f] = $f;
328 if ($f === $file) {
329 $kFile = $k = $file;
330 } elseif ($f !== $k = strtolower($f)) {
331 $dirFiles[$k] = $f;
335 self::$darwinCache[$kDir][1] = $dirFiles;
339 $real .= $dirFiles[$kFile];
342 if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
343 && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
345 throw new \RuntimeException(sprintf('Case mismatch between class and real file names: "%s" vs "%s" in "%s".', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
349 return true;