Skip to content

Commit 68d442a

Browse files
committed
Switch cache configuration to PSR-6
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent 01ab70d commit 68d442a

19 files changed

+244
-116
lines changed

lib/Doctrine/ORM/AbstractQuery.php

+29-11
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,15 @@ public function setHydrationCacheProfile(?QueryCacheProfile $profile = null)
543543
// DBAL < 3.2
544544
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
545545
if (! $profile->getResultCacheDriver()) {
546-
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCacheImpl();
546+
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
547547
if ($defaultHydrationCacheImpl) {
548-
$profile = $profile->setResultCacheDriver($defaultHydrationCacheImpl);
548+
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultHydrationCacheImpl));
549549
}
550550
}
551551
} elseif (! $profile->getResultCache()) {
552-
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCacheImpl();
552+
$defaultHydrationCacheImpl = $this->_em->getConfiguration()->getHydrationCache();
553553
if ($defaultHydrationCacheImpl) {
554-
$profile = $profile->setResultCache(CacheAdapter::wrap($defaultHydrationCacheImpl));
554+
$profile = $profile->setResultCache($defaultHydrationCacheImpl);
555555
}
556556
}
557557

@@ -587,9 +587,9 @@ public function setResultCacheProfile(?QueryCacheProfile $profile = null)
587587
// DBAL < 3.2
588588
if (! method_exists(QueryCacheProfile::class, 'setResultCache')) {
589589
if (! $profile->getResultCacheDriver()) {
590-
$defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl();
590+
$defaultResultCacheDriver = $this->_em->getConfiguration()->getResultCache();
591591
if ($defaultResultCacheDriver) {
592-
$profile = $profile->setResultCacheDriver($defaultResultCacheDriver);
592+
$profile = $profile->setResultCacheDriver(DoctrineProvider::wrap($defaultResultCacheDriver));
593593
}
594594
}
595595
} elseif (! $profile->getResultCache()) {
@@ -727,15 +727,33 @@ public function disableResultCache(): self
727727
*
728728
* @param int|null $lifetime How long the cache entry is valid, in seconds.
729729
*
730-
* @return static This query instance.
730+
* @return $this
731731
*/
732732
public function setResultCacheLifetime($lifetime)
733733
{
734-
$lifetime = $lifetime !== null ? (int) $lifetime : 0;
734+
$lifetime = (int) $lifetime;
735735

736-
$this->_queryCacheProfile = $this->_queryCacheProfile
737-
? $this->_queryCacheProfile->setLifetime($lifetime)
738-
: new QueryCacheProfile($lifetime, null, $this->_em->getConfiguration()->getResultCacheImpl());
736+
if ($this->_queryCacheProfile) {
737+
$this->_queryCacheProfile = $this->_queryCacheProfile->setLifetime($lifetime);
738+
739+
return $this;
740+
}
741+
742+
$this->_queryCacheProfile = new QueryCacheProfile($lifetime);
743+
744+
$cache = $this->_em->getConfiguration()->getResultCache();
745+
if (! $cache) {
746+
return $this;
747+
}
748+
749+
// Compatibility for DBAL < 3.2
750+
if (! method_exists($this->_queryCacheProfile, 'setResultCache')) {
751+
$this->_queryCacheProfile = $this->_queryCacheProfile->setResultCacheDriver(DoctrineProvider::wrap($cache));
752+
753+
return $this;
754+
}
755+
756+
$this->_queryCacheProfile = $this->_queryCacheProfile->setResultCache($cache);
739757

740758
return $this;
741759
}

lib/Doctrine/ORM/Configuration.php

+97-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Doctrine\ORM\Exception\InvalidEntityRepository;
2424
use Doctrine\ORM\Exception\NamedNativeQueryNotFound;
2525
use Doctrine\ORM\Exception\NamedQueryNotFound;
26-
use Doctrine\ORM\Exception\ORMException;
2726
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
2827
use Doctrine\ORM\Exception\UnknownEntityNamespace;
2928
use Doctrine\ORM\Mapping\ClassMetadataFactory;
@@ -43,6 +42,7 @@
4342
use ReflectionClass;
4443

4544
use function class_exists;
45+
use function method_exists;
4646
use function strtolower;
4747
use function trim;
4848

@@ -251,46 +251,142 @@ public function getMetadataDriverImpl()
251251
return $this->_attributes['metadataDriverImpl'] ?? null;
252252
}
253253

254+
/**
255+
* Gets the cache driver implementation that is used for query result caching.
256+
*/
257+
public function getResultCache(): ?CacheItemPoolInterface
258+
{
259+
// Compatibility with DBAL < 3.2
260+
if (! method_exists(parent::class, 'getResultCache')) {
261+
$cacheImpl = $this->getResultCacheImpl();
262+
263+
return $cacheImpl ? CacheAdapter::wrap($cacheImpl) : null;
264+
}
265+
266+
return parent::getResultCache();
267+
}
268+
269+
/**
270+
* Sets the cache driver implementation that is used for query result caching.
271+
*/
272+
public function setResultCache(CacheItemPoolInterface $cache): void
273+
{
274+
// Compatibility with DBAL < 3.2
275+
if (! method_exists(parent::class, 'setResultCache')) {
276+
$this->setResultCacheImpl(DoctrineProvider::wrap($cache));
277+
278+
return;
279+
}
280+
281+
parent::setResultCache($cache);
282+
}
283+
254284
/**
255285
* Gets the cache driver implementation that is used for the query cache (SQL cache).
256286
*
287+
* @deprecated Call {@see getQueryCache()} instead.
288+
*
257289
* @return CacheDriver|null
258290
*/
259291
public function getQueryCacheImpl()
260292
{
293+
Deprecation::trigger(
294+
'doctrine/orm',
295+
'https://github.com/doctrine/orm/pull/9002',
296+
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use getQueryCache() instead.',
297+
__METHOD__
298+
);
299+
261300
return $this->_attributes['queryCacheImpl'] ?? null;
262301
}
263302

264303
/**
265304
* Sets the cache driver implementation that is used for the query cache (SQL cache).
266305
*
306+
* @deprecated Call {@see setQueryCache()} instead.
307+
*
267308
* @return void
268309
*/
269310
public function setQueryCacheImpl(CacheDriver $cacheImpl)
270311
{
312+
Deprecation::trigger(
313+
'doctrine/orm',
314+
'https://github.com/doctrine/orm/pull/9002',
315+
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use setQueryCache() instead.',
316+
__METHOD__
317+
);
318+
319+
$this->_attributes['queryCache'] = CacheAdapter::wrap($cacheImpl);
271320
$this->_attributes['queryCacheImpl'] = $cacheImpl;
272321
}
273322

323+
/**
324+
* Gets the cache driver implementation that is used for the query cache (SQL cache).
325+
*/
326+
public function getQueryCache(): ?CacheItemPoolInterface
327+
{
328+
return $this->_attributes['queryCache'] ?? null;
329+
}
330+
331+
/**
332+
* Sets the cache driver implementation that is used for the query cache (SQL cache).
333+
*/
334+
public function setQueryCache(CacheItemPoolInterface $cache): void
335+
{
336+
$this->_attributes['queryCache'] = $cache;
337+
$this->_attributes['queryCacheImpl'] = DoctrineProvider::wrap($cache);
338+
}
339+
274340
/**
275341
* Gets the cache driver implementation that is used for the hydration cache (SQL cache).
276342
*
343+
* @deprecated Call {@see getHydrationCache()} instead.
344+
*
277345
* @return CacheDriver|null
278346
*/
279347
public function getHydrationCacheImpl()
280348
{
349+
Deprecation::trigger(
350+
'doctrine/orm',
351+
'https://github.com/doctrine/orm/pull/9002',
352+
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use getHydrationCache() instead.',
353+
__METHOD__
354+
);
355+
281356
return $this->_attributes['hydrationCacheImpl'] ?? null;
282357
}
283358

284359
/**
285360
* Sets the cache driver implementation that is used for the hydration cache (SQL cache).
286361
*
362+
* @deprecated Call {@see setHydrationCache()} instead.
363+
*
287364
* @return void
288365
*/
289366
public function setHydrationCacheImpl(CacheDriver $cacheImpl)
290367
{
368+
Deprecation::trigger(
369+
'doctrine/orm',
370+
'https://github.com/doctrine/orm/pull/9002',
371+
'Method %s() is deprecated and will be removed in Doctrine ORM 3.0. Use setHydrationCache() instead.',
372+
__METHOD__
373+
);
374+
375+
$this->_attributes['hydrationCache'] = CacheAdapter::wrap($cacheImpl);
291376
$this->_attributes['hydrationCacheImpl'] = $cacheImpl;
292377
}
293378

379+
public function getHydrationCache(): ?CacheItemPoolInterface
380+
{
381+
return $this->_attributes['hydrationCache'] ?? null;
382+
}
383+
384+
public function setHydrationCache(CacheItemPoolInterface $cache): void
385+
{
386+
$this->_attributes['hydrationCache'] = $cache;
387+
$this->_attributes['hydrationCacheImpl'] = DoctrineProvider::wrap($cache);
388+
}
389+
294390
/**
295391
* Gets the cache driver implementation that is used for metadata caching.
296392
*

lib/Doctrine/ORM/Query.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ORM;
66

77
use Doctrine\Common\Cache\Cache;
8+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
89
use Doctrine\Common\Collections\ArrayCollection;
910
use Doctrine\DBAL\Cache\QueryCacheProfile;
1011
use Doctrine\DBAL\LockMode;
@@ -497,7 +498,9 @@ public function getQueryCacheDriver(): ?Cache
497498
return $this->queryCache;
498499
}
499500

500-
return $this->_em->getConfiguration()->getQueryCacheImpl();
501+
$queryCache = $this->_em->getConfiguration()->getQueryCache();
502+
503+
return $queryCache ? DoctrineProvider::wrap($queryCache) : null;
501504
}
502505

503506
/**

lib/Doctrine/ORM/Tools/Console/Command/ClearCache/QueryCommand.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand;
1212
use InvalidArgumentException;
1313
use LogicException;
14+
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Input\InputOption;
1617
use Symfony\Component\Console\Output\OutputInterface;
@@ -65,18 +66,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
6566
$ui = new SymfonyStyle($input, $output);
6667

6768
$em = $this->getEntityManager($input);
69+
$cache = $em->getConfiguration()->getQueryCache();
6870
$cacheDriver = $em->getConfiguration()->getQueryCacheImpl();
6971

7072
if (! $cacheDriver) {
7173
throw new InvalidArgumentException('No Query cache driver is configured on given EntityManager.');
7274
}
7375

74-
if ($cacheDriver instanceof ApcCache) {
75-
throw new LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
76+
if ($cacheDriver instanceof ApcCache || $cache instanceof ApcuAdapter) {
77+
throw new LogicException('Cannot clear APCu Cache from Console, it\'s shared in the Webserver memory and not accessible from the CLI.');
7678
}
7779

7880
if ($cacheDriver instanceof XcacheCache) {
79-
throw new LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
81+
throw new LogicException('Cannot clear XCache Cache from Console, it\'s shared in the Webserver memory and not accessible from the CLI.');
8082
}
8183

8284
if (! ($cacheDriver instanceof ClearableCache)) {
@@ -88,10 +90,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8890

8991
$ui->comment('Clearing <info>all</info> Query cache entries');
9092

91-
$result = $cacheDriver->deleteAll();
93+
$result = $cache ? $cache->clear() : $cacheDriver->deleteAll();
9294
$message = $result ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
9395

94-
if ($input->getOption('flush') === true) {
96+
if ($input->getOption('flush') === true && ! $cache) {
9597
if (! ($cacheDriver instanceof FlushableCache)) {
9698
throw new LogicException(sprintf(
9799
'Can only clear cache when FlushableCache interface is implemented, %s does not implement.',

lib/Doctrine/ORM/Tools/Console/Command/ClearCache/ResultCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6868
$ui = new SymfonyStyle($input, $output);
6969

7070
$em = $this->getEntityManager($input);
71-
$cache = method_exists(Configuration::class, 'getResultCache') ? $em->getConfiguration()->getResultCache() : null;
71+
$cache = $em->getConfiguration()->getResultCache();
7272
$cacheDriver = method_exists(Configuration::class, 'getResultCacheImpl') ? $em->getConfiguration()->getResultCacheImpl() : null;
7373

7474
if (! $cacheDriver && ! $cache) {

lib/Doctrine/ORM/Tools/Setup.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public static function createConfiguration($isDevMode = false, $proxyDir = null,
124124
$config = new Configuration();
125125

126126
$config->setMetadataCache(CacheAdapter::wrap($cache));
127-
$config->setQueryCacheImpl($cache);
128-
$config->setResultCacheImpl($cache);
127+
$config->setQueryCache(CacheAdapter::wrap($cache));
128+
$config->setResultCache(CacheAdapter::wrap($cache));
129129
$config->setProxyDir($proxyDir);
130130
$config->setProxyNamespace('DoctrineProxies');
131131
$config->setAutoGenerateProxyClasses($isDevMode);

phpstan-dbal2.neon

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ parameters:
1212
- '/Class Doctrine\\DBAL\\Platforms\\PostgreSqlPlatform referenced with incorrect case: Doctrine\\DBAL\\Platforms\\PostgreSQLPlatform\./'
1313

1414
# Forward compatibility for DBAL 3.2
15-
- '/^Call to an undefined method Doctrine\\.*::[gs]etResultCache\(\)\.$/'
15+
- '/^Call to an undefined method Doctrine\\DBAL\\Cache\\QueryCacheProfile::[gs]etResultCache\(\)\.$/'
16+
-
17+
message: '/^Call to an undefined static method Doctrine\\DBAL\\Configuration::[gs]etResultCache\(\)\.$/'
18+
path: lib/Doctrine/ORM/Configuration.php
1619
-
1720
message: '/^Parameter #3 \$resultCache of class Doctrine\\DBAL\\Cache\\QueryCacheProfile constructor/'
1821
path: lib/Doctrine/ORM/AbstractQuery.php

phpstan.neon

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ parameters:
3131
path: lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
3232

3333
# Forward compatibility for DBAL 3.2
34-
- '/^Call to an undefined method Doctrine\\.*::[gs]etResultCache\(\)\.$/'
34+
- '/^Call to an undefined method Doctrine\\DBAL\\Cache\\QueryCacheProfile::[gs]etResultCache\(\)\.$/'
35+
-
36+
message: '/^Call to an undefined static method Doctrine\\DBAL\\Configuration::[gs]etResultCache\(\)\.$/'
37+
path: lib/Doctrine/ORM/Configuration.php
3538
-
3639
message: '/^Parameter #3 \$resultCache of class Doctrine\\DBAL\\Cache\\QueryCacheProfile constructor/'
3740
path: lib/Doctrine/ORM/AbstractQuery.php

0 commit comments

Comments
 (0)