Explorar o código

ZF-7058: Zend_Application Local Resource - set cache via config

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23747 44c647ce-9c0f-0410-b52a-842ac1e357ba
intiilapa %!s(int64=15) %!d(string=hai) anos
pai
achega
2eccbb9fb6

+ 21 - 0
documentation/manual/en/module_specs/Zend_Application-AvailableResources-Locale.xml

@@ -147,4 +147,25 @@ resources.locale.force = true
 ]]></programlisting>
         </example>
     </sect3>
+
+    <sect3 id="zend.application.available-resources.locale.cache">
+        <title>Configure cache template</title>
+
+        <para>
+            When you have set no cache, <classname>Zend_Locale</classname> will set itself a cache 
+            with the file backend by default. But if you want to choose the backend or others 
+            options, you can use the name of a cache template or an instance of
+            <classname>Zend_Cache_Core</classname>.
+            For more informations look into <xref linkend="zend.locale.cache" />.
+        </para>
+
+        <example id="zend.application.available-resources.locale.configExampleCache">
+            <title>Defining a cache template to use</title>
+
+            <programlisting language="ini"><![CDATA[
+; Optionally you can also the cache template to use for caching:
+resources.locale.cache = "locale"
+]]></programlisting>
+        </example>
+    </sect3>
 </sect2>

+ 21 - 0
documentation/manual/fr/module_specs/Zend_Application-AvailableResources-Locale.xml

@@ -151,4 +151,25 @@ resources.locale.force = true
 ]]></programlisting>
         </example>
     </sect3>
+
+    <sect3 id="zend.application.available-resources.locale.cache">
+        <title>Configurer le cache</title>
+
+        <para>
+        	Lorsque vous ne définissez pas de cache, <classname>Zend_Locale</classname> définit son
+        	propre cache avec le backend fichier par défaut. Cependant, vous pouvez utiliser un nom
+        	de modèle du gestionnaire de cache, ou une instance de
+        	<classname>Zend_Cache_Core</classname>, pour choisir le backend, ou d'autres options.
+            Pour plus d'informations, veuillez vous reporter à <xref linkend="zend.locale.cache" />.
+        </para>
+
+        <example id="zend.application.available-resources.locale.configExampleCache">
+            <title>Définition du cache avec un modèle du gestionnaire</title>
+
+            <programlisting language="ini"><![CDATA[
+; Optionnellement, on peut utiliser un modèle du gestionnaire de cache :
+resources.locale.cache = "locale"
+]]></programlisting>
+        </example>
+    </sect3>
 </sect2>

+ 30 - 2
library/Zend/Application/Resource/Locale.php

@@ -56,7 +56,6 @@ class Zend_Application_Resource_Locale
         return $this->getLocale();
     }
 
-
     /**
      * Retrieve locale object
      *
@@ -66,7 +65,8 @@ class Zend_Application_Resource_Locale
     {
         if (null === $this->_locale) {
             $options = $this->getOptions();
-            if(!isset($options['default'])) {
+
+            if (!isset($options['default'])) {
                 $this->_locale = new Zend_Locale();
             } elseif(!isset($options['force']) ||
                      (bool) $options['force'] == false)
@@ -86,4 +86,32 @@ class Zend_Application_Resource_Locale
 
         return $this->_locale;
     }
+
+    /**
+     * Set the cache
+     *
+     * @param string|Zend_Cache_Core $cache
+     * @return Zend_Application_Resource_Locale
+     */
+    public function setCache($cache)
+    {
+        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)) {
+                    $cache = $cacheManager->getCache($cache);
+                }
+            }
+        }
+
+        if ($cache instanceof Zend_Cache_Core) {
+            Zend_Locale::setCache($cache);
+        }
+
+        return $this;
+    }
 }

+ 54 - 0
tests/Zend/Application/Resource/LocaleTest.php

@@ -128,6 +128,60 @@ class Zend_Application_Resource_LocaleTest extends PHPUnit_Framework_TestCase
         $this->assertFalse('kok_IN' == $locale->__toString());
         $this->assertSame(Zend_Registry::get('Zend_Locale'), $locale);
     }
+
+    /**
+     * @group ZF-7058
+     */
+    public function testSetCache()
+    {
+        $cache = Zend_Cache::factory('Core', 'Black Hole', array(
+            'lifetime' => 120,
+            'automatic_serialization' => true
+        ));
+
+        $config = array(
+            'default' => 'fr_FR',
+            'cache' => $cache,
+        );
+        $resource = new Zend_Application_Resource_Locale($config);
+        $resource->init();
+        $backend = Zend_Locale::getCache()->getBackend();
+        $this->assertInstanceOf('Zend_Cache_Backend_BlackHole', $backend);
+        Zend_Locale::removeCache();
+    }
+
+    /**
+     * @group ZF-7058
+     */
+    public function testSetCacheFromCacheManager()
+    {
+        $configCache = array(
+            'memory' => array(
+                'frontend' => array(
+                    'name' => 'Core',
+                    'options' => array(
+                        'lifetime' => 120,
+                        'automatic_serialization' => true
+                    )
+                ),
+                'backend' => array(
+                    'name' => 'Black Hole'
+                )
+            )
+        );
+        $this->bootstrap->registerPluginResource('cachemanager', $configCache);
+        $this->assertFalse(Zend_Locale::hasCache());
+
+        $config = array(
+            'bootstrap' => $this->bootstrap,
+            'cache' => 'memory',
+        );
+        $resource = new Zend_Application_Resource_Locale($config);
+        $resource->init();
+
+        $this->assertTrue(Zend_Locale::hasCache());
+        Zend_Locale::removeCache();
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LocaleTest::main') {