[Feature Request] [Config] Simplify service extending services with other services
Description
I'm very new to Spiral and looking how I can make my Spiral Modules extendable. I had a look at the following Config code in the Twig Bridge here:
https://github.com/spiral/twig-bridge/blob/0463fab97cdbe2caaeaf41f38c197a372660a22b/src/Config/TwigConfig.php#L32-L41
At the wire part here:
https://github.com/spiral/twig-bridge/blob/0463fab97cdbe2caaeaf41f38c197a372660a22b/src/Config/TwigConfig.php#L62-L76
This is something which maybe could be easier as extending a package with something from outside via own is a code is common pattern, which even some design patterns are build on top of it:
In Symfony service tags can be used to mark services with a tag which will then automatically be injected.
In Spiral maybe if service tags are not a way to go, I understand that, but maybe the exist code maybe could be moved into a HelperTrait or Helper Function. So that kind of mechanism of that lines could be more simplified and is provided by the Framework and not all need to copy it to there modules.
Example
Could the TwigConfig could look like this:
final class TwigConfig extends InjectableConfig
{
public const CONFIG = 'views/twig';
protected array $config = [
'options' => [],
'extensions' => [],
'processors' => [],
];
public function getOptions(): array
{
return $this->config['options'];
}
/**
* @return Autowire[]
*/
public function getExtensions(): array
{
return ServiceHelper::loadServices($this->config['extensions']);
}
/**
* @return Autowire[]
*/
public function getProcessors(): array
{
return ServiceHelper::loadServices($this->config['processors']);
}
}
Hi @alexander-schranz ,
By design and Single responsibility principe Config object should just provide data for services. If you want to register extensions and processors you need to use something like ExtensionsRegistry and ProcessorsRegistry and pass data from config there.
@butschster thx for your response. Is there simpler example I already have a service which is the Registry. But I think I still need this logic also in my case:
- a way how people can add something: https://github.com/spiral/twig-bridge/blob/0463fab97cdbe2caaeaf41f38c197a372660a22b/src/Bootloader/TwigBootloader.php#L53-L56
- add in the Config that the added service are correctly wired: https://github.com/spiral/twig-bridge/blob/0463fab97cdbe2caaeaf41f38c197a372660a22b/src/Config/TwigConfig.php#L32-L41
- and that the edited servicer are then added to my registry: https://github.com/spiral/twig-bridge/blob/0463fab97cdbe2caaeaf41f38c197a372660a22b/src/Bootloader/TwigBootloader.php#L69-L75
Is that correct? I just thought maybe there could maybe something improved to make things here easier as it seems to be common pattern.