Skip to content

[Contracts] Fix ServiceMethodsSubscriberTrait for nullable service #60267

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

Conversation

StevenRenaux
Copy link
Contributor

Q A
Branch? 7.2
Bug fix? yes
New feature? no
Deprecations? no
License MIT

Used in a bundle context.

If you use the following example, you will get an exception:
ServiceNotFoundException: The service "twig" in the container provided to "App\Service\Dependency" has a dependency on a non-existent service "Twig\Environment".

The nullable argument of the SubscribedService attribute is ignored.

// src/Service/TwigAware.php
namespace App\Service;

use Twig\Environment;
use Symfony\Contracts\Service\Attribute\SubscribedService;

trait TwigAware
{
   #[SubscribedService('twig', nullable: true)]
    private function twig(): Environment
    {
         if (!$this->container->has('twig') ) {
            throw new \LogicException(\sprintf('Twig is required to use "%s" method. Try to run "composer require symfony/twig-bundle".', __METHOD__));
        }

        return $environment;
    }
}

// src/Service/MyService.php
namespace App\Service;

use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Service\ServiceSubscriberTrait;

class MyService implements ServiceSubscriberInterface
{
    use ServiceSubscriberTrait, TwigAware;

    public function doWithTwig(): void
    {
        // $this->twig() ...
    }
}

Related to #60265

@carsonbot carsonbot added this to the 7.2 milestone Apr 25, 2025
@StevenRenaux StevenRenaux force-pushed the fix/Fix-nullable-argument-for-SubscribedService-attribute branch from 1a9c3a2 to 7d7e4a0 Compare April 25, 2025 09:12
@StevenRenaux StevenRenaux force-pushed the fix/Fix-nullable-argument-for-SubscribedService-attribute branch 2 times, most recently from db4b606 to 562600f Compare April 25, 2025 09:33
@StevenRenaux StevenRenaux force-pushed the fix/Fix-nullable-argument-for-SubscribedService-attribute branch from 562600f to e67db9a Compare April 25, 2025 09:37
@stof
Copy link
Member

stof commented Apr 25, 2025

#60268 should be merged first and upmerged to 7.2, so that this PR can then be only about the new things for 7.2

@carsonbot carsonbot changed the title Fix ServiceMethodsSubscriberTrait for nullable service [Contracts] Fix ServiceMethodsSubscriberTrait for nullable service Apr 25, 2025
@StevenRenaux
Copy link
Contributor Author

StevenRenaux commented Apr 25, 2025

#60268 should be merged first and upmerged to 7.2, so that this PR can then be only about the new things for 7.2

@stof
I'm agree but since 6.4 the tests about ServiceSubscriberTraits was updated, you can see with LegacyParentTestService. Or we need to updated it after the merge.

Let me know

@stof
Copy link
Member

stof commented Apr 25, 2025

@StevenRenaux I'm not saying this PR is useless. This PR should be updated after the upmerge (which will happen anyway)

Copy link
Member

@welcoMattic welcoMattic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @stof said, this one must be rebased after merge (and upmerge) of #60268.
Otherwise, it's 👍 for me

@fabpot
Copy link
Member

fabpot commented Apr 30, 2025

Thank you @StevenRenaux.

@fabpot fabpot merged commit 070869c into symfony:7.2 Apr 30, 2025
11 checks passed
@fabpot fabpot mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants