Problem/Motivation

Wondering if you could auto-discover classes in a compiler pass by looking for annotations, and hence avoid needing to declare a service.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork hux-3266008

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

larowlan created an issue. See original summary.

dpi’s picture

If not added as a service..

What did you have in mind in particular?

  • Looking for classes in a directory?
  • Scan all classes looking for a class annotation? Do we even have access to the classloader? get_declared_classes/spl_autoload_functions?
  • Look for classes implementing an interface? (Didnt want to do this)

There is of course already a compiler pass which looks for tagged services,

dpi’s picture

I've got some code I'm fiddling with, instead of a class attributes I'm just checking methods for any Hux attributes. Then creating a service for each class with the same tags as to-date functionality.

What do you think of this?

How do you think dep injection or otherwise should work?

larowlan’s picture

Looks good, dependency injection could use ContainerInjectionInterface (ie controller resolver)
I wonder if you should check if there's already a definition in the container for the class, just in case someone has manually registered it.

dpi’s picture

With this approach it adds all the classes detected as a service. Which means that when I ->container->get the service the class is created for me. I dont have the ability like ClassResolver to just call ::create.

Just had a thought about abusing factories to do this for me...

dpi’s picture

@larowlan how does this look?

dpi’s picture

I wonder if you should check if there's already a definition in the container for the class, just in case someone has manually registered it.

added! It will only ignore if same class and also tagged with “hooks”

larowlan’s picture

As mentioned in a call earlier in the week, this looks great - nice work!

  • dpi committed 366cef4 on 1.x
    Issue #3266008 by dpi, larowlan: Add a compiler pass to auto-discover...
dpi’s picture

Status: Active » Fixed

Merged!

Can be found in 1.0.0-beta4

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.