[DCOM-75] remove leading backslash from class name before comparing to namespace in annotation autoloading Created: 03/Nov/11 Updated: 03/Nov/11 |
|
| Status: | Open |
| Project: | Doctrine Common |
| Component/s: | Annotations |
| Affects Version/s: | 2.1.2 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Minor |
| Reporter: | Guillaume ORIOL | Assignee: | Benjamin Eberlei |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Environment: |
not relevant |
||
| Description |
|
I am figuring a problem with Symfony Validator constraints (I use annotations to define the constraint rules). As I don't use Symfony's framework, I create the validator service by myself.
AnnotationRegistry::registerAutoloadNamespaces(array(
'\Symfony\Component\Validator\Constraints' => APPLICATION_ROOT . '/library'
));
Then, in my entities, I have annotations such as: use Symfony\Component\Validator\Constraints as Assert;
class Author {
/**
* @Assert\NotBlank()
*/
protected $name;
}
In this configuration, I get the following error:
I was able to trace it down to the Doctrine\Common\Annotations\AnnotationRegistry#loadAnnotationClass($class) where we can find the following test: if (strpos($class, $namespace) === 0) {
require ...;
}
which means "if the namespace can be found at the beginning of the FQCN, require it". Christophe Coevoet answered:
Benjamin Eberlei suggested to remove the leading backslash before comparing the class to the namespace. When I add a use statement to my code for a class, I can then use its alias to get an instance of that class. use Doctrine\ORM\Mapping\ClassMetadata;
...
$metadata = new ClassMetadata();
The same is true with: use Doctrine\ORM\Mapping as Foo;
...
$metadata = new Foo\ClassMetadata();
This is not a fully qualified class name. But I find the syntax of a fully-qualified annotation (@My\Annotation\Whatever) erroneous (or at least counter-intuitive) as it doesn't start with a backslash. |