123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Predis\Command\Processor;
- use Predis\Command\CommandInterface;
- class ProcessorChain implements CommandProcessorChainInterface, \ArrayAccess
- {
- private $processors = array();
-
- public function __construct($processors = array())
- {
- foreach ($processors as $processor) {
- $this->add($processor);
- }
- }
-
- public function add(CommandProcessorInterface $processor)
- {
- $this->processors[] = $processor;
- }
-
- public function remove(CommandProcessorInterface $processor)
- {
- if (false !== $index = array_search($processor, $this->processors, true)) {
- unset($this[$index]);
- }
- }
-
- public function process(CommandInterface $command)
- {
- for ($i = 0; $i < $count = count($this->processors); $i++) {
- $this->processors[$i]->process($command);
- }
- }
-
- public function getProcessors()
- {
- return $this->processors;
- }
-
- public function getIterator()
- {
- return new \ArrayIterator($this->processors);
- }
-
- public function count()
- {
- return count($this->processors);
- }
-
- public function offsetExists($index)
- {
- return isset($this->processors[$index]);
- }
-
- public function offsetGet($index)
- {
- return $this->processors[$index];
- }
-
- public function offsetSet($index, $processor)
- {
- if (!$processor instanceof CommandProcessorInterface) {
- throw new \InvalidArgumentException(
- 'A processor chain can hold only instances of classes implementing '.
- 'the Predis\Command\Processor\CommandProcessorInterface interface'
- );
- }
- $this->processors[$index] = $processor;
- }
-
- public function offsetUnset($index)
- {
- unset($this->processors[$index]);
- $this->processors = array_values($this->processors);
- }
- }
|