[Semantical Error] The annotation does not exist, or could not be auto-loaded.
unckleg opened this issue · 4 comments
Following Doctrine Annotation 2.1 documentation.
<?php
use MyProject\Annotations\Custom;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
AnnotationRegistry::registerAutoloadNamespace("MyProject\Annotations", "/path/to/myproject/src");
$reader = new AnnotationReader();
$data = $reader->getClassAnnotations(
new \ReflectionClass(SomeClass::class)
);
AnnotationReader will call static method loadAnnotationClass($class), where the argument of method
is Annotation FQN and try to resolve providen path.
So if we provided the directory as second argument to registerAutoloadNamespace the loadAnnotationClass will result with wrong class file path and in that case it will not be able to find the same.
.
Doctrine/Common/Annotations/AnnotationRegistry
....
static public function loadAnnotationClass($class)
{
foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
if (strpos($class, $namespace) === 0) {
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
if ($dirs === null) {
if ($path = stream_resolve_include_path($file)) {
require $path;
return true;
}
} else {
foreach((array)$dirs AS $dir) {
// dumped file path
dump($dir . DIRECTORY_SEPARATOR . $file); exit;
if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
require $dir . DIRECTORY_SEPARATOR . $file;
return true;
}
}
}
}
}
foreach (self::$loaders AS $loader) {
if (call_user_func($loader, $class) === true) {
return true;
}
}
return false;
}
Result is: /path/to/myproject/src/MyProject/Annotations/Custom.php
But it should be: /path/to/myproject/src/Annotations/Custom.php
@unckleg the code just checks for the namespace substring and bails out if it doesn't match: we never supported PSR-4 directory style, and we're one of the PHP-FIG voters that voted against adoption of PSR-4, so this is really against our intent to support it anyway.
Closing as invalid
.
Please note that we also plan to drop the AnnotationRegistry
details: you should use the composer autoloader to load your annotations.
Now everything makes sense.
As you said, you are planning to drop AnnotationRegistry, currently I'm autoloading annotations through
registerLoader method and passing required composer autoload.php file.
But how it should be done without a registry ?
Thanks!
@unckleg I documented this in the deprecation: https://github.com/doctrine/annotations/blob/2497b1f9db56278d3ad2248f9e4bdbbbaa271c3e/lib/Doctrine/Common/Annotations/AnnotationRegistry.php#L77-L79
As I thought, thanks!