Skip to content

Commit 532cf9e

Browse files
committed
Symfony 4 cache pool support
1 parent 97c70f5 commit 532cf9e

File tree

6 files changed

+43
-92
lines changed

6 files changed

+43
-92
lines changed

DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function getConfigTreeBuilder()
2828
->scalarNode('cookie_domain')->defaultValue(null)->end()
2929
->scalarNode('table_prefix')->defaultValue('wp_')->end()
3030
->scalarNode('connection')->defaultValue('default')->end()
31+
->arrayNode('orm')->children()
32+
->scalarNode('metadata_cache_pool')->defaultValue('cache.system')->end()
33+
->scalarNode('query_cache_pool')->defaultValue('cache.app')->end()
34+
->scalarNode('result_cache_pool')->defaultValue('cache.app')->end()
35+
->end()
3136
->end();
3237

3338
return $treeBuilder;

DependencyInjection/KayueWordpressExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ public function load(array $configs, ContainerBuilder $container)
3030
$loader->load('services.yml');
3131

3232
$container->setAlias('kayue_wordpress.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
33+
$container->setAlias('kayue_wordpress.orm.metadata_cache.pool', $config['orm']['metadata_cache_pool']);
34+
$container->setAlias('kayue_wordpress.orm.result_cache.pool', $config['orm']['result_cache_pool']);
35+
$container->setAlias('kayue_wordpress.orm.query_cache.pool', $config['orm']['query_cache_pool']);
3336
}
3437
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ I started that bundle two years ago and the original repository grew somewhat ch
1515
* Twig extension (v1.1.0)
1616
* WordPress style shortcode (v1.1.0)
1717
* Major code update. (v2.0.0)
18+
* Support Symfony 4, new cache configuration (v4.0.0)
1819

1920
#### Todo
2021

@@ -73,6 +74,12 @@ kayue_wordpress:
7374

7475
# Doctrine connection to use. Default is 'default'.
7576
connection: 'default'
77+
78+
# Specify Symfony cache pool
79+
orm:
80+
metadata_cache_pool: cache.system
81+
query_cache_pool: cache.app
82+
result_cache_pool: cache.app
7683

7784
# The following configuration only needed only when you use WordPress authentication.
7885

Resources/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ services:
2020
- '@event_dispatcher'
2121
- '%kernel.root_dir%'
2222
- '%kernel.environment%'
23+
- '@kayue_wordpress.orm.metadata_cache.pool'
24+
- '@kayue_wordpress.orm.result_cache.pool'
25+
- '@kayue_wordpress.orm.query_cache.pool'
2326

2427
kayue_wordpress.helper.attachment:
2528
class: Kayue\WordpressBundle\Wordpress\Helper\AttachmentHelper

Wordpress/ManagerRegistry.php

Lines changed: 24 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BadMethodCallException;
66
use Doctrine\Common\Persistence\ManagerRegistry as ManagerRegistryInterface;
7+
use Psr\Cache\CacheItemPoolInterface;
78
use Redis;
89
use Memcache;
910
use Memcached;
@@ -12,6 +13,9 @@
1213
use Doctrine\ORM\Tools\Setup;
1314
use Kayue\WordpressBundle\Doctrine\WordpressEntityManager;
1415
use Kayue\WordpressBundle\WordpressEvents;
16+
use Symfony\Component\Cache\Adapter\AdapterInterface;
17+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
18+
use Symfony\Component\Cache\DoctrineProvider;
1519
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1620
use Symfony\Component\EventDispatcher\GenericEvent;
1721

@@ -38,19 +42,29 @@ class ManagerRegistry implements ManagerRegistryInterface
3842
protected $previousBlogId = 1;
3943
protected $managers = [];
4044

45+
private $metaCache;
46+
private $queryCache;
47+
private $resultCache;
48+
4149
public function __construct(
4250
Connection $connection,
4351
EntityManager $defaultEntityManager,
4452
EventDispatcherInterface $eventDispatcher,
4553
$rootDir,
46-
$environment
54+
$environment,
55+
AdapterInterface $metadataCache,
56+
AdapterInterface $queryCache,
57+
AdapterInterface $resultCache
4758
)
4859
{
4960
$this->connection = $connection;
5061
$this->defaultEntityManager = $defaultEntityManager;
5162
$this->eventDispatcher = $eventDispatcher;
5263
$this->rootDir = $rootDir;
5364
$this->environment = $environment;
65+
$this->metadataCache = $metadataCache;
66+
$this->queryCache = $queryCache;
67+
$this->resultCache = $resultCache;
5468
}
5569

5670
/**
@@ -75,9 +89,10 @@ public function getManager($blogId = null)
7589
$this->eventDispatcher->dispatch(WordpressEvents::CREATE_ENTITY_MANAGER, new GenericEvent($em));
7690

7791
$em->setBlogId($this->currentBlogId);
78-
$em->getMetadataFactory()->setCacheDriver($this->getCacheImpl('metadata_cache', $this->currentBlogId));
79-
$em->getConfiguration()->setQueryCacheImpl($this->getCacheImpl('query_cache', $this->currentBlogId));
80-
$em->getConfiguration()->setResultCacheImpl($this->getCacheImpl('result_cache', $this->currentBlogId));
92+
93+
$em->getMetadataFactory()->setCacheDriver($this->getCacheProvider($this->metadataCache, $this->currentBlogId));
94+
$em->getConfiguration()->setQueryCacheImpl($this->getCacheProvider($this->queryCache, $this->currentBlogId));
95+
$em->getConfiguration()->setResultCacheImpl($this->getCacheProvider($this->resultCache, $this->currentBlogId));
8196

8297
$this->managers[$this->currentBlogId] = $em;
8398
}
@@ -108,95 +123,13 @@ public function restorePreviousBlog()
108123
$this->setCurrentBlogId($this->previousBlogId);
109124
}
110125

111-
/**
112-
* Loads a configured object manager metadata, query or result cache driver.
113-
*
114-
* @param string $cacheName
115-
*
116-
* @param $blogId
117-
* @throws \InvalidArgumentException In case of unknown driver type.
118-
* @return \Doctrine\Common\Cache\Cache
119-
*/
120-
protected function getCacheImpl($cacheName, $blogId)
126+
protected function getCacheProvider(CacheItemPoolInterface $pool, $blogId)
121127
{
122-
$config = $this->defaultEntityManager->getConfiguration();
123-
124-
switch ($cacheName) {
125-
case 'metadata_cache':
126-
$baseCache = $config->getMetadataCacheImpl();
127-
break;
128-
case 'query_cache':
129-
$baseCache = $config->getQueryCacheImpl();
130-
break;
131-
case 'result_cache':
132-
$baseCache = $config->getResultCacheImpl();
133-
break;
134-
default:
135-
throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Doctrine cache name.
136-
Supported cache names are: "metadata_cache", "query_cache" and "result_cache"', $cacheName));
137-
}
138-
139-
$namespace = 'sf2_kayue_wordpress_bundle_blog_'.$blogId.'_'.md5($this->rootDir.$this->environment);
140-
141-
$className = get_class($baseCache);
142-
143-
switch ($className) {
144-
case 'Doctrine\Common\Cache\ApcCache':
145-
case 'Doctrine\Common\Cache\ApcuCache':
146-
case 'Doctrine\Common\Cache\ArrayCache':
147-
case 'Doctrine\Common\Cache\XcacheCache':
148-
case 'Doctrine\Common\Cache\WinCacheCache':
149-
case 'Doctrine\Common\Cache\ZendDataCache':
150-
$cache = new $className();
151-
break;
152-
case 'Doctrine\Common\Cache\MemcacheCache':
153-
$memcache = $baseCache->getMemcache();
154-
$rawStats = $memcache->getExtendedStats();
155-
$servers = array_keys($rawStats);
156-
157-
$cache = new $className();
158-
$newMemcache = new Memcache();
159-
160-
foreach ($servers as $server) {
161-
$host = substr($server, 0, strpos($server, ':'));
162-
$port = substr($server, strpos($server, ':') + 1);
163-
$newMemcache->connect($host, $port);
164-
}
165-
166-
$cache->setMemcache($newMemcache);
167-
break;
168-
case 'Doctrine\Common\Cache\MemcachedCache':
169-
$memcached = $baseCache->getMemcached();
170-
$servers = $memcached->getServerList();
171-
172-
$cache = new $className();
173-
$newMemcached = new Memcached();
174-
175-
foreach ($servers as $server) {
176-
$newMemcached->connect($server['host'], $server['port']);
177-
}
178-
179-
$cache->setMemcached($newMemcached);
180-
break;
181-
case 'Doctrine\Common\Cache\RedisCache':
182-
$redis = $baseCache->getRedis();
183-
$host = $redis->getHost();
184-
$port = $redis->getPort();
185-
186-
$cache = new $className();
187-
188-
$newRedis = new Redis();
189-
$newRedis->connect($host, $port);
190-
191-
$cache->setRedis($newRedis);
192-
break;
193-
default:
194-
throw new \InvalidArgumentException(sprintf('Unknown or unsupported cache type class in configuration: "%s"', get_class($baseCache)));
195-
}
196-
197-
$cache->setNamespace($namespace);
128+
$namespace = sprintf('wordpress_blog_%s_', $blogId);
129+
$proxyAdapter = new ProxyAdapter($pool, $namespace);
130+
$doctrineCache = new DoctrineProvider($proxyAdapter);
198131

199-
return $cache;
132+
return $doctrineCache;
200133
}
201134

202135
public function getDefaultConnectionName()

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
"require": {
1212
"php": "^5.5.9|~7.0",
13-
"symfony/framework-bundle": "^2.7|^3.0|^4.0|^5.0",
13+
"symfony/framework-bundle": "^4.0|^5.0",
1414
"doctrine/orm": "~2.2,>=2.2.3"
1515
},
1616
"require-dev": {

0 commit comments

Comments
 (0)