annotations icon indicating copy to clipboard operation
annotations copied to clipboard

Register namespace prefix(es) for concrete annotations?

Open rulatir opened this issue 9 years ago • 4 comments

For project organization reasons I need my concrete annotation classes to reside in a deeply nested namespace, but I don't want to type the whole namespace path every time I use the annotation. It would be very welcome if I could register namespace prefix(es) for concrete annotation classes.

rulatir avatar Dec 17 '16 14:12 rulatir

Doctrine annotations does that, but it comes to the expense of parsing the file containing the dockblock looking for use statements.

An alternative approach could be to set the prefix on the annotation reader:

$reader->getParser()->registerFallbackNamespace('\My\CustomNamespace');

/**
 * @MyAnnotation -> {'foo': ['bar']}
 */
class SomeClass {}

$annotations = $reader->getClassAnnotations(SomeClass::class);

assert($annotations['\MyAnnotation'] === null);
assert($annotations['\My\CustomNamespace\MyAnnotation'] instanceof Someclass);

Unfortunately that alternative is not good as it could create interoperability issues. I guess parsing the file is the only way to go, but it should be activated optionally:

use My\CustomNamespace\MyAnnotation;

$reader->getParser()->resolveUseStatementsFromSource(true);

/**
 * @MyAnnotation -> {'foo': ['bar']}
 */
class SomeClass {}

$annotations = $reader->getClassAnnotations(SomeClass::class);

assert($annotations['\MyAnnotation'] === null);
assert($annotations['\My\CustomNamespace\MyAnnotation'] instanceof Someclass);

marcioAlmada avatar Dec 17 '16 15:12 marcioAlmada

I managed to achieve what I need by subclassing Parser and ConcreteType. As for the general solution, what specific interoperability issues do you foresee with registerFallbackNamespace() approach?

rulatir avatar Dec 17 '16 15:12 rulatir

Consider MyPackage\Validation\Email and OtherPackage\Validation\Email implementing a common Yada\ValidatorInterface, you won't be able to resolve both concrete annotations if needed.

marcioAlmada avatar Dec 17 '16 16:12 marcioAlmada

If you have two different classes with same name you definitely have to use them appropriately in the code (and/or docblock in this case)

Yesterday evening had a bit of a fiddle with this simple library to adapt registered namespace and worked out well in a lightweight framework currently trying to upgrade. Probably needs a bit of clean-up, fresh eyes as it was late in the night. Going to fork now and see what I have done

Can apply multiple registered namespaces too, but again, if you have same names for the class - that is a local project scope matter to deal with

doncem avatar Apr 29 '17 12:04 doncem