Source for file callback.php

Documentation is available at callback.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.  * Joomla! Cache callback type object
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Cache
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * Executes a cacheable callback if not found in cache else returns cached output and result
  22.      *
  23.      * Since arguments to this function are read with func_get_args you can pass any number of
  24.      * arguments to this method
  25.      * as long as the first argument passed is the callback definition.
  26.      *
  27.      * The callback definition can be in several forms:
  28.      * - Standard PHP Callback array see <http://php.net/callback> [recommended]
  29.      * - Function name as a string eg. 'foo' for function foo()
  30.      * - Static method name as a string eg. 'MyClass::myMethod' for method myMethod() of class MyClass
  31.      *
  32.      * @return  mixed  Result of the callback
  33.      *
  34.      * @since   11.1
  35.      */
  36.     public function call()
  37.     {
  38.         // Get callback and arguments
  39.         $args func_get_args();
  40.         $callback array_shift($args);
  41.  
  42.         return $this->get($callback$args);
  43.     }
  44.  
  45.     /**
  46.      * Executes a cacheable callback if not found in cache else returns cached output and result
  47.      *
  48.      * @param   mixed    $callback    Callback or string shorthand for a callback
  49.      * @param   array    $args        Callback arguments
  50.      * @param   string   $id          Cache id
  51.      * @param   boolean  $wrkarounds  True to use wrkarounds
  52.      * @param   array    $woptions    Workaround options
  53.      *
  54.      * @return  mixed  Result of the callback
  55.      *
  56.      * @since   11.1
  57.      */
  58.     public function get($callback$args array()$id false$wrkarounds false$woptions array())
  59.     {
  60.  
  61.         // Normalize callback
  62.         if (is_array($callback))
  63.         {
  64.             // We have a standard php callback array -- do nothing
  65.         }
  66.         elseif (strstr($callback'::'))
  67.         {
  68.             // This is shorthand for a static method callback classname::methodname
  69.             list ($class$methodexplode('::'$callback);
  70.             $callback array(trim($class)trim($method));
  71.         }
  72.         elseif (strstr($callback'->'))
  73.         {
  74.             /*
  75.              * This is a really not so smart way of doing this... we provide this for backward compatability but this
  76.              * WILL! disappear in a future version.  If you are using this syntax change your code to use the standard
  77.              * PHP callback array syntax: <http://php.net/callback>
  78.              *
  79.              * We have to use some silly global notation to pull it off and this is very unreliable
  80.              */
  81.             list ($object_123456789$methodexplode('->'$callback);
  82.             global $$object_123456789;
  83.             $callback array($$object_123456789$method);
  84.         }
  85.         else
  86.         {
  87.             // We have just a standard function -- do nothing
  88.         }
  89.  
  90.         if (!$id)
  91.         {
  92.             // Generate an ID
  93.             $id $this->_makeId($callback$args);
  94.         }
  95.  
  96.         $data $this->cache->get($id);
  97.  
  98.         $locktest new stdClass;
  99.         $locktest->locked null;
  100.         $locktest->locklooped null;
  101.  
  102.         if ($data === false)
  103.         {
  104.             $locktest $this->cache->lock($id);
  105.             if ($locktest->locked == true && $locktest->locklooped == true)
  106.             {
  107.                 $data $this->cache->get($id);
  108.             }
  109.         }
  110.  
  111.         $coptions array();
  112.  
  113.         if ($data !== false)
  114.         {
  115.  
  116.             $cached unserialize(trim($data));
  117.             $coptions['mergehead'= isset($woptions['mergehead']$woptions['mergehead'0;
  118.             $output ($wrkarounds == false$cached['output'JCache::getWorkarounds($cached['output']$coptions);
  119.             $result $cached['result'];
  120.             if ($locktest->locked == true)
  121.             {
  122.                 $this->cache->unlock($id);
  123.             }
  124.  
  125.         }
  126.         else
  127.         {
  128.  
  129.             if (!is_array($args))
  130.             {
  131.                 $Args !empty($argsarray(&$argsarray();
  132.             }
  133.             else
  134.             {
  135.                 $Args &$args;
  136.             }
  137.  
  138.             if ($locktest->locked == false)
  139.             {
  140.                 $locktest $this->cache->lock($id);
  141.             }
  142.  
  143.             if (isset($woptions['modulemode']&& $woptions['modulemode'== 1)
  144.             {
  145.                 $document JFactory::getDocument();
  146.                 $coptions['modulemode'1;
  147.                 $coptions['headerbefore'$document->getHeadData();
  148.             }
  149.             else
  150.             {
  151.                 $coptions['modulemode'0;
  152.             }
  153.  
  154.             ob_start();
  155.             ob_implicit_flush(false);
  156.  
  157.             $result call_user_func_array($callback$Args);
  158.             $output ob_get_contents();
  159.  
  160.             ob_end_clean();
  161.  
  162.             $cached array();
  163.  
  164.             $coptions['nopathway'= isset($woptions['nopathway']$woptions['nopathway'1;
  165.             $coptions['nohead'= isset($woptions['nohead']$woptions['nohead'1;
  166.             $coptions['nomodules'= isset($woptions['nomodules']$woptions['nomodules'1;
  167.  
  168.             $cached['output'($wrkarounds == false$output JCache::setWorkarounds($output$coptions);
  169.             $cached['result'$result;
  170.  
  171.             // Store the cache data
  172.             $this->cache->store(serialize($cached)$id);
  173.             if ($locktest->locked == true)
  174.             {
  175.                 $this->cache->unlock($id);
  176.             }
  177.         }
  178.  
  179.         echo $output;
  180.         return $result;
  181.     }
  182.  
  183.     /**
  184.      * Generate a callback cache id
  185.      *
  186.      * @param   callback  $callback  Callback to cache
  187.      * @param   array     $args      Arguments to the callback method to cache
  188.      *
  189.      * @return  string  MD5 Hash : function cache id
  190.      *
  191.      * @since   11.1
  192.      */
  193.     protected function _makeId($callback$args)
  194.     {
  195.         if (is_array($callback&& is_object($callback[0]))
  196.         {
  197.             $vars get_object_vars($callback[0]);
  198.             $vars[strtolower(get_class($callback[0]));
  199.             $callback[0$vars;
  200.         }
  201.  
  202.         return md5(serialize(array($callback$args)));
  203.     }
  204. }

Documentation generated on Tue, 19 Nov 2013 14:55:00 +0100 by phpDocumentor 1.4.3