8000 [DI] Reintroduce removed closure-proxy as callable type · Issue #23454 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[DI] Reintroduce removed closure-proxy as callable type #23454
Closed
@ro0NL

Description

@ro0NL
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.4

I'd like to reintroduce the removed closure proxy type, in a simplified manner, to provide laziness without injecting a container.

A callable type; it came to mind facing the following use case, which imo. ideally i'd write as;

My\CachePoolManager:
    arguments: [!callable memory_cache_factory] # proposed feature used here

memory_cache_factory:
    class: Symfony\Component\Cache\Adapter\ArrayAdapter
    arguments: [0, false]
    shared: false # plays well with this feature :)
    public: false
namespace My;
final class CachePoolManager {
    private $cacheFactory;
    private static $pools = [];

    public function __construct(callable $cacheFactory) {
        $this->cacheFactory = $cacheFactory;
    }

    public function getForKey(string $key): \Psr\Cache\CacheItemPoolInterface {
        return self::$pools[$key] ?? (self::$pools[$key] = ($this->cacheFactory)());
    }

5E4E
}

Note this becomes close related to service_locators, but i cannot create services per $key as these are runtime related.

The "workaround" would be to inject a service locator containing the memory_cache_factory creating a dependency between class and service id ($this->container->get('memory_cache_factory'); which in turn can be injected as well making things look weird.

This keeps everything at the config level.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0