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

[RFC] Support decorators #149

Closed
BoShurik opened this issue Sep 16, 2020 · 5 comments
Closed

[RFC] Support decorators #149

BoShurik opened this issue Sep 16, 2020 · 5 comments

Comments

@BoShurik
Copy link

It would be great to have something like this:

return [
    ApiInterface::class => Reference::to(Api::class),
    CacheApi::class => Reference::decorate(ApiInterface::class, $priority = 0),
    // To log only real api calls
    'app.api.logged.calls' => Reference::decorate(ApiInterface::class, $priority = 10, [
        'context' => 'calls',
    ], LogApi::class), 
    // To log application api access
    'app.api.logged.access' => Reference::decorate(ApiInterface::class, $priority = -10, [
        'context' => 'access',
    ], LogApi::class), 
];
class CacheApi implements ApiInterface
{
    public function __construct(ApiInterface $decorated, CacheItemPoolInterface $cache)
    {
        //
    }
}
class LogApi implements ApiInterface
{
    public function __construct(ApiInterface $decorated, LoggerInterface $logger, string $context)
    {
        //
    }
}
$api = $container->get(ApiInterface::class); // LogApi (access) instance

Now it will be:

return [
    ApiInterface::class => function (ContainerInterface $container) {
        $logger = $container->get(LoggerInterface::class);
        $cache = $container->get(CacheItemPoolInterface::class);

        return new LogApi(
            new CacheApi(
                new LogApi(
                    $container->get(Api::class),
                    $logger,
                    'calls'
                ),
                $cache
            ),
            $logger,
            'access'
        );
    },
];

And it's impossible to register decorators by modules (e.g. debug decorators)

https://symfony.com/doc/current/service_container/service_decoration.html
https://php-di.org/doc/definition-overriding.html#decorators
https://laravel.com/docs/8.x/container#extending-bindings

@yiiliveext
Copy link
Contributor

@BoShurik
Copy link
Author

As I understand it will be impossible with reworked service providers

@samdark
Copy link
Member

samdark commented Aug 10, 2021

Implemented via service providers.

@samdark samdark reopened this Aug 10, 2021
@samdark
Copy link
Member

samdark commented Aug 10, 2021

@BoShurik please check if new service provider extensions are what you've meant.

@BoShurik
Copy link
Author

@samdark yes it is. Thanks.
As I understand you can't decorate service in one provider extension multiple times, but it's not that important

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

No branches or pull requests

3 participants