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

Add maker for decorate services #1401

Open
alexander-schranz opened this issue Nov 23, 2023 · 0 comments
Open

Add maker for decorate services #1401

alexander-schranz opened this issue Nov 23, 2023 · 0 comments

Comments

@alexander-schranz
Copy link

alexander-schranz commented Nov 23, 2023

Most people go over inheritance instead of composition when overwriting services. But the best way would be always go with composition when decorate the service. A maker make:service-decorator could automatically detect the interfaces which a decorated service implements and use so the composition when decorating services and call the inner service e.g.:

class SomeService implements SomeInterface
{
    public function someMethod(): string
    {
        return 'test';
    }

    public function some2Method(): string
    {
    }
}

interface SomeInterface {
    public function someMethod(): string;
    public function some2Method(): void;
}

Calling bin/console make:service-decorator does:

#[AsDecorator(decorates: SomeService::class)]
class DecoratorService implements SomeInterface
{
    public function __construct(private #[MapDecorated] SomeInterface $inner)
    {}

    public function someMethod(): string
    {
         return $this->inner->someMethod();
    }

    public function someMethod(): void
    {
        $this->inner->some2Method();
    }
}

An idea i just came to my mind while helping somebody decorate a service correctly, one common issue is also that AsDecorator requires the service id and people are used to write a class in it, there a maker could help with Did you mean service .... In this specific case it was a twig extra extension which only has a service id and not a public class alias in the container.

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

No branches or pull requests

1 participant