Skip to content

Conversation

@StevenRenaux
Copy link
Contributor

Q A
Branch? 6.4
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 6.4 milestone Apr 25, 2025
@StevenRenaux StevenRenaux changed the title Fix ServiceMethodsSubscriberTrait for nullable service for LTs version Fix ServiceMethodsSubscriberTrait for nullable service for LTS version Apr 25, 2025
@welcoMattic welcoMattic changed the title Fix ServiceMethodsSubscriberTrait for nullable service for LTS version Fix ServiceSubscriberTrait for nullable service for LTS version Apr 25, 2025
@stof stof changed the title Fix ServiceSubscriberTrait for nullable service for LTS version Fix ServiceSubscriberTrait for nullable service Apr 25, 2025
@carsonbot carsonbot changed the title Fix ServiceSubscriberTrait for nullable service [Contracts] Fix ServiceSubscriberTrait for nullable service Apr 25, 2025
@fabpot
Copy link
Member

fabpot commented Apr 30, 2025

Thank you @StevenRenaux.

@fabpot fabpot merged commit b59a25a into symfony:6.4 Apr 30, 2025
10 of 11 checks passed
This was referenced 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.

7 participants