DASPRiD / container-interop-doctrine

Doctrine factories for container-interop
107 stars 25 forks source link

[Feature] Allow using cosutm cache providers. #36

Closed azjezz closed 6 years ago

azjezz commented 6 years ago

this will allow using custom cache providers, i made a cache provider bridge that uses a Psr\Cache\CacheItemPoolInterface, and it needs the pool as it first argument, its not a problem since i have a factory but it seems to be a problem here as the factory creates a new instance without providing any arguments.

azjezz commented 6 years ago

should i add a CHANGELOG.md file ?

azjezz commented 6 years ago

fixed tests.

azjezz commented 6 years ago

@DASPRiD is there anything else is should add here ? i really hope this gets merged, i had to do a really nasty workaround to get it to work for me

config :

     'cache_provider' => [
         'class' => CacheBridge::class,
         'instance' => CacheBridge::class,
     ],

cache provider :

    /**
     * CacheBridge constructor.
     *
     * @param CacheItemPoolInterface|null $cachePool
     */
    public function __construct(CacheItemPoolInterface $cachePool = null)
    {
        if (null === $cachePool) {
            if (null !== static::$cachePoolFactory) {
                $this->cachePool = (static::$cachePoolFactory)();
            } else {
                throw new RuntimeException('...');
            }
        } else {
            $this->cachePool = $cachePool;
        }
    }

factory :

class CacheBridgeFactory
{
    public function __invoke(ContainerInterface $container): CacheBridge
    {
        CacheBridge::setCachePoolFactory(function () use ($container) {
            return $container->get(CacheItemPoolInterface::class);
        });

        return new CacheBridge();
    }
}

since the instance is looked up in the container, it triggers the cache provider factory to set the cache pool factory

asgrim commented 6 years ago

Thanks @azjezz