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

[ZF-9877] Zend_Translate:

- allow to set cache as option and at initiation

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

+ 14 - 0
documentation/manual/en/module_specs/Zend_Translate-Additional.xml

@@ -100,6 +100,20 @@ $translate->addTranslation(
                     </row>
 
                     <row>
+                        <entry>cache</entry>
+                        <entry>all</entry>
+
+                        <entry>
+                            Sets a cache for the translation adapter. This must be a instance of
+                            <classname>Zend_Cache_Core</classname>
+                        </entry>
+
+                        <entry>
+                            <emphasis>Per default no cache is set</emphasis>
+                        </entry>
+                    </row>
+
+                    <row>
                         <entry>content</entry>
                         <entry>all</entry>
 

+ 5 - 1
library/Zend/Translate.php

@@ -127,8 +127,12 @@ class Zend_Translate {
             Zend_Loader::loadClass($options['adapter']);
         }
 
+        if (array_key_exists('cache', $options)) {
+            self::setCache($options['cache']);
+        }
+
         if (self::$_cache !== null) {
-            call_user_func(array($options['adapter'], 'setCache'), self::$_cache);
+            $options['cache'] = self::getCache();
         }
 
         $adapter = $options['adapter'];

+ 10 - 0
library/Zend/Translate/Adapter.php

@@ -133,6 +133,11 @@ abstract class Zend_Translate_Adapter {
             $options = array('content' => $options);
         }
 
+        if (array_key_exists('cache', $options)) {
+            self::setCache($options['cache']);
+            unset($options['cache']);
+        }
+
         if (isset(self::$_cache)) {
             $id = 'Zend_Translate_' . $this->toString() . '_Options';
             $result = self::$_cache->load($id);
@@ -326,6 +331,11 @@ abstract class Zend_Translate_Adapter {
                     throw new Zend_Translate_Exception('Instance of Zend_Log expected for option log');
                 }
 
+                if ($key == 'cache') {
+                    self::setCache($options);
+                    continue;
+                }
+
                 $this->_options[$key] = $option;
                 $change = true;
             }

+ 22 - 0
tests/Zend/TranslateTest.php

@@ -810,6 +810,28 @@ class Zend_TranslateTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * ZF-9877
+     */
+    public function testSetCacheThroughOptions()
+    {
+        require_once 'Zend/Cache.php';
+        $cache = Zend_Cache::factory('Core', 'File',
+            array('lifetime' => 120, 'automatic_serialization' => true),
+            array('cache_dir' => dirname(__FILE__) . '/_files/'));
+
+        $translate = new Zend_Translate(array(
+            'adapter' => Zend_Translate::AN_ARRAY,
+            'content' => array('msg1' => 'Message 1 (en)'),
+            'locale'  => 'en',
+            'cache'   => $cache,
+        ));
+
+        $return = Zend_Translate::getCache();
+        $this->assertTrue($return instanceof Zend_Cache_Core);
+        $this->assertTrue(Zend_Translate::hasCache());
+    }
+
+    /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *
      * @param  integer $errno