Problem/Motivation

Since #3108309: Support Yaml::PARSE_CUSTOM_TAGS in \Drupal\Component\Serialization\YamlSymfony::decode we support custom tags in \Drupal\Core\DependencyInjection\YamlFileLoader but do not have explicit support for the !service_closure custom tag.

We already support this in the container with the #[AutowireServiceClosure('foo')] attribute.

This issue adds support for the !service_closure custom tag in services.yml files.

https://symfony.com/doc/current/service_container/service_closures.html

This feature wraps the injected service into a closure allowing it to be lazily loaded when and if needed. This is useful if the service being injected is a bit heavy to instantiate or is used only in certain cases. The service is instantiated the first time the closure is called, while all subsequent calls return the same instance, unless the service is not shared:

Steps to reproduce

Proposed resolution

Add support for !service_closure custom tag in YamlFileLoader

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3450516

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

kim.pepper created an issue. See original summary.

kim.pepper’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Change seems simple enough

1) Drupal\Tests\Core\DependencyInjection\YamlFileLoaderTest::testParseDefinitionsWithProvider
Failed asserting that an object is an instance of class Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument.
/builds/issue/drupal-3450516/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php:74
FAILURES!
Tests: 15, Assertions: 38, Failures: 1.

Test-only failure shows also has coverage.

Solution matches what's proposed so believe this is good.

kim.pepper’s picture

Issue summary: View changes

Updated IS with more background

alexpott’s picture

Issue tags: +Needs change record

We should have a CR for this... something like #3436859: Services can now use tagged iterators in services.yml files

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

ONce #6 is done we can set this back to rtbc.

kim.pepper’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

Added a change record.

  • alexpott committed 72397df3 on 11.x
    Issue #3450516 by kim.pepper: Add support for !service_closure custom...
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 72397df and pushed to 11.x. Thanks!

diff --git a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
index cbf282b181..39c4a771f5 100644
--- a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
+++ b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
@@ -494,9 +494,9 @@ private function resolveServices(mixed $value): mixed
               return $argument;
             }
 
-          if ('service_closure' === $value->getTag()) {
-            return new ServiceClosureArgument($this->resolveServices($argument));
-          }
+            if ($value->getTag() === 'service_closure') {
+                return new ServiceClosureArgument($this->resolveServices($argument));
+            }
 
         }
         if (is_array($value)) {

Fixed coding standards to comply with Symfony's and the rest of the file... spaces and yoda ifs...

Going to discuss with release managers about which releases this belongs in.

  • alexpott committed 03dbcdc7 on 10.4.x
    Issue #3450516 by kim.pepper: Add support for !service_closure custom...
alexpott’s picture

Discussed with @catch we agreed to put in 10.4.x - and possibly 10.3 and 11.0 - will discuss some more.

  • alexpott committed 3552c87a on 10.3.x
    Issue #3450516 by kim.pepper: Add support for !service_closure custom...

  • alexpott committed 231fafe5 on 11.0.x
    Issue #3450516 by kim.pepper: Add support for !service_closure custom...
alexpott’s picture

Version: 11.x-dev » 10.3.x-dev

Discussed with @longwave and we agreed to backport this to 10.3.x

Status: Fixed » Closed (fixed)

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