Skip to content
This repository
Browse code

[ClassLoader] added a debug class loader

  • Loading branch information...
commit aba8f1e1802fbafcd954c2b6f59272492bd4e6f7 1 parent fe9ef5c
Fabien Potencier authored May 05, 2011
62  src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php
... ...
@@ -0,0 +1,62 @@
  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
+/**
  15
+ * Checks that the class is actually declared in the included file.
  16
+ *
  17
+ * @author Fabien Potencier <fabien@symfony.com>
  18
+ */
  19
+class DebugUniversalClassLoader extends UniversalClassLoader
  20
+{
  21
+    /**
  22
+     * Replaces all regular UniversalClassLoader instances by a DebugUniversalClassLoader ones.
  23
+     */
  24
+    static public function enable()
  25
+    {
  26
+        if (!is_array($functions = spl_autoload_functions())) {
  27
+            return;
  28
+        }
  29
+
  30
+        foreach ($functions as $function) {
  31
+            spl_autoload_unregister($function);
  32
+        }
  33
+
  34
+        foreach ($functions as $function) {
  35
+            if (is_array($function) && $function[0] instanceof UniversalClassLoader) {
  36
+                $loader = new static();
  37
+                $loader->registerNamespaceFallback($function[0]->getNamespaceFallback());
  38
+                $loader->registerPrefixFallback($function[0]->getPrefixFallback());
  39
+                $loader->registerNamespaces($function[0]->getNamespaces());
  40
+                $loader->registerPrefixes($function[0]->getPrefixes());
  41
+
  42
+                $function[0] = $loader;
  43
+            }
  44
+
  45
+            spl_autoload_register($function);
  46
+        }
  47
+    }
  48
+
  49
+    /**
  50
+     * {@inheritDoc}
  51
+     */
  52
+    public function loadClass($class)
  53
+    {
  54
+        if ($file = $this->findFile($class)) {
  55
+            require $file;
  56
+
  57
+            if (!class_exists($class, false) && !interface_exists($class, false)) {
  58
+                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));
  59
+            }
  60
+        }
  61
+    }
  62
+}

0 notes on commit aba8f1e

Olli81

I worked 30 Minutes on this exception. Namespace and Class Name were correct. Exception was thrown due to missing "php" after "<?"

Christophe Coevoet

Well, if you are using short open tags (which is bad practice) on a server where they are disabled (which is good practice), it is normal to have issues as the code will not be parsed as PHP code.

stresler

I'm beating my head against the wall here. I don't have a typo, so that seems to indicate that Symfony is expecting a different namespace, but I don't really know what it's expecting. Is there a way to alter this exception to include expected namespace? It already has expected classname.

Christophe Coevoet

This is a fully qualified class name; So the namespace is already included

stresler

Ok, that makes sense. However, then this check (or a later commit) is checking for something and throwing this exception that isn't reflected in the error thrown.

Here is a pastebin of what I'm trying: http://pastebin.com/WX8aX7Pd

The namespace and classname are both correct without typos, so perhaps this should throw that error only when it knows that is the issue. Maybe another validation was added later that isn't a typo related issue?

edit: I'll start looking into this and make an issue/patch if I figure it out. Thanks for your response.

Christophe Coevoet

"Hopper/QueueBundle/Queue" as class name ? Where did you find that it is the same than Hopper\QueueBundle\Queue ?

Matthieu Vachon

Could it be that service definition should be read class: "Hopper\QueueBundle\Queue"?

stresler

Ok, THANK YOU! That fixed it. I really do appreciate it.

I do need to point out that the error thrown on this has the proper slashes, and found the file, so I think it's pretty natural I assumed my syntax was OK there. The error thrown left me looking in the wrong place for four hours, because it was not a typo in the classname or namespace.

Christophe Coevoet

It was a typo in the class name, in the place where you used it.

But as using / instead of \ seems quite common as mistake (I already helped several people facing such typo), I opened #6803 to give a better error message in this case as we can detect it quite easily.

Please sign in to comment.
Something went wrong with that request. Please try again.