web/lib/Zend/Controller/Action/Helper/Cache.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Controller
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Cache.php 22662 2010-07-24 17:37:36Z mabe $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Controller_Action_Helper_Abstract
       
    24  */
       
    25 require_once 'Zend/Controller/Action/Helper/Abstract.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Controller_Action_Exception
       
    29  */
       
    30 require_once 'Zend/Controller/Action/Exception.php';
       
    31 
       
    32 /**
       
    33  * @see Zend_Cache_Manager
       
    34  */
       
    35 require_once 'Zend/Cache/Manager.php';
       
    36 
       
    37 /**
       
    38  * @category   Zend
       
    39  * @package    Zend_Controller
       
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    42  */
       
    43 class Zend_Controller_Action_Helper_Cache
       
    44     extends Zend_Controller_Action_Helper_Abstract
       
    45 {
       
    46 
       
    47     /**
       
    48      * Local Cache Manager object used by Helper
       
    49      *
       
    50      * @var Zend_Cache_Manager
       
    51      */
       
    52     protected $_manager = null;
       
    53 
       
    54     /**
       
    55      * Indexed map of Actions to attempt Page caching on by Controller
       
    56      *
       
    57      * @var array
       
    58      */
       
    59     protected $_caching = array();
       
    60 
       
    61     /**
       
    62      * Indexed map of Tags by Controller and Action
       
    63      *
       
    64      * @var array
       
    65      */
       
    66     protected $_tags = array();
       
    67     
       
    68     /**
       
    69      * Indexed map of Extensions by Controller and Action
       
    70      *
       
    71      * @var array
       
    72      */
       
    73     protected $_extensions = array();
       
    74 
       
    75     /**
       
    76      * Track output buffering condition
       
    77      */
       
    78     protected $_obStarted = false;
       
    79 
       
    80     /**
       
    81      * Tell the helper which actions are cacheable and under which
       
    82      * tags (if applicable) they should be recorded with
       
    83      *
       
    84      * @param array $actions
       
    85      * @param array $tags
       
    86      * @return void
       
    87      */
       
    88     public function direct(array $actions, array $tags = array(), $extension = null)
       
    89     {
       
    90         $controller = $this->getRequest()->getControllerName();
       
    91         $actions = array_unique($actions);
       
    92         if (!isset($this->_caching[$controller])) {
       
    93             $this->_caching[$controller] = array();
       
    94         }
       
    95         if (!empty($tags)) {
       
    96             $tags = array_unique($tags);
       
    97             if (!isset($this->_tags[$controller])) {
       
    98                 $this->_tags[$controller] = array();
       
    99             }
       
   100         }
       
   101         foreach ($actions as $action) {
       
   102             $this->_caching[$controller][] = $action;
       
   103             if (!empty($tags)) {
       
   104                 $this->_tags[$controller][$action] = array();
       
   105                 foreach ($tags as $tag) {
       
   106                     $this->_tags[$controller][$action][] = $tag;
       
   107                 }
       
   108             }
       
   109         }
       
   110         if ($extension) {
       
   111             if (!isset($this->_extensions[$controller])) {
       
   112                 $this->_extensions[$controller] = array();
       
   113             }
       
   114             foreach ($actions as $action) {
       
   115                 $this->_extensions[$controller][$action] = $extension;
       
   116             }
       
   117         }
       
   118     }
       
   119 
       
   120     /**
       
   121      * Remove a specific page cache static file based on its
       
   122      * relative URL from the application's public directory.
       
   123      * The file extension is not required here; usually matches
       
   124      * the original REQUEST_URI that was cached.
       
   125      *
       
   126      * @param string $relativeUrl
       
   127      * @param bool $recursive
       
   128      * @return mixed
       
   129      */
       
   130     public function removePage($relativeUrl, $recursive = false)
       
   131     {
       
   132         $cache = $this->getCache(Zend_Cache_Manager::PAGECACHE);
       
   133         if ($recursive) {
       
   134             $backend = $cache->getBackend();
       
   135             if (($backend instanceof Zend_Cache_Backend)
       
   136                 && method_exists($backend, 'removeRecursively')
       
   137             ) {
       
   138                 return $backend->removeRecursively($relativeUrl);
       
   139             }
       
   140         }
       
   141 
       
   142         return $cache->remove($relativeUrl);
       
   143     }
       
   144 
       
   145     /**
       
   146      * Remove a specific page cache static file based on its
       
   147      * relative URL from the application's public directory.
       
   148      * The file extension is not required here; usually matches
       
   149      * the original REQUEST_URI that was cached.
       
   150      *
       
   151      * @param array $tags
       
   152      * @return mixed
       
   153      */
       
   154     public function removePagesTagged(array $tags)
       
   155     {
       
   156         return $this->getCache(Zend_Cache_Manager::PAGECACHE)
       
   157             ->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
       
   158     }
       
   159 
       
   160     /**
       
   161      * Commence page caching for any cacheable actions
       
   162      *
       
   163      * @return void
       
   164      */
       
   165     public function preDispatch()
       
   166     {
       
   167         $controller = $this->getRequest()->getControllerName();
       
   168         $action = $this->getRequest()->getActionName();
       
   169         $stats = ob_get_status(true);
       
   170         foreach ($stats as $status) {
       
   171             if ($status['name'] == 'Zend_Cache_Frontend_Page::_flush'
       
   172             || $status['name'] == 'Zend_Cache_Frontend_Capture::_flush') {
       
   173                 $obStarted = true;
       
   174             }
       
   175         }
       
   176         if (!isset($obStarted) && isset($this->_caching[$controller]) &&
       
   177         in_array($action, $this->_caching[$controller])) {
       
   178             $reqUri = $this->getRequest()->getRequestUri();
       
   179             $tags = array();
       
   180             if (isset($this->_tags[$controller][$action])
       
   181             && !empty($this->_tags[$controller][$action])) {
       
   182                 $tags = array_unique($this->_tags[$controller][$action]);
       
   183             }
       
   184             $extension = null;
       
   185             if (isset($this->_extensions[$controller][$action])) {
       
   186                 $extension = $this->_extensions[$controller][$action];
       
   187             }
       
   188             $this->getCache(Zend_Cache_Manager::PAGECACHE)
       
   189                 ->start($this->_encodeCacheId($reqUri), $tags, $extension);
       
   190         }
       
   191     }
       
   192     
       
   193     /**
       
   194      * Encode a Cache ID as hexadecimal. This is a workaround because Backend ID validation
       
   195      * is trapped in the Frontend classes. Will try to get this reversed for ZF 2.0
       
   196      * because it's a major annoyance to have IDs so restricted!
       
   197      *
       
   198      * @return string
       
   199      * @param string $requestUri
       
   200      */
       
   201     protected function _encodeCacheId($requestUri)
       
   202     {
       
   203         return bin2hex($requestUri);
       
   204     }
       
   205 
       
   206     /**
       
   207      * Set an instance of the Cache Manager for this helper
       
   208      *
       
   209      * @param Zend_Cache_Manager $manager
       
   210      * @return void
       
   211      */
       
   212     public function setManager(Zend_Cache_Manager $manager)
       
   213     {
       
   214         $this->_manager = $manager;
       
   215         return $this;
       
   216     }
       
   217 
       
   218     /**
       
   219      * Get the Cache Manager instance or instantiate the object if not
       
   220      * exists. Attempts to load from bootstrap if available.
       
   221      *
       
   222      * @return Zend_Cache_Manager
       
   223      */
       
   224     public function getManager()
       
   225     {
       
   226         if ($this->_manager !== null) {
       
   227             return $this->_manager;
       
   228         }
       
   229         $front = Zend_Controller_Front::getInstance();
       
   230         if ($front->getParam('bootstrap')
       
   231         && $front->getParam('bootstrap')->getResource('CacheManager')) {
       
   232             return $front->getParam('bootstrap')
       
   233                 ->getResource('CacheManager');
       
   234         }
       
   235         $this->_manager = new Zend_Cache_Manager;
       
   236         return $this->_manager;
       
   237     }
       
   238 
       
   239     /**
       
   240      * Return a list of actions for the current Controller marked for
       
   241      * caching
       
   242      *
       
   243      * @return array
       
   244      */
       
   245     public function getCacheableActions()
       
   246     {
       
   247         return $this->_caching;
       
   248     }
       
   249 
       
   250     /**
       
   251      * Return a list of tags set for all cacheable actions
       
   252      *
       
   253      * @return array
       
   254      */
       
   255     public function getCacheableTags()
       
   256     {
       
   257         return $this->_tags;
       
   258     }
       
   259 
       
   260     /**
       
   261      * Proxy non-matched methods back to Zend_Cache_Manager where
       
   262      * appropriate
       
   263      *
       
   264      * @param string $method
       
   265      * @param array $args
       
   266      * @return mixed
       
   267      */
       
   268     public function __call($method, $args)
       
   269     {
       
   270         if (method_exists($this->getManager(), $method)) {
       
   271             return call_user_func_array(
       
   272                 array($this->getManager(), $method), $args
       
   273             );
       
   274         }
       
   275         throw new Zend_Controller_Action_Exception('Method does not exist:'
       
   276             . $method);
       
   277     }
       
   278 
       
   279 }