Explorar el Código

ZF-10034 Zend_App | Allow to initialize Zend_Translate cache from application configuration with the cache manager | Patch by Benoît Durand

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22545 44c647ce-9c0f-0410-b52a-842ac1e357ba
freak hace 15 años
padre
commit
1075264244

+ 15 - 0
library/Zend/Application/Resource/Translate.php

@@ -87,6 +87,21 @@ class Zend_Application_Resource_Translate extends Zend_Application_Resource_Reso
                 }
             }
 
+            if (!empty($options['cache']) && is_string($options['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($options['cache'])
+                    ) {
+                        $options['cache'] = $cacheManager->getCache($options['cache']);
+                    }
+                }
+            }
+
             $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
                  ? $options['registry_key']
                  : self::DEFAULT_REGISTRY_KEY;

+ 31 - 0
tests/Zend/Application/Resource/TranslateTest.php

@@ -148,6 +148,37 @@ class Zend_Application_Resource_TranslateTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('bericht4', $translate->translate('message4'));
         $this->assertEquals('shouldNotExist', $translate->translate('shouldNotExist'));
     }
+
+    /**
+     * @group ZF-10034
+     */
+    public function testSetCacheFromCacheManager()
+    {
+        $configCache = array(
+            'translate' => array(
+                'frontend' => array(
+                    'name' => 'Core',
+                    'options' => array(
+                        'lifetime' => 120,
+                        'automatic_serialization' => true
+                    )
+                ),
+                'backend' => array(
+                    'name' => 'Black Hole'
+                )
+            )
+        );
+        $this->bootstrap->registerPluginResource('cachemanager', $configCache);
+
+        $options = $this->_translationOptions;
+        $options['cache'] = 'translate';
+        $resource = new Zend_Application_Resource_Translate($options);
+        $resource->setBootstrap($this->bootstrap);
+        $resource->init();
+
+        $this->assertType('Zend_Cache_Core', Zend_Translate::getCache());
+        Zend_Translate::clearCache();
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_TranslateTest::main') {