Просмотр исходного кода

ZF-10049 Zend_App | Allow to initialize Zend_Db_Table_Abstract metadata cache for the resource multidb | Patch by Benoit Durand

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22546 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak 15 лет назад
Родитель
Сommit
ee5f1970c8

+ 39 - 0
library/Zend/Application/Resource/Multidb.php

@@ -32,6 +32,8 @@ require_once 'Zend/Db/Table.php';
  *
  * Example configuration:
  * <pre>
+ *   resources.multidb.defaultMetadataCache = "database"
+ *
  *   resources.multidb.db1.adapter = "pdo_mysql"
  *   resources.multidb.db1.host = "localhost"
  *   resources.multidb.db1.username = "webuser"
@@ -77,6 +79,11 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
     {
         $options = $this->getOptions();
 
+        if (isset($options['defaultMetadataCache'])) {
+            $this->_setDefaultMetadataCache($options['defaultMetadataCache']);
+            unset($options['defaultMetadataCache']);
+        }
+
         foreach ($options as $id => $params) {
         	$adapter = $params['adapter'];
             $default = (int) (
@@ -168,4 +175,36 @@ class Zend_Application_Resource_Multidb extends Zend_Application_Resource_Resour
         Zend_Db_Table::setDefaultAdapter($adapter);
         $this->_defaultDb = $adapter;
     }
+
+   /**
+     * Set the default metadata cache
+     * 
+     * @param string|Zend_Cache_Core $cache
+     * @return Zend_Application_Resource_Multidb
+     */
+    protected function _setDefaultMetadataCache($cache)
+    {
+        $metadataCache = null;
+
+        if (is_string($cache)) {
+            $bootstrap = $this->getBootstrap();
+            if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper &&
+                $bootstrap->hasPluginResource('CacheManager')
+            ) {
+                $cacheManager = $bootstrap->bootstrap('CacheManager')
+                    ->getResource('CacheManager');
+                if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
+                    $metadataCache = $cacheManager->getCache($cache);
+                }
+            }
+        } else if ($cache instanceof Zend_Cache_Core) {
+            $metadataCache = $cache;
+        }
+
+        if ($metadataCache instanceof Zend_Cache_Core) {
+            Zend_Db_Table::setDefaultMetadataCache($metadataCache);
+        }
+
+        return $this;
+    }
 }

+ 45 - 0
tests/Zend/Application/Resource/MultidbTest.php

@@ -82,6 +82,7 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
     public function tearDown()
     {
         Zend_Db_Table::setDefaultAdapter(null);
+        Zend_Db_Table::setDefaultMetadataCache();
 
         // Restore original autoloaders
         $loaders = spl_autoload_functions();
@@ -198,7 +199,51 @@ class Zend_Application_Resource_MultidbTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($expected, $res->getDb('db2')->getConfig());
     }
 
+    /**
+     * @group ZF-10049
+     */
+    public function testSetDefaultMetadataCache()
+    {
+        $cache = Zend_Cache::factory('Core', 'Black Hole', array(
+            'lifetime' => 120,
+            'automatic_serialization' => true
+        ));
 
+        $options = $this->_dbOptions;
+        $options['defaultMetadataCache'] = $cache;
+        $resource = new Zend_Application_Resource_Multidb($options);
+        $resource->init();
+        $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
+    }
+
+    /**
+     * @group ZF-10049
+     */
+    public function testSetDefaultMetadataCacheFromCacheManager()
+    {
+        $configCache = array(
+            'database' => array(
+                'frontend' => array(
+                    'name' => 'Core',
+                    'options' => array(
+                        'lifetime' => 120,
+                        'automatic_serialization' => true
+                    )
+                ),
+                'backend' => array(
+                    'name' => 'Black Hole'
+                )
+            )
+        );
+        $this->bootstrap->registerPluginResource('cachemanager', $configCache);
+
+        $options = $this->_dbOptions;
+        $options['defaultMetadataCache'] = 'database';
+        $resource = new Zend_Application_Resource_Multidb($options);
+        $resource->setBootstrap($this->bootstrap);
+        $resource->init();
+        $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {