Skip to content

Commit

Permalink
[ClassLoader] added a debug class loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 5, 2011
1 parent fe9ef5c commit aba8f1e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <fabien@symfony.com>
*/
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)
{

This comment has been minimized.

Copy link
@superjavason

superjavason May 23, 2011

Contributor

I can't delete this line!

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));

This comment has been minimized.

Copy link
@Olli81

Olli81 Jun 13, 2011

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

This comment has been minimized.

Copy link
@stof

stof Jun 13, 2011

Member

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.

This comment has been minimized.

Copy link
@stresler

stresler Jan 18, 2013

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.

This comment has been minimized.

Copy link
@stof

stof Jan 18, 2013

Member

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

This comment has been minimized.

Copy link
@stresler

stresler Jan 18, 2013

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.

This comment has been minimized.

Copy link
@stof

stof Jan 18, 2013

Member

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

This comment has been minimized.

Copy link
@maoueh

maoueh Jan 18, 2013

Contributor

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

This comment has been minimized.

Copy link
@stresler

stresler Jan 18, 2013

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.

This comment has been minimized.

Copy link
@stof

stof Jan 18, 2013

Member

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.

}
}
}
}

0 comments on commit aba8f1e

Please sign in to comment.