Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[6.2] Reference to abstract service with invalidBehavior:IGNORE_ON_INVALID_REFERENCE still produces error #48411

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

Closed
ruudk opened this issue Nov 30, 2022 · 2 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented Nov 30, 2022

Symfony version(s) affected

6.2.0

Description

After upgrading to 6.2.0 I noticed that some of my service locators broke.

How to reproduce

I was adding some typed arguments of a method (in a compiler pass) to an array like this:

foreach {
  // some logic
  $services[$class] = new Reference(
      $class
      ContainerInterface::IGNORE_ON_INVALID_REFERENCE,
  );
}

// Create service locator
ServiceLocatorTagPass::register($container, $services);

I was using ContainerInterface::IGNORE_ON_INVALID_REFERENCE to automatically filter out invalid services. This used to work fine in 6.1.

But in 6.2.0 I get this error:

In CheckReferenceValidityPass.php line 37:

  The definition ".service_locator.ZCXM_ST" has a reference to an abstract definition "App\Entity\SomeEntity". Abstract definitions cannot be the target of references.

Possible Solution

I think CheckReferenceValidityPass should not report an error when the Reference has ContainerInterface::IGNORE_ON_INVALID_REFERENCE.

Additional Context

It's related to:

@ruudk ruudk added the Bug label Nov 30, 2022
@ruudk ruudk changed the title Reference to abstract service with invalidBehavior:IGNORE_ON_INVALID_REFERENCE still produces error [6.2] Reference to abstract service with invalidBehavior:IGNORE_ON_INVALID_REFERENCE still produces error Nov 30, 2022
@ruudk
Copy link
Contributor Author

ruudk commented Nov 30, 2022

I think the problem is that those files are now added by auto wire (and tagged with container.excluded) and therefore do seem to exist in the container. That causes the invalid behaviour to not hit, because it does exist, it's just abstract by default.
Screenshot 2022-11-30 at 19 55 20@2x

@ruudk
Copy link
Contributor Author

ruudk commented Dec 1, 2022

Current work around is before I add services to the service locator, I filter them like this:

/**
 * This is needed because Symfony 6.2 adds excluded services ALSO to the container, but then
 * tagged with `container.excluded`. The Reference invalidBehavior: IGNORE_ON_INVALID_REFERENCE is ignored.
 * Waiting for them to fix it, until then, we filter out any services that are either abstract or excluded.
 *
 * @see https://github.com/symfony/symfony/issues/48411
 *
 * @param array<string, Reference> $references
 *
 * @return array<string, Reference>
 */
private function filterInvalidReferences(ContainerBuilder $container, array $references) : array
{
    return array_filter($references, function (Reference $reference) use ($container) {
        if ($container->hasDefinition((string) $reference) === false) {
            return false;
        }

        $definition = $container->getDefinition((string) $reference);

        if ($definition->isAbstract()) {
            return false;
        }

        if ($definition->hasTag('container.excluded')) {
            return false;
        }

        return true;
    });
}

nicolas-grekas added a commit that referenced this issue Dec 5, 2022
…excluded services when allowed (nicolas-grekas)

This PR was merged into the 6.2 branch.

Discussion
----------

[DependencyInjection] Remove refs that point to container.excluded services when allowed

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48411
| License       | MIT
| Doc PR        | -

Commits
-------

2e9fdf0 [DependencyInjection] Remove refs that point to container.excluded services when allowed
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

4 participants