Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
Description
Symfony version(s) affected
7.*
Description
ChainAdapter
accepts list ofCacheItemPoolInterface
.
It makes me assuming that I can use it together with eitherAdapterInterface
or any external class implementingCacheItemPoolInterface
.
If I got it correct, I can do such thing in my DI asCacheItemPoolInterface
:
newChainAdapter( adapters: [new \Symfony\Component\Cache\Adapter\ArrayAdapter(storeSerialized:false), YiiHelper::get(\Yii2Extended\Yii2Cache\Psr6ToYii2Cache::class),// it implements CacheItemPoolInterface ],);
But I can't. It leads to the TypeError (see below in Additonal context).
How to reproduce
I've created a minimal script to reproduce. it's attached.
reproducer.php.txt
Here's the text of the script, if that's more convenient.
<?phprequire_once__DIR__ .'/vendor/autoload.php';useSymfony\Component\Cache\Adapter\ChainAdapter;useYii2Extended\Yii2Cache\Psr6ToYii2Cache;$chainAdapter =newChainAdapter( adapters: [new \Symfony\Component\Cache\Adapter\ArrayAdapter(storeSerialized:false),newPsr6ToYii2Cache(new \yii\caching\ArrayCache()), ],);$cacheKey ='foo';$item =$chainAdapter->getItem($cacheKey);if ($item->isHit()) {echo'isHit',"\n";echo$item->get(),"\n";}$item->set('bar');echo'before save',"\n";$chainAdapter->save($item);echo'after save',"\n";
In place of thePsr6ToYii2Cache
can be any external (i.e., not implementing the\Symfony\Component\Cache\Adapter\AdapterInterface
) class that implements theCacheItemPoolInterface
.
Possible Solution
I've fixed it locally by such fix:vendor/symfony/cache/CacheItem.php
- protected ?ItemInterface$innerItem =null;+ protected ?\Psr\Cache\CacheItemInterface$innerItem = null;
In my view it's adequate: as long asChainAdapter
acceptsCacheItemPoolInterface
, it's normal thatinnerItem
should be ofCacheItemInterface
.
Additional Context
reproducer.php
without fix leads to such error message:
PHP Fatal error: Uncaught TypeError: Cannot assign Yii2Extended\Yii2Cache\Psr6ToYii2CacheItem to property Symfony\Component\Cache\CacheItem::$innerItem of type ?Symfony\Contracts\Cache\ItemInterface in /code/vendor/symfony/cache/Adapter/ProxyAdapter.php:60