mediamonks / drupal-controller-annotations

Drupal port of the controller annotations from SymfonyFrameworkExtraBundle.
https://www.drupal.org/project/controller_annotations
9 stars 1 forks source link

Custom annotation classes #4

Open donquixote opened 7 years ago

donquixote commented 7 years ago

I was looking at AnnotatedRouteControllerLoader::configureRoute(). Currently this supports a limited and hardcoded selection of annotation classes.

What about a new interface for generic route config annotations, like this:

interface RouteConfigurationAnnotationInterface {
  public function configureRoute(Route $route);
}

Then in AnnotatedRouteControllerLoader::configureRoute() we would add this:

            } elseif ($configuration instanceof RouteConfigurationAnnotationInterface) {
                $configuration->configureRoute($route);

As a (controversial) use case, currently I am thinking of this: https://github.com/mediamonks/drupal-controller-annotations/issues/3 E.g. a @MenuLink(..) annotation, which would set an option in the route, which later can be read by a menu link plugin derivative.

Maybe there are less controversial use cases. E.g. shortcuts for @Security: Instead of @Security(access=true), one could then write @AccessPublic. Or instead of @Security(role="admin"), write @Role("admin")

Or maybe a @RouteTitle("Hello") or a generic @Option(_title = "Hello"), instead of @Route("/path/to/hello", options = {"title" = "Hello"})

Ok, I am not sure yet if my examples are really worthwhile, but I do think it would open some interesting possibilities.

Btw, I prefer longer identifiers like @RouteTitle over just @Title, this reduces the ambiguity on IDE autocomplete.

donquixote commented 7 years ago

Let me know if you want a PR :)

donquixote commented 7 years ago

See https://github.com/mediamonks/drupal-controller-annotations/issues/5 @RouteTitleCallback

donquixote commented 7 years ago

Another use case: @RouteIsAdmin instead of @Route(.., admin=true).

slootjes commented 7 years ago

I'm excited about the new ideas you come with and I also like the idea of the interface but I think it should be prevented to end up with tons of different annotations that could conflict with each other. If using the PhpStorm with the Annotations plugin the options available are all being autocompleted too btw. I'll leave this open for now to discuss possible new annotations that can currently not be done.

donquixote commented 7 years ago

it should be prevented to end up with tons of different annotations that could conflict with each other.

The idea, if that wasn't clear, is to allow others to write annotations for route settings outside of this module.

If using the PhpStorm with the Annotations plugin the options available are all being autocompleted too btw.

--Yes, this is what I am doing. It is great :)-- EDIT: Oh actually this specific feature I did not see yet, thanks!

slootjes commented 7 years ago

The annotations also have to be loaded manually because I couldn't not find a way of doing this in Drupal. Maybe a few other annotations can be created but I think most of it is covered by this.

donquixote commented 7 years ago

Yes, I noticed I needed to add some code to \Drupal\controller_annotations\Configuration\ConfigurationLoader::load().

This is called from RouteEventSubscriber and from ControllerEventSubscriber. So other modules which provide route modifier annotations would need to do the same. Annoying, but I don't see another way atm.

I am going to push a preview branch or PR soon-ish.

donquixote commented 7 years ago

I notice that those extra annotations only work on methods, not on the class itself. Do you know where the class annotations are processed?

donquixote commented 7 years ago

I am working with the new annotations, and I think they are fun.

I also created a custom module with more annotation classes @RouteMenuLink, @RouteTaskLink, @RouteDefaultTaskLink. It works!