Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file CacheMemory.php

Documentation is available at CacheMemory.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator_CacheMemory class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @copyright 2004-2007 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: CacheMemory.php,v 1.13 2007/12/03 07:35:50 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * Load Translation2 decorator base class
  41.  */
  42. require_once 'Translation2/Decorator.php';
  43.  
  44. /**
  45.  * Allows redefinition of alternate key for empty pageID
  46.  */
  47. if (!defined('TRANSLATION2_EMPTY_PAGEID_KEY')) {
  48.     define('TRANSLATION2_EMPTY_PAGEID_KEY''array_key_4_empty_pageID');
  49. }
  50. /**
  51.  * Allows redefinition of alternate key for null pageID
  52.  */
  53. if (!defined('TRANSLATION2_NULL_PAGEID_KEY')) {
  54.     define('TRANSLATION2_NULL_PAGEID_KEY''array_key_4_null_pageID');
  55. }
  56.  
  57. /**
  58.  * Decorator to cache fetched data in memory
  59.  *
  60.  * @category  Internationalization
  61.  * @package   Translation2
  62.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  63.  * @copyright 2004-2007 Lorenzo Alberton
  64.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  65.  * @version   CVS: $Id: CacheMemory.php,v 1.13 2007/12/03 07:35:50 quipo Exp $
  66.  * @link      http://pear.php.net/package/Translation2
  67.  */
  68. {
  69.     // {{{ class vars
  70.  
  71.     /**
  72.      * Translated strings array
  73.      * Used for cache purposes.
  74.      * No parameter substitution or fallback langs here.
  75.      * @var array 
  76.      * @access protected
  77.      */
  78.     var $rawData = array();
  79.  
  80.     /**
  81.      * set prefetch on/off
  82.      * @var boolean 
  83.      * @access protected
  84.      */
  85.     var $prefetch = true;
  86.  
  87.     // }}}
  88.     // {{{ _getPageIDKey()
  89.  
  90.     /**
  91.      * return a valid array key based on pageID value
  92.      *
  93.      * @param mixed $pageID (string or null)
  94.      *
  95.      * @return string 
  96.      * @access private
  97.      */
  98.     function _getPageIDKey($pageID)
  99.     {
  100.         if (is_null($pageID)) {
  101.             return TRANSLATION2_NULL_PAGEID_KEY;
  102.         }
  103.         if (empty($pageID)) {
  104.             return TRANSLATION2_EMPTY_PAGEID_KEY;
  105.         }
  106.         if ($pageID == TRANSLATION2_DEFAULT_PAGEID{
  107.             return $this->translation2->currentPageID;
  108.         }
  109.         return $pageID;
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ getRaw()
  114.  
  115.     /**
  116.      * Get translated string (as-is)
  117.      *
  118.      * First check if the string is cached, if not => fetch the page
  119.      * from the container and cache it for later use.
  120.      *
  121.      * @param string $stringID    string ID
  122.      * @param string $pageID      page/group ID
  123.      * @param string $langID      language ID
  124.      * @param string $defaultText Text to display when the strings in both
  125.      *                             the default and the fallback lang are empty
  126.      *
  127.      * @return string 
  128.      */
  129.     function getRaw($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText = null)
  130.     {
  131.         $pageID_key $this->_getPageIDKey($pageID);
  132.         $langID_key = empty($langID$this->translation2->lang['id'$langID;
  133.  
  134.         if (!array_key_exists($langID_key$this->rawData)) {
  135.             $this->rawData[$langID_key= array();
  136.         }
  137.  
  138.         if ($this->prefetch{
  139.             $this->getRawPage($pageID$langID);
  140.         }
  141.         if (array_key_exists($pageID_key$this->rawData[$langID_key])) {
  142.             if (PEAR::isError($this->rawData[$langID_key][$pageID_key])) {
  143.                 return $this->rawData[$langID_key][$pageID_key];
  144.             }
  145.             $str (isset($this->rawData[$langID_key][$pageID_key][$stringID]?
  146.                     $this->rawData[$langID_key][$pageID_key][$stringID'')//empty string or null value?
  147.         else {
  148.             $str $this->translation2->getRaw($stringID$pageID$langID$defaultText);
  149.         }
  150.         return $str;
  151.     }
  152.  
  153.     // }}}
  154.     // {{{ get()
  155.  
  156.     /**
  157.      * Get translated string
  158.      *
  159.      * First check if the string is cached, if not => fetch the page
  160.      * from the container and cache it for later use.
  161.      *
  162.      * @param string $stringID    string ID
  163.      * @param string $pageID      page/group ID
  164.      * @param string $langID      language ID
  165.      * @param string $defaultText Text to display when the strings in both
  166.      *                             the default and the fallback lang are empty
  167.      *
  168.      * @return string 
  169.      */
  170.     function get($stringID$pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null$defaultText = null)
  171.     {
  172.         $str $this->getRaw($stringID$pageID$langID$defaultText);
  173.         return $this->_replaceParams($str);
  174.     }
  175.  
  176.     // }}}
  177.     // {{{ getRawPage()
  178.  
  179.     /**
  180.      * Get the array of strings in a page
  181.      *
  182.      * First check if the strings are cached, if not => fetch the page
  183.      * from the container and cache it for later use.
  184.      *
  185.      * @param string $pageID page/group ID
  186.      * @param string $langID language ID
  187.      *
  188.      * @return array 
  189.      */
  190.     function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  191.     {
  192.         $pageID_key $this->_getPageIDKey($pageID);
  193.         $langID_key = empty($langID$this->translation2->lang['id'$langID;
  194.  
  195.         if (!array_key_exists($langID_key$this->rawData)) {
  196.             $this->rawData[$langID_key= array();
  197.         }
  198.         if (!array_key_exists($pageID_key$this->rawData[$langID_key])) {
  199.             $this->rawData[$langID_key][$pageID_key=
  200.                 $this->translation2->getRawPage($pageID$langID);
  201.         }
  202.         return $this->rawData[$langID_key][$pageID_key];
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ getPage()
  207.  
  208.     /**
  209.      * Same as getRawPage, but resort to fallback language and
  210.      * replace parameters when needed
  211.      *
  212.      * @param string $pageID page/group ID
  213.      * @param string $langID language ID
  214.      *
  215.      * @return array 
  216.      */
  217.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID$langID = null)
  218.     {
  219.         $pageID_key $this->_getPageIDKey($pageID);
  220.         $langID_key = empty($langID$this->translation2->lang['id'$langID;
  221.  
  222.         $this->getRawPage($pageID$langID);
  223.         return $this->_replaceParams($this->rawData[$langID_key][$pageID_key]);
  224.     }
  225.  
  226.     // }}}
  227. }
  228. ?>

Documentation generated on Tue, 06 May 2008 06:00:15 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.