Apache Zeta Components Manual :: File Source for cache_backend.php

Source for file cache_backend.php

Documentation is available at cache_backend.php

  1. <?php
  2. /**
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements.  See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership.  The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License.  You may obtain a copy of the License at
  11.  * 
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  * 
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  *
  21.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22.  * @version //autogentag//
  23.  * @filesource
  24.  * @package TranslationCacheTiein
  25.  */
  26.  
  27. /**
  28.  * Translation backend that reads translation data from a cache.
  29.  *
  30.  * This class is a backend implementation for the Translation system. This
  31.  * specific one uses the Cache Component to store and serve cached translation
  32.  * data.
  33.  *
  34.  * Example that uses both the {@link ezcCacheStorageFileArray} and {@link }
  35.  * ezcTranslationCacheBackend} classes:
  36.  * <code>
  37.  * <?php
  38.  * // Create a cache object with content
  39.  * $cacheObj = new ezcCacheStorageFileArray( 'ezcTranslationCacheBackendTest' );
  40.  * 
  41.  * $expected = array(
  42.  *     new ezcTranslationData( 'Node ID: %node_id Visibility: %visibility',
  43.  *                             'Knoop ID: %node_id Zichtbaar: %visibility',
  44.  *                             false, ezcTranslationData::TRANSLATED )
  45.  * );
  46.  * $cacheObj->store( 'nl-nl/contentstructuremenu/show_content_structure', $expected );
  47.  *
  48.  * // Use the cache backend
  49.  * $backend = new ezcTranslationCacheBackend( $cacheObj );
  50.  * $context = $backend->getContext( 'nl-nl', 'contentstructuremenu/show_content_structure' );
  51.  * $translation = new ezcTranslation( $context );
  52.  * echo $translation->getTranslation( 'Node ID: %node_id Visibility: %visibility',
  53.  *                                    array( 'node_id' => 42, 'visibility' => 'yes' ) );
  54.  * ?>
  55.  * </code>
  56.  *
  57.  * Example that stores a whole translation file into a cache by using {@link }
  58.  * ezcTranslationContextRead} interface that is implemented by the {@link }
  59.  * ezcTranslationTsBackend} and the {@link ezcTranslationContextWrite}
  60.  * interface that is implemented by this class:
  61.  * <code>
  62.  * <?php
  63.  * // Settings
  64.  * $locale = 'nb-no';
  65.  *
  66.  * // Setup the cache object
  67.  * $cacheObj = new ezcCacheStorageFileArray( 'ezcTranslationCacheBackendTest' );
  68.  *
  69.  * // Initialize the writer
  70.  * $writer = new ezcTranslationCacheBackend( $cacheObj );
  71.  * $writer->initWriter( $locale );
  72.  *
  73.  * // Initialize the reader
  74.  * $reader = new ezcTranslationTsBackend( "translations" );
  75.  * $reader->setOptions( array ( 'format' => '[LOCALE].xml' ) );
  76.  * $reader->initReader( $locale );
  77.  * 
  78.  * // Process the data
  79.  * $contexts = array();
  80.  * foreach ( $reader as $contextName => $contextData )
  81.  * {
  82.  *     $writer->storeContext( $contextName, $contextData );
  83.  * }
  84.  *
  85.  * // Deinitialize the writer and reader
  86.  * $writer->deinitWriter();
  87.  * $reader->deinitReader();
  88.  * ?>
  89.  * </code>
  90.  *
  91.  * @package TranslationCacheTiein
  92.  * @version //autogentag//
  93.  * @mainclass
  94.  */
  95. class ezcTranslationCacheBackend implements ezcTranslationBackendezcTranslationContextWrite
  96. {
  97.     /**
  98.      * Stores the cache object to use for storing and fetching.
  99.      *
  100.      * @var ezcCacheStorageFileArray 
  101.      */
  102.     private $cache;
  103.  
  104.     /**
  105.      * The locale to write to.
  106.      *
  107.      * @var string 
  108.      */
  109.     private $writeLocale;
  110.  
  111.     /**
  112.      * Constructs a new ezcTranslationCacheBackend that will store data to $cacheObject.
  113.      *
  114.      * @param ezcCacheStorageFileArray $cacheObject 
  115.      */
  116.     public function __constructezcCacheStorageFileArray $cacheObject )
  117.     {
  118.         $this->cache $cacheObject;
  119.     }
  120.  
  121.     /**
  122.      * Sets configuration data
  123.      *
  124.      * This backend accepts no settings at all, and will always throw an
  125.      * ezcBaseSettingNotFoundException for every setting that is contained
  126.      * in the $configurationData.
  127.      *
  128.      * @param array $configurationData 
  129.      * @throws ezcBaseSettingNotFoundException if an unknown setting is passed.
  130.      * @return void 
  131.      * @todo Implement ezcBaseOptions class, if options are added.
  132.      */
  133.     public function setOptions$configurationData )
  134.     {
  135.         foreach $configurationData as $name => $value )
  136.         {
  137.             throw new ezcBaseSettingNotFoundException$name );
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * Returns a array containing the translation map for the specified
  143.      * $locale and $context.
  144.      *
  145.      * It uses the $tsLocationPath and
  146.      * $tsFilenameFormat properties to locate the file, unless caching is
  147.      * enabled. If a cache object is available it will be used to retrieve the
  148.      * information from the cache.
  149.      *
  150.      * @throws ezcTranslationContextNotAvailableException if the context is not available.
  151.      * @param string $locale 
  152.      * @param string $context 
  153.      * @return array(ezcTranslationData) 
  154.      */
  155.     public function getContext$locale$context )
  156.     {
  157.         $cachedContext $this->cache->restore"$locale/$context);
  158.         if $cachedContext === false )
  159.         {
  160.             throw new ezcTranslationContextNotAvailableException$context );
  161.         }
  162.         foreach $cachedContext as $key => $cachedElement )
  163.         {
  164.             if $cachedElement->status == ezcTranslationData::OBSOLETE )
  165.             {
  166.                 unset$cachedContext[$key);
  167.             }
  168.         }
  169.         return $cachedContext;
  170.     }
  171.  
  172.     /**
  173.      * Initializes the writer to write to the locale $locale.
  174.      *
  175.      * Before starting to writer contexts to the writer, you should call
  176.      * this method to initialize it.
  177.      *
  178.      * @param string $locale 
  179.      *  $return void
  180.      */
  181.     public function initWriter$locale )
  182.     {
  183.         $this->writeLocale $locale;
  184.     }
  185.  
  186.     /**
  187.      * Deinitializes the writer.
  188.      *
  189.      * This method should be called after the last context was written to
  190.      * cleanup resources.
  191.      *
  192.      * @throws ezcTranslationWriterNotInitializedException when the writer is
  193.      *          not initialized with initWriter().
  194.      * @return void 
  195.      */
  196.     public function deinitWriter()
  197.     {
  198.         if is_null$this->writeLocale ) )
  199.         {
  200.             throw new ezcTranslationWriterNotInitializedException();
  201.         }
  202.         $this->writeLocale null;
  203.     }
  204.  
  205.     /**
  206.      * Stores a context.
  207.      *
  208.      * This method stores the context that it received to the backend specified
  209.      * storage place.
  210.      *
  211.      * @throws ezcTranslationWriterNotInitializedException when the writer is
  212.      *          not initialized with initWriter().
  213.      * @param string $context The context's name
  214.      * @param array(ezcTranslationData)  $data The context's translation map
  215.      * @return void 
  216.      */
  217.     public function storeContext$contextarray $data )
  218.     {
  219.         if is_null$this->writeLocale ) )
  220.         {
  221.             throw new ezcTranslationWriterNotInitializedException();
  222.         }
  223.         foreach $data as $key => $cachedElement )
  224.         {
  225.             if $cachedElement->status == ezcTranslationData::OBSOLETE )
  226.             {
  227.                 unset$data[$key);
  228.             }
  229.         }
  230.         $cachedContext $this->cache->store"{$this->writeLocale}/$context$data );
  231.     }
  232. }
  233. ?>
Documentation generated by phpDocumentor 1.4.3