Symfony2: Getting Easier (part 2)

Yesterday, a member of the core team asked me for help. He didn't remembered
the naming convention for bundle extensions. The convention is simple enough
as this is basically the name of the bundle with Extension
as a suffix
instead of Bundle
. But that didn't work for him. Frankly, we were stuck. I
had no idea why it wouldn't work for him as it definitely works for me. But a
few minutes later, he found the problem. He had a typo in the namespace
declaration:
// file is Foo/Bar/Baz.php
namespace Foo/Bab;
// notice that the namespace is Foo/Bab
// but the path is Foo/Bar!
class Baz
{
// ...
}
No way I would have been able to find the problem as I had no access to his code. This is really frustrating as he does not work for the person you are trying to help, but you know that this should definitely work. At some point, you start yelling at the framework, and eventually you give up because the damn thing is too "complex". I had the exact same problem more than once in the past and I bet that most of you had it too. And if you don't give up, you can spend hours before spotting the problem... and start yelling at you!
Even if the problem has nothing to do with the framework, is it possible to enhance the developer experience here? Can Symfony2 help the developer avoiding this error in the first place? Every time someone reports a problem like this one, we should think how we can change the framework for the better.
After thinking a bit about this specific issue last night, it turns out that we can easily improve the situation and save hours of frustration all around the world.
Whenever the standard Symfony2 autoloader finds a file where the class to autoload should be defined, we can easily check that the class is effectively declared in the file we have just included. If that's not the case, there is something wrong:
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));
}
The actual patch is a bit more complex as it should not slow down your application in production, but you get the idea.
This enhancement will be available in Symfony SE beta2.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Great job.
>namespace Foo/Bab;
That's the power of having a community that's focused on the developer experience.
How will I make my customers pay while it becomes so easy !
Typo: "He didn't remembered the naming convention..." should be "He didn't remember the naming convention..."
I think you 'fix' is cute, but working on better documentation for many parts of Symfony2 is more important. As someone who develops in Symfony2, if I run into a problem when adding a class or bundle, the first thing I always check is namespaces. Always.
First great great work.
I've been experimenting with symfony 2 for a week now, overcoming some problems with tampering with the codes, updating components and all.
however, yesterday i came across validation with yaml and it seemed it does not work. pray tell me, master, is it in place yet? or i should look even harder into my code?
god speed!
Idea: Create some sort of command that lists all the files/classes "recognized" by the framework. I.e: bundles, DI extensions, etc.