Skip to content

[DependencyInjection] Allow registering attributes on classes that cannot be instantiated #48392

Closed
@ruudk

Description

@ruudk

Description

In our application we rely Symfony's ContainerBuilder::registerAttributeForAutoconfiguration together with auto wire for a lot of things. I also helped improving it:

But now I have an issue. I have a class that has a private constructor. It needs to be constructed by calling a static method.
Example:

#[SomeAttribute]
final class SomeService
{
    private function __construct() {}
}

I have a compiler pass that tries to work this class by looking for SomeAttribute:

$container->registerAttributeForAutoconfiguration(
    SomeAttribute::class,
    static function (ChildDefinition $definition, SomeAttribute $attribute, ReflectionClass $reflector) : void {
        // do magic
    },
);

I load everything using:

$services->load('App\\', __DIR__ . '/');

Because of this line the class is never loaded because it's not instantiable.

if ($r->isInstantiable() || $r->isInterface()) {
$classes[$class] = null;
}

Possible solutions:

  1. Always run the registered attributes configurators for classes that cannot be instantiated
  2. Allow an additional option on the PrototypeConfigurator to indicate you want to always load attributes
  3. Change the condition above:
    - if ($r->isInstantiable() || $r->isInterface()) {
    -    $classes[$class] = null;
    - }
    + $classes[$class] = null;

/cc @nicolas-grekas since we worked in this area together in #42039 do you have ideas?

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy