44
55use BadMethodCallException ;
66use Doctrine \Common \Persistence \ManagerRegistry as ManagerRegistryInterface ;
7+ use Psr \Cache \CacheItemPoolInterface ;
78use Redis ;
89use Memcache ;
910use Memcached ;
1213use Doctrine \ORM \Tools \Setup ;
1314use Kayue \WordpressBundle \Doctrine \WordpressEntityManager ;
1415use Kayue \WordpressBundle \WordpressEvents ;
16+ use Symfony \Component \Cache \Adapter \AdapterInterface ;
17+ use Symfony \Component \Cache \Adapter \ProxyAdapter ;
18+ use Symfony \Component \Cache \DoctrineProvider ;
1519use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
1620use 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 ()
0 commit comments