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

Source for file Words.php

Documentation is available at Words.php

  1. <?php
  2. /**
  3.  * Mai_Cache_Words
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Cache
  27.  * @subpackage Words
  28.  * 
  29.  */
  30.  
  31. /** Load required file */
  32. {
  33.     require_once 'Zend/CodeGenerator/Php/File.php';
  34.     require_once 'Mai/Cache/Interface.php';
  35. }
  36.  
  37. /**
  38.  * Mai_Cache_Words is a way to cache words.
  39.  * 
  40.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  41.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  42.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  43.  * @link http://www.launchpad.net/mai
  44.  * 
  45.  * @category Mai
  46.  * @package Cache
  47.  * @subpackage Words
  48.  * 
  49.  * @uses Zend_CodeGenerator_Php_File
  50.  */
  51. class Mai_Cache_Words implements Mai_Cache_Interface
  52. {
  53.     /**
  54.      * Location of the cache file.
  55.      * @var string 
  56.      */
  57.     protected $_cacheLocation;
  58.     
  59.     /**
  60.      * Name of the cache file.
  61.      * @var string 
  62.      */
  63.     protected $_cacheFilename;
  64.     
  65.     /**
  66.      * Name of the cache variable.
  67.      * @var string 
  68.      */
  69.     protected $_cacheVariableName;
  70.     
  71.     /**
  72.      * Constructor sets the path and filename for saving and loading the cache.
  73.      * @param $location Location of the file. Use null to use the value in the ini file.
  74.      * @param $filename Name of the file.
  75.      * @param $varName Name of the variable.
  76.      * @param $creator Creator of this class.
  77.      * @return void 
  78.      */
  79.     function __construct($location null$filename$varName$creator null)
  80.     {
  81.         //This class should only be created by Mai_Cache
  82.         if(!($creator instanceof Mai_Cache&& Mai_Config::getVar()->cache->allow_direct_access != 1)
  83.         {
  84.             trigger_error('Please use Mai_Cache to create an instance of Mai_Cache_Words.'E_USER_NOTICE);
  85.         }
  86.         
  87.         //Load the location if null.
  88.         if(is_null($location|| !is_string($location))
  89.         {
  90.             $location Mai_Config::getVar()->cache->location;
  91.         }
  92.         
  93.         if(!is_string($filename))
  94.         {
  95.             throw new InvalidArgumentException('$filename must be a string.');
  96.         }
  97.         
  98.         if(!is_string($varName))
  99.         {
  100.             throw new InvalidArgumentException('$varName must be a string.');
  101.         }
  102.         
  103.         $this->_cacheLocation = $location;
  104.         $this->_cacheFilename = $filename;
  105.         $this->_cacheVariableName = $varName;
  106.     }
  107.     
  108.     /**
  109.      * Creates an cache file with date from the base database.
  110.      * @param $words The words to cache.
  111.      * @return void 
  112.      */
  113.     public function createCache(array $words)
  114.     {
  115.         $body $this->convertArrayToSourceCode($words);
  116.         
  117.         $this->createCacheFile($body);
  118.     }
  119.     
  120.     /**
  121.      * Converts an double array to an php array.
  122.      * @param $data 
  123.      * @return string 
  124.      */
  125.     protected function convertArrayToSourceCode(array $data)
  126.     {
  127.         $output '$' $this->_cacheVariableName ' = array(';
  128.         
  129.         foreach($data as $key_1 => $value_1)
  130.         {
  131.             $output .= $key_1 ' => array(';
  132.             
  133.             foreach($value_1 as $key_2 => $value_2)
  134.             {
  135.                 //Integers should not be transformed to strings
  136.                 if(is_int($value_2))
  137.                 {
  138.                     $output .= '\'' $key_2 '\'' ' => ' $value_2 ', ';
  139.                 }
  140.                 else
  141.                 {
  142.                     $output .= '\'' $key_2 '\'' ' => \'' $value_2 '\', ';
  143.                 }
  144.             }
  145.             
  146.             $output .= "),";
  147.         }
  148.         
  149.         $output .= ');';
  150.         
  151.         return $output;
  152.     }
  153.     
  154.     /**
  155.      * Creates the cache file.
  156.      * @param string $body 
  157.      * @return void 
  158.      */
  159.     protected function createCacheFile($body)
  160.     {
  161.         $file new Zend_CodeGenerator_Php_File(array(
  162.                     'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  163.                         'shortDescription'    => 'Cache file by Mai Lib.',
  164.                         'longDescription'     => 'Cache file generated by Mai_Cache_Words. Part of Mai Lib library (www.mai-ai.org). Usage of words granted by OpenTaal.org',
  165.                         'tags'                 => array(
  166.                             array(
  167.                                 'name'            => 'version',
  168.                                 'description'    => date(DATE_RFC822)
  169.                             ),
  170.                             array(
  171.                                 'name'            => 'License',
  172.                                 'description'    => 'http://creativecommons.org/licenses/by/3.0/legalcode'
  173.                             ),
  174.                             array(
  175.                                 'name'            => 'package',
  176.                                 'description'    => 'Other'
  177.                             )
  178.                         )
  179.                     )),
  180.                     
  181.                     'body' => $body
  182.         ));
  183.  
  184.         file_put_contents($this->_cacheLocation '/' .$this->_cacheFilename '.php' $file->generate());
  185.     }
  186.     
  187.     /**
  188.      * Loads the class array from a defined php file.
  189.      * @return boolean|array
  190.      */
  191.     public function returnCacheFromFile()
  192.     {
  193.         $variableName $this->_cacheVariableName;
  194.         
  195.         try
  196.         {
  197.             //Try to get the file
  198.             require$this->_cacheLocation '/' .$this->_cacheFilename '.php');
  199.         }
  200.         catch(Exception $e)
  201.         {
  202.             //Do not show an error message, just return false
  203.             return false;    
  204.         }
  205.         
  206.         
  207.         //Test the file (it should contain an array named $variableName)
  208.         if(isset($$variableName&& is_array($$variableName))
  209.         {
  210.             return $$variableName;
  211.         }
  212.         
  213.         return false;
  214.     }
  215. }

Documentation generated on Mon, 27 Jul 2009 19:55:14 +0200 by phpDocumentor 1.4.1