3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\EventManager
;
12 use Zend\Stdlib\CallbackHandler
;
15 * FilterChain: intercepting filter manager
17 class FilterChain
implements Filter\FilterInterface
20 * @var Filter\FilterIterator All filters
27 * Initializes Filter\FilterIterator in which filters will be aggregated
29 public function __construct()
31 $this->filters
= new Filter\
FilterIterator();
37 * Begins iteration of the filters.
39 * @param mixed $context Object under observation
40 * @param mixed $argv Associative array of arguments
43 public function run($context, array $argv = array())
45 $chain = clone $this->getFilters();
47 if ($chain->isEmpty()) {
51 $next = $chain->extract();
52 if (!$next instanceof CallbackHandler
) {
56 return call_user_func($next->getCallback(), $context, $argv, $chain);
60 * Connect a filter to the chain
62 * @param callable $callback PHP Callback
63 * @param int $priority Priority in the queue at which to execute; defaults to 1 (higher numbers == higher priority)
64 * @return CallbackHandler (to allow later unsubscribe)
65 * @throws Exception\InvalidCallbackException
67 public function attach($callback, $priority = 1)
69 if (empty($callback)) {
70 throw new Exception\
InvalidCallbackException('No callback provided');
72 $filter = new CallbackHandler($callback, array('priority' => $priority));
73 $this->filters
->insert($filter, $priority);
78 * Detach a filter from the chain
80 * @param CallbackHandler $filter
81 * @return bool Returns true if filter found and unsubscribed; returns false otherwise
83 public function detach(CallbackHandler
$filter)
85 return $this->filters
->remove($filter);
89 * Retrieve all filters
91 * @return Filter\FilterIterator
93 public function getFilters()
95 return $this->filters
;
103 public function clearFilters()
105 $this->filters
= new Filter\
FilterIterator();
109 * Return current responses
111 * Only available while the chain is still being iterated. Returns the
112 * current ResponseCollection.
114 * @return null|ResponseCollection
116 public function getResponses()