spiral / spiral/framework

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

Open
#830 2 comments 0 reactions 1 assignee View on GitHub

@butschster is already working on this.

Since Nov 11, 2022.

Feature
Dominant language
PHP
Stars
2.1k
Forks
92
Avg merge
14h 57m
Merged PRs (30d)
4

Description

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']);
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.