Source for file controller.php

Documentation is available at controller.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Cache
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Public cache handler
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * JCache object
  22.      *
  23.      * @var    JCache 
  24.      * @since  11.1
  25.      */
  26.     public $cache;
  27.  
  28.     /**
  29.      * Array of options
  30.      *
  31.      * @var    array 
  32.      * @since  11.1
  33.      */
  34.     public $options;
  35.  
  36.     /**
  37.      * Constructor
  38.      *
  39.      * @param   array  $options  Array of options
  40.      *
  41.      * @since   11.1
  42.      */
  43.     public function __construct($options)
  44.     {
  45.         $this->cache = new JCache($options);
  46.         $this->options = $this->cache->_options;
  47.  
  48.         // Overwrite default options with given options
  49.         foreach ($options as $option => $value)
  50.         {
  51.             if (isset($options[$option]))
  52.             {
  53.                 $this->options[$option$options[$option];
  54.             }
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Magic method to proxy JCacheControllerMethods
  60.      *
  61.      * @param   string  $name       Name of the function
  62.      * @param   array   $arguments  Array of arguments for the function
  63.      *
  64.      * @return  mixed 
  65.      *
  66.      * @since   11.1
  67.      */
  68.     public function __call($name$arguments)
  69.     {
  70.         $nazaj call_user_func_array(array($this->cache$name)$arguments);
  71.         return $nazaj;
  72.     }
  73.  
  74.     /**
  75.      * Returns a reference to a cache adapter object, always creating it
  76.      *
  77.      * @param   string  $type     The cache object type to instantiate; default is output.
  78.      * @param   array   $options  Array of options
  79.      *
  80.      * @return  JCache  A JCache object
  81.      *
  82.      * @since   11.1
  83.      * @throws  RuntimeException
  84.      */
  85.     public static function getInstance($type 'output'$options array())
  86.     {
  87.         self::addIncludePath(JPATH_PLATFORM '/joomla/cache/controller');
  88.  
  89.         $type strtolower(preg_replace('/[^A-Z0-9_\.-]/i'''$type));
  90.  
  91.         $class 'JCacheController' ucfirst($type);
  92.  
  93.         if (!class_exists($class))
  94.         {
  95.             // Search for the class file in the JCache include paths.
  96.             jimport('joomla.filesystem.path');
  97.  
  98.             if ($path JPath::find(self::addIncludePath()strtolower($type'.php'))
  99.             {
  100.                 include_once $path;
  101.             }
  102.             else
  103.             {
  104.                 throw new RuntimeException('Unable to load Cache Controller: ' $type500);
  105.             }
  106.         }
  107.  
  108.         return new $class($options);
  109.     }
  110.  
  111.     /**
  112.      * Set caching enabled state
  113.      *
  114.      * @param   boolean  $enabled  True to enable caching
  115.      *
  116.      * @return  void 
  117.      *
  118.      * @since   11.1
  119.      */
  120.     public function setCaching($enabled)
  121.     {
  122.         $this->cache->setCaching($enabled);
  123.     }
  124.  
  125.     /**
  126.      * Set cache lifetime
  127.      *
  128.      * @param   integer  $lt  Cache lifetime
  129.      *
  130.      * @return  void 
  131.      *
  132.      * @since   11.1
  133.      */
  134.     public function setLifeTime($lt)
  135.     {
  136.         $this->cache->setLifeTime($lt);
  137.     }
  138.  
  139.     /**
  140.      * Add a directory where JCache should search for controllers. You may
  141.      * either pass a string or an array of directories.
  142.      *
  143.      * @param   string  $path  A path to search.
  144.      *
  145.      * @return  array   An array with directory elements
  146.      *
  147.      * @since   11.1
  148.      */
  149.     public static function addIncludePath($path '')
  150.     {
  151.         static $paths;
  152.  
  153.         if (!isset($paths))
  154.         {
  155.             $paths array();
  156.         }
  157.         if (!empty($path&& !in_array($path$paths))
  158.         {
  159.             jimport('joomla.filesystem.path');
  160.             array_unshift($pathsJPath::clean($path));
  161.         }
  162.         return $paths;
  163.     }
  164.  
  165.     /**
  166.      * Get stored cached data by id and group
  167.      *
  168.      * @param   string  $id     The cache data id
  169.      * @param   string  $group  The cache data group
  170.      *
  171.      * @return  mixed   False on no result, cached object otherwise
  172.      *
  173.      * @since   11.1
  174.      */
  175.     public function get($id$group null)
  176.     {
  177.         $data $this->cache->get($id$group);
  178.  
  179.         if ($data === false)
  180.         {
  181.             $locktest new stdClass;
  182.             $locktest->locked null;
  183.             $locktest->locklooped null;
  184.             $locktest $this->cache->lock($id$group);
  185.             if ($locktest->locked == true && $locktest->locklooped == true)
  186.             {
  187.                 $data $this->cache->get($id$group);
  188.             }
  189.             if ($locktest->locked == true)
  190.             {
  191.                 $this->cache->unlock($id$group);
  192.             }
  193.         }
  194.  
  195.         // Check again because we might get it from second attempt
  196.         if ($data !== false)
  197.         {
  198.             // Trim to fix unserialize errors
  199.             $data unserialize(trim($data));
  200.         }
  201.         return $data;
  202.     }
  203.  
  204.     /**
  205.      * Store data to cache by id and group
  206.      *
  207.      * @param   mixed    $data        The data to store
  208.      * @param   string   $id          The cache data id
  209.      * @param   string   $group       The cache data group
  210.      * @param   boolean  $wrkarounds  True to use wrkarounds
  211.      *
  212.      * @return  boolean  True if cache stored
  213.      *
  214.      * @since   11.1
  215.      */
  216.     public function store($data$id$group null$wrkarounds true)
  217.     {
  218.         $locktest new stdClass;
  219.         $locktest->locked null;
  220.         $locktest->locklooped null;
  221.  
  222.         $locktest $this->cache->lock($id$group);
  223.  
  224.         if ($locktest->locked == false && $locktest->locklooped == true)
  225.         {
  226.             $locktest $this->cache->lock($id$group);
  227.         }
  228.  
  229.         $sucess $this->cache->store(serialize($data)$id$group);
  230.  
  231.         if ($locktest->locked == true)
  232.         {
  233.             $this->cache->unlock($id$group);
  234.         }
  235.  
  236.         return $sucess;
  237.     }
  238. }

Documentation generated on Tue, 19 Nov 2013 14:57:05 +0100 by phpDocumentor 1.4.3