Browse Source

ZF-10033 Zend_App Allow to initialize Zend_Db_Table_Abstract metadata cache from application configuration. Patch by Benoît Durand

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22544 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak 15 years ago
parent
commit
94a5d789f3

+ 32 - 0
library/Zend/Application/Resource/Db.php

@@ -158,4 +158,36 @@ class Zend_Application_Resource_Db extends Zend_Application_Resource_ResourceAbs
             return $db;
         }
     }
+
+    /**
+     * Set the default metadata cache
+     * 
+     * @param string|Zend_Cache_Core $cache
+     * @return Zend_Application_Resource_Db
+     */
+    public 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;
+    }
 }

+ 58 - 0
tests/Zend/Application/Resource/DbTest.php

@@ -71,6 +71,8 @@ class Zend_Application_Resource_DbTest extends PHPUnit_Framework_TestCase
 
     public function tearDown()
     {
+    	Zend_Db_Table::setDefaultMetadataCache();
+
         // Restore original autoloaders
         $loaders = spl_autoload_functions();
         foreach ($loaders as $loader) {
@@ -145,6 +147,62 @@ class Zend_Application_Resource_DbTest extends PHPUnit_Framework_TestCase
         $db = $resource->getDbAdapter();
         $this->assertTrue($db instanceof Zend_Db_Adapter_Pdo_Sqlite);
     }
+
+    /**
+     * @group ZF-10033
+     */
+    public function testSetDefaultMetadataCache()
+    {
+        $cache = Zend_Cache::factory('Core', 'Black Hole', array(
+            'lifetime' => 120,
+            'automatic_serialization' => true
+        ));
+
+        $config = array(
+            'adapter' => 'PDO_SQLite',
+            'params'  => array(
+                'dbname' => ':memory:',
+            ),
+            'defaultMetadataCache' => $cache,
+        );
+        $resource = new Zend_Application_Resource_Db($config);
+        $resource->init();
+        $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
+    }
+
+    /**
+     * @group ZF-10033
+     */
+    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);
+
+        $config = array(
+            'bootstrap' => $this->bootstrap,
+            'adapter' => 'PDO_SQLite',
+            'params'  => array(
+                'dbname' => ':memory:',
+            ),
+            'defaultMetadataCache' => 'database',
+        );
+        $resource = new Zend_Application_Resource_Db($config);
+        $resource->init();
+        $this->assertType('Zend_Cache_Core', Zend_Db_Table::getDefaultMetadataCache());
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_DbTest::main') {