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

Source for file plain.php

Documentation is available at plain.php

  1. <?php
  2. /**
  3.  * File containing the ezcCacheStoragePlain class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package Cache
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * This class implements a simple storage to cache plain text
  30.  * on the filesystem. It takes its base methods from the extended
  31.  * storage base class {@link ezcCacheStorageFile}.
  32.  *
  33.  * In contrast to other {@link ezcCacheStorageFile} implementations, the stored
  34.  * cache data is restored using PHP's file_get_contents() class. This cache
  35.  * is not capable to store array values. If numeric values are stored the
  36.  * restored values will be of type string. The same applies to values of the
  37.  * simple type bool. It is highly recommended that you cast the resulting
  38.  * value to its correct type, also PHP will automatically perform this cast
  39.  * when necessary. An explicit cast ensures that type consistent comparisons
  40.  * (using the === or !== operators) will not fail on restored cache data.
  41.  *
  42.  * An even better solution, if you want to store non-string values, are the
  43.  * usage of {@link ezcCacheStorageFileArray} and
  44.  * {@link ezcCacheStorageFileEvalArray} storage classes, since those keep the
  45.  * variable types of you cached data consistent.
  46.  *
  47.  * For example code of using a cache storage, see {@link ezcCacheManager}.
  48.  *
  49.  * The Cache package contains several other implementations of
  50.  * {@link ezcCacheStorageFile}. As there are:
  51.  *
  52.  * - ezcCacheStorageFileArray
  53.  * - ezcCacheStorageFilePlain
  54.  * 
  55.  * @package Cache
  56.  * @version //autogentag//
  57.  */
  58. {
  59.     /**
  60.      * Fetch data from the cache.
  61.      * This method does the fetching of the data itself. In this case, the
  62.      * method simply includes the file and returns the value returned by the
  63.      * include (or false on failure).
  64.      * 
  65.      * @param string $filename The file to fetch data from.
  66.      * @return string The fetched data or false on failure.
  67.      */
  68.     protected function fetchData$filename )
  69.     {
  70.         return file_get_contents$filename );
  71.     }
  72.     
  73.     /**
  74.      * Serialize the data for storing.
  75.      * Serializes a PHP variable (except type resource and object) to a
  76.      * executable PHP code representation string.
  77.      * 
  78.      * @param mixed $data Simple type or array
  79.      * @return string The serialized data
  80.      *
  81.      * @throws ezcCacheInvalidDataException
  82.      *          If the data submitted is an array,object or a resource, since
  83.      *          this implementation of {@link ezcCacheStorageFile} can only deal
  84.      *          with scalar values.
  85.      */
  86.     protected function prepareData$data )
  87.     {
  88.         if is_scalar$data === false 
  89.         {
  90.             throw new ezcCacheInvalidDataExceptiongettype$data )array'simple' ) );
  91.         }
  92.         return ( string ) $data;
  93.     }
  94.  
  95.     /**
  96.      * Restores and returns the meta data struct.
  97.      *
  98.      * This method fetches the meta data stored in the storage and returns the
  99.      * according struct of type {@link ezcCacheStackMetaData}. The meta data
  100.      * must be stored inside the storage, but should not be visible as normal
  101.      * cache items to the user.
  102.      * 
  103.      * @return ezcCacheStackMetaData 
  104.      */
  105.     public function restoreMetaData()
  106.     {
  107.         // Silence require warnings. It's ok that meta data does not exist.
  108.         $dataStr @$this->fetchData(
  109.             $this->properties['location'$this->properties['options']->metaDataFile
  110.         );
  111.         $dataArr unserialize$dataStr );
  112.  
  113.  
  114.         $result null;
  115.         if $dataArr !== false )
  116.         {
  117.             $result new $dataArr['class']();
  118.             $result->setState$dataArr['data');
  119.         }
  120.         return $result;
  121.     }
  122.  
  123.     /**
  124.      * Stores the given meta data struct.
  125.      *
  126.      * This method stores the given $metaData inside the storage. The data must
  127.      * be stored with the same mechanism that the storage itself uses. However,
  128.      * it should not be stored as a normal cache item, if possible, to avoid
  129.      * accedental user manipulation.
  130.      *
  131.      * @param ezcCacheStackMetaData $metaData 
  132.      * @return void 
  133.      */
  134.     public function storeMetaDataezcCacheStackMetaData $metaData )
  135.     {
  136.         $dataArr array(
  137.             'class' => get_class$metaData ),
  138.             'data'  => $metaData->getState(),
  139.         );
  140.         // This storage only handles scalar values, so we serialize here.
  141.         $dataStr serialize$dataArr );
  142.         $this->storeRawData(
  143.             $this->properties['location'$this->properties['options']->metaDataFile,
  144.             $this->prepareData$dataStr )
  145.         );
  146.     }
  147. }
  148. ?>
Documentation generated by phpDocumentor 1.4.3