I am trying to use Doctrine Common to create my own annotation. http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html does not match https://github.com/doctrine/common because Call to undefined method Doctrine\\Common\\Annotations\\AnnotationReader::setDefaultAnnotationNamespace and PHP Fatal error: Call to undefined method Doctrine\\Common\\Annotations\\AnnotationRegistry::registerAnnotationNamespace. I checked the source, they are not there. According to git log they were removed a year ago.
I have a PSR-0 autoloader going (from Symfony). So I have a file where the PSR-0 loader expects:
namespace My\Test;
/**
* @Annotation
*/
class Foo {
}
Another class
namespace My\Annotated
/**
* @My\Test\Foo
*/
class Test {
}
Reader:
namespace My\Reader
use ReflectionClass;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use My\Test\Foo;
$reader = new AnnotationReader();
$reflectionClass = new ReflectionClass('My\\Annotated\\Test');
$classAnnotations = $reader->getClassAnnotations($reflectionClass);
var_dump($classAnnotations);
Gets: "[Semantical Error] The annotation "@My\\Test\\Foo" in class My\\Annotated\\test was never imported. Did you maybe forget to add a "use" statement for this annotation?" I ilikely did but for my life I can’t figure out where to add it. And I really would like to just use @Foo if possible.
Autoloading is apparently handled by
AnnotationRegistry::registerAutoloadNamespacewhich is a PSR-0 autoloader. Documentation/source.I found that you can do a
use My\Test\Fooin the annotated file to use@Fooas an annotation. And the reason for that is possible is because Doctrine re-parses the file solely forusestatements.