* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Predis\Commands\Processors; use Predis\Commands\ICommand; /** * Default implementation of a command processors chain. * * @author Daniele Alessandri */ class ProcessorChain implements ICommandProcessorChain, \ArrayAccess { private $processors = array(); /** * @param array $processors List of instances of ICommandProcessor. */ public function __construct($processors = array()) { foreach ($processors as $processor) { $this->add($processor); } } /** * {@inheritdoc} */ public function add(ICommandProcessor $processor) { $this->processors[] = $processor; } /** * {@inheritdoc} */ public function remove(ICommandProcessor $processor) { $index = array_search($processor, $this->processors, true); if ($index !== false) { unset($this[$index]); } } /** * {@inheritdoc} */ public function process(ICommand $command) { $count = count($this->processors); for ($i = 0; $i < $count; $i++) { $this->processors[$i]->process($command); } } /** * {@inheritdoc} */ public function getProcessors() { return $this->processors; } /** * Returns an iterator over the list of command processor in the chain. * * @return \ArrayIterator */ public function getIterator() { return new \ArrayIterator($this->processors); } /** * Returns the number of command processors in the chain. * * @return int */ public function count() { return count($this->processors); } /** * {@inheritdoc} */ public function offsetExists($index) { return isset($this->processors[$index]); } /** * {@inheritdoc} */ public function offsetGet($index) { return $this->processors[$index]; } /** * {@inheritdoc} */ public function offsetSet($index, $processor) { if (!$processor instanceof ICommandProcessor) { throw new \InvalidArgumentException( 'A processor chain can hold only instances of classes implementing '. 'the Predis\Commands\Preprocessors\ICommandProcessor interface' ); } $this->processors[$index] = $processor; } /** * {@inheritdoc} */ public function offsetUnset($index) { unset($this->processors[$index]); $this->processors = array_values($this->processors); } }