Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register namespace prefix(es) for concrete annotations? #63

Open
rulatir opened this issue Dec 17, 2016 · 4 comments
Open

Register namespace prefix(es) for concrete annotations? #63

rulatir opened this issue Dec 17, 2016 · 4 comments

Comments

@rulatir
Copy link

rulatir commented Dec 17, 2016

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.

@marcioAlmada
Copy link
Owner

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);

@rulatir
Copy link
Author

rulatir commented Dec 17, 2016

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?

@marcioAlmada
Copy link
Owner

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.

@doncem
Copy link

doncem commented Apr 29, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants