Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] [Config] Simplify service extending services with other services #830

Open
alexander-schranz opened this issue Nov 10, 2022 · 2 comments
Assignees
Labels

Comments

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Nov 10, 2022

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']);
    }
}
@butschster
Copy link
Member

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.

@alexander-schranz
Copy link
Contributor Author

alexander-schranz commented Nov 12, 2022

@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:

Is that correct? I just thought maybe there could maybe something improved to make things here easier as it seems to be common pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

2 participants