File indexing completed on 2024-05-12 06:02:28

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_EventManager
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 // require_once 'Zend/EventManager/Filter.php';
0022 // require_once 'Zend/EventManager/Filter/FilterIterator.php';
0023 // require_once 'Zend/Stdlib/CallbackHandler.php';
0024 
0025 /**
0026  * FilterChain: intercepting filter manager
0027  *
0028  * @category   Zend
0029  * @package    Zend_EventManager
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_EventManager_FilterChain implements Zend_EventManager_Filter
0034 {
0035     /**
0036      * @var Zend_EventManager_Filter_FilterIterator All filters
0037      */
0038     protected $filters;
0039 
0040     /**
0041      * Constructor
0042      *
0043      * Initializes Zend_EventManager_Filter_FilterIterator in which filters will be aggregated
0044      * 
0045      * @return void
0046      */
0047     public function __construct()
0048     {
0049         $this->filters = new Zend_EventManager_Filter_FilterIterator();
0050     }
0051 
0052     /**
0053      * Apply the filters
0054      *
0055      * Begins iteration of the filters.
0056      * 
0057      * @param  mixed $context Object under observation
0058      * @param  mixed $argv Associative array of arguments
0059      * @return mixed
0060      */
0061     public function run($context, array $argv = array())
0062     {
0063         $chain = clone $this->getFilters();
0064 
0065         if ($chain->isEmpty()) {
0066             return;
0067         }
0068 
0069         $next = $chain->extract();
0070         if (!$next instanceof Zend_Stdlib_CallbackHandler) {
0071             return;
0072         }
0073 
0074         return call_user_func($next->getCallback(), $context, $argv, $chain);
0075     }
0076 
0077     /**
0078      * Connect a filter to the chain
0079      *
0080      * @param  callback $callback PHP Callback
0081      * @param  int      $priority Priority in the queue at which to execute; defaults to 1 (higher numbers == higher priority)
0082      * @throws Zend_Stdlib_Exception_InvalidCallbackException
0083      * @return Zend_Stdlib_CallbackHandler (to allow later unsubscribe)
0084      */
0085     public function attach($callback, $priority = 1)
0086     {
0087         if (empty($callback)) {
0088             // require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
0089             throw new Zend_Stdlib_Exception_InvalidCallbackException('No callback provided');
0090         }
0091         $filter = new Zend_Stdlib_CallbackHandler($callback, array('priority' => $priority));
0092         $this->filters->insert($filter, $priority);
0093         return $filter;
0094     }
0095 
0096     /**
0097      * Detach a filter from the chain
0098      * 
0099      * @param  Zend_Stdlib_CallbackHandler $filter 
0100      * @return bool Returns true if filter found and unsubscribed; returns false otherwise
0101      */
0102     public function detach(Zend_Stdlib_CallbackHandler $filter)
0103     {
0104         return $this->filters->remove($filter);
0105     }
0106 
0107     /**
0108      * Retrieve all filters
0109      * 
0110      * @return Zend_EventManager_Filter_FilterIterator
0111      */
0112     public function getFilters()
0113     {
0114         return $this->filters;
0115     }
0116 
0117     /**
0118      * Clear all filters
0119      * 
0120      * @return void
0121      */
0122     public function clearFilters()
0123     {
0124         $this->filters = new Zend_EventManager_Filter_FilterIterator();
0125     }
0126 
0127     /**
0128      * Return current responses
0129      *
0130      * Only available while the chain is still being iterated. Returns the 
0131      * current ResponseCollection.
0132      * 
0133      * @return null|Zend_EventManager_ResponseCollection
0134      */
0135     public function getResponses()
0136     {
0137         return $this->responses;
0138     }
0139 }