ProcessorChain.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Predis\Commands\Processors;
  3. class ProcessorChain implements ICommandProcessorChain, \ArrayAccess {
  4. private $_processors;
  5. public function __construct($processors = array()) {
  6. foreach ($processors as $processor) {
  7. $this->add($processor);
  8. }
  9. }
  10. public function add(ICommandProcessor $processor) {
  11. $this->_processors[] = $processor;
  12. }
  13. public function remove(ICommandProcessor $processor) {
  14. $index = array_search($processor, $this->_processors, true);
  15. if ($index !== false) {
  16. unset($this->_processors);
  17. }
  18. }
  19. public function process($method, &$arguments) {
  20. $count = count($this->_processors);
  21. for ($i = 0; $i < $count; $i++) {
  22. $this->_processors[$i]->process($method, $arguments);
  23. }
  24. }
  25. public function getProcessors() {
  26. return $this->_processors;
  27. }
  28. public function getIterator() {
  29. return new \ArrayIterator($this->_processors);
  30. }
  31. public function count() {
  32. return count($this->_processors);
  33. }
  34. public function offsetExists($index) {
  35. return isset($this->_processors[$index]);
  36. }
  37. public function offsetGet($index) {
  38. return $this->_processors[$index];
  39. }
  40. public function offsetSet($index, $processor) {
  41. if (!$processor instanceof ICommandProcessor) {
  42. throw new \InvalidArgumentException(
  43. 'A processor chain can hold only instances of classes implementing '.
  44. 'the Predis\Commands\Preprocessors\ICommandProcessor interface'
  45. );
  46. }
  47. $this->_processors[$index] = $processor;
  48. }
  49. public function offsetUnset($index) {
  50. unset($this->_processors[$index]);
  51. }
  52. }