From 5bd91e2f19c14a7bd6906e3c97e08c7be799114d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 5 May 2011 08:28:10 +0200 Subject: [PATCH] [ClassLoader] added a debug class loader --- DebugUniversalClassLoader.php | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 DebugUniversalClassLoader.php diff --git a/DebugUniversalClassLoader.php b/DebugUniversalClassLoader.php new file mode 100644 index 0000000..83220c9 --- /dev/null +++ b/DebugUniversalClassLoader.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\ClassLoader; + +/** + * Checks that the class is actually declared in the included file. + * + * @author Fabien Potencier + */ +class DebugUniversalClassLoader extends UniversalClassLoader +{ + /** + * Replaces all regular UniversalClassLoader instances by a DebugUniversalClassLoader ones. + */ + static public function enable() + { + if (!is_array($functions = spl_autoload_functions())) { + return; + } + + foreach ($functions as $function) { + spl_autoload_unregister($function); + } + + foreach ($functions as $function) { + if (is_array($function) && $function[0] instanceof UniversalClassLoader) { + $loader = new static(); + $loader->registerNamespaceFallback($function[0]->getNamespaceFallback()); + $loader->registerPrefixFallback($function[0]->getPrefixFallback()); + $loader->registerNamespaces($function[0]->getNamespaces()); + $loader->registerPrefixes($function[0]->getPrefixes()); + + $function[0] = $loader; + } + + spl_autoload_register($function); + } + } + + /** + * {@inheritDoc} + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + require $file; + + if (!class_exists($class, false) && !interface_exists($class, false)) { + throw new \Exception(sprintf('The autoloader expected class "%s" to be defined in file "%s". You probably have a typo in the namespace or the class name.', $class, $file)); + } + } + } +}