Add support for !service_closure custom tag in YamlFileLoader

Created on 29 May 2024, 27 days ago
Updated 18 June 2024, 7 days ago

Problem/Motivation

Since πŸ“Œ Support Yaml::PARSE_CUSTOM_TAGS in \Drupal\Component\Serial::decodeization\YamlSymfony:: Needs work 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

πŸ“Œ Task
Status

Fixed

Version

10.3 ✨

Component
BaseΒ  β†’

Last updated about 2 hours ago

Created by

πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @kim.pepper
  • Status changed to Needs review 27 days ago
  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia
  • Pipeline finished with Canceled
    27 days ago
    Total: 88s
    #184689
  • Pipeline finished with Success
    27 days ago
    Total: 512s
    #184690
  • Pipeline finished with Success
    27 days ago
    Total: 509s
    #184714
  • Status changed to RTBC 26 days ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    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.

  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

    Updated IS with more background

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    We should have a CR for this... something like [#3436859]

  • Status changed to Needs work 24 days ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

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

  • Status changed to RTBC 23 days ago
  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

    Added a change record.

    • alexpott β†’ committed 72397df3 on 11.x
      Issue #3450516 by kim.pepper: Add support for !service_closure custom...
  • Status changed to Fixed 23 days ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    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...
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    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...
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

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

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

Production build 0.69.0 2024