Review main Merged

87575[TASK] Add DI alias for ExtensionConfiguration

Sascha Nowak CR +2 V +2 Easy +2 −0 updated 01/21/25, 11:45

Commit message

When services need extension configuration options, and when
these options are not relevant during a potential load/unload
proceduce within extensionmanager, it can be helpful to simplify
injection of single extension configuration values directly.

Previously, the fully qualified class name with three slashes was
required. This update adds an alias to facilitate the use of the
autowire expression. You can now use the alias in the service, for
example:

final readonly class MyClass
{
    public function __construct(
        #[Autowire(expression: 'service("extension-configuration").get("extdemo", "enabled")')]
        protected bool $enabled = false
    ) {
    }

    public function __invoke(): void
    {
        if ($this->enabled === false) {
            return;
        }
    }
}

We should not blindly add aliases for arbitrary services, but
in this case, it can be benefitial to not bloat consuming service
attributes too much. This is mainly side effect free as long as
the configuration option is not relevant during a load/unload
extension operation, which is the case in many cases.

Review

  • Code review CR +2 Done
    • Stefan Bürk +2
    • Christian Kuhn +1
    • Benni Mack +1
  • Verified V +2 Done
    • Stefan Bürk +2
    • Christian Kuhn +1
    • Benni Mack +1
    • core-ci +1 CI

Issues 1

Files 1

Review log 2225

  1. Sascha Nowak uploaded patch set 1 12/20/24, 07:32

  2. core-ci voted on patch set 1 V +1 12/20/24, 07:39 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/71494

  3. Sascha Nowak voted on patch set 1 CR +1 V +1 12/20/24, 07:44

  4. Andreas Kienast commented on patch set 1 12/20/24, 08:27

    @typo3@saschanowak.me please do not vote your own patches unless somebody else made _significant_ changes. This defeats the purpose of a review system...
  5. Benjamin Franzke commented on patch set 1 CR -1 12/20/24, 08:32

    I do not exactly get this argument. In functional tests you can create an instance of private services via `$this->get(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)`. Is this patch still needed, taking this possibility into account? See https://github.com/TYPO3/testing-framework/pull/331 for more information.
  6. Sascha Nowak updated the commit message in patch set 2 CR +1 V +1 12/20/24, 11:23

  7. Sascha Nowak commented on patch set 2 12/20/24, 11:28

    Reply

    Thanks for the clarification. I thought there was a time where you vote on your on patch to show it's ready for review.

    Reply

    Ok it seems my example was removed from the commit message. I added it again an hopefully it's more clear why it's easier to test a class using the feature, as you do not have to mock the ExtensionConfiguration at all. You can just use the value of choice when constructing the object. This alias is just a convenience as you can still use the fqcn already: #[Autowire(expression: 'service("TYPO3\\\CMS\\\Core\\\Configuration\\\ExtensionConfiguration").get("extdemo", "enabled")')] vs. #[Autowire(expression: 'service("extension-configuration").get("extdemo", "enabled")')]
  8. Sascha Nowak commented on patch set 2 12/20/24, 11:28

    Reply

    Done
  9. core-ci voted on patch set 2 V +1 12/20/24, 11:30 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/71504

  10. Christian Kuhn commented on patch set 2 CR +1 V +1 01/07/25, 12:51

    Reply

    Acknowledged
  11. Christian Kuhn voted on patch set 2 CR 0 V 0 01/07/25, 13:41

  12. Stefan Bürk commented on patch set 2 01/07/25, 16:49

    Reply

    I'm not a fan of adding an alias here. I understood the intention behind this, but in my eyes injecting scalar configuration values out of a extension configuration is not a good design, specially when the configuraiton get more options etc. What I personally used in the past and got easier with PHP Attributes is, to implement a extension configuration object holding all extension configuration values. In old days I used the constructor to retrieve the ExtensionConfiguration class with GU::makeInstance(), since v11 I uses (Services.yaml) to use either a factory class or a factory method on the configuration object to fill that in, and simply using the custom configuration class as TypeHint. So, within a pure TYPO3 v13 (or main/v14) something like following (simplified): ```php <?php declare(strict_types=1); namespace MyVendor\MyExtension\Configuration; use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; #[Autoconfigure(constructor: 'createForDI')] final readonly MyExtensionConfiguration { public function __construct( public bool $enabled = false, public string $apiKey = '', ) { } /** * @param ExtensionConfiguration $configuration * @return self * @throws ExtensionConfigurationExtensionNotConfiguredException * @throws ExtensionConfigurationPathDoesNotExistException */ public static function createForDI( ExtensionConfiguration $configuration ): self { // @todo Eventually use a try-catch block and return a empty // default configuraiton in case extension configuration // does not exists. return new self( enabled: (bool)$configuration->get( 'my_ext', 'enabled' ), apiKey: (string)$configuration->get( 'my_ext', 'apiKey' ), ); } } ``` and for the service requiring the extension configuration, ```php <?php declare(strict_types=1); namespace MyVendor\MyExtension\Service; final readonly class MyService { public function __construct( private MyExtensionConfiguration $myExtensionConfiguration, private EventDispatcherInterface $eventDispatcher, ) { } public function __invoke(): void { if ($this->myExtensionConfiguration->enabled === false) { return; } } } ``` which requires only the pure minimalistic `Services.yaml` configuration: ```yaml services: _defaults: autowire: true autoconfigure: true public: false MyVendor\MyExtension\: resource: '../Classes/*' ``` along with the `ext_conf_template.txt`. This approach can be further enrichted by using PHP Attributes on the custom configuration class. This allows to test different configurations within a test by creating the custom configuration class by hand, still avoiding to mock the TYPO3 ExtensionConfiguration class: ``` public static function configurationValues(): \Generator { yield 'disabled' => [ 'extensionConfiguration' => [ 'enabled' => false, 'apiKey' => '', ], ]; yield 'enabled but missing apiKey' => [ 'extensionConfiguration' => [ 'enabled' => true, 'apiKey' => '', ], ]; yield 'enabled with apiKey set' => [ 'extensionConfiguration' => [ 'enabled' => true, 'apiKey' => 'some-api-key', ], ]; } /** * @param array{enabled: bool, apiKey: string} $extensionConfiguration */ #[Test] public function someTestMethod(array $extensionConfiguration): void $myConfiguration = new MyExtensionConfiguration( ...$extensionConfiguration, ); $subject = new MyService( // my extension configuration class $myConfiguration, // additional constructor services $this->get(EventDispatcherInterface::class), ; } ``` If really needed a extension can create a custom alias in the Services.yaml: ``` services: _defaults: autowire: true autoconfigure: true public: false MyVendor\MyExtension\: resource: '../Classes/*' 'myextension-extension-configuration': '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration' ``` and use ```php <?php declare(strict_types=1); namespace MyVendor\MyExtension\Service; final readonly class MyService { public function __construct( #[Autowire( expression: 'service("myextension-extension-configuration").get("my_ext", "enabled")' )] private bool $enabled, private EventDispatcherInterface $eventDispatcher, ) { } public function __invoke(): void { if ($this->myExtensionConfiguration->enabled === false) { return; } } } ``` I would suggest to abandon the change and reject the issue on forge, but not doing so right now to see what @lolli@schwarzbu.ch and/or @ben@bnf.dev think about this. For the ExtensionConfiguration it may be reasonable, but I fear that we start to aliasing classes over and over due to having "simpler" autowire on parameter service definitions versus thinking about some better class design in most cases.
  13. Stefan Bürk voted on patch set 2 CR -1 01/10/25, 14:48

  14. Christian Kuhn commented on patch set 2 01/13/25, 17:05

    Reply

    hey. i agree there are different ways to model this and modeling extension configuration as DO may be a good idea for complex extensions with many configuration options. however, for simple things like retrieving a single value from a simple ext_conf_template.txt that contains only few options, modeling a DO feels like overkill to me. in such cases, a simplified injection of single compile time / configuration values looks like a straight alternative. as such, having an alias for ExtensionConfiguration does not hurt much, and i tend to follow the proposed path since it makes injection of configuration options to services that have to additional relevance at load and unload time of an extension (in non-composer mode) is fine with me. all in all, i tend to be fine with the proposed change: we will not simply add aliases to *all* classes, but in this case in can live with it: it's simple, it's useful, and the injected scalars are determined during compile-time in many cases. what do you think?
  15. Stefan Bürk commented on patch set 2 CR +1 V +1 01/13/25, 17:07

    Reply

    Talked with lolli about that and can see that adding the alias in this case is a useful helper for extension authors. Let's get the class alias in this case in.
  16. Stefan Bürk voted on patch set 2 CR 0 V 0 01/13/25, 17:09

  17. Christian Kuhn uploaded patch set 3 V +1 01/13/25, 17:14

  18. core-ci voted on patch set 3 V +1 01/13/25, 17:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/72372

  19. Christian Kuhn voted on patch set 3 CR +1 V +1 01/13/25, 17:31

  20. Stefan Bürk voted on patch set 3 CR +1 V +1 01/13/25, 17:32

  21. Benni Mack voted on patch set 3 CR +1 V +1 01/14/25, 16:18

  22. Stefan Bürk voted on patch set 3 CR +2 V +2 01/14/25, 22:59

  23. Stefan Bürk merged patch set 4 01/14/25, 23:07

  24. Benjamin Franzke commented on patch set 4 01/15/25, 07:19

    Reply

    I still don't get this argument. For the given example the alias does not need to be public and for testing we have the private container `$this->get()`. I'm still sure that `public: true` is not a good idea here and would appreciate if other would not ignore my previous -1 votes. Thanks.
  25. Christian Kuhn commented on patch set 4 01/21/25, 11:45

    Reply

    Hey Benjamin. I don't understand you feel ignored: We discussed your comment with three persons and came to the conclusion the alias is a good addition in this case. To my understanding and according to my testing, the alias must be public in order to be used as exemplified in the commit message. The example has nothing to do with testing, but is a solution aiming towards improved developer experience. Could you elaborate on your technical considerations if we missed something, please?