ProcessorChain.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Command\Processor;
  11. use ArrayAccess;
  12. use ArrayIterator;
  13. use InvalidArgumentException;
  14. use Predis\Command\CommandInterface;
  15. /**
  16. * Default implementation of a command processors chain.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class ProcessorChain implements ArrayAccess, ProcessorInterface
  21. {
  22. private $processors = array();
  23. /**
  24. * @param array $processors List of instances of ProcessorInterface.
  25. */
  26. public function __construct($processors = array())
  27. {
  28. foreach ($processors as $processor) {
  29. $this->add($processor);
  30. }
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function add(ProcessorInterface $processor)
  36. {
  37. $this->processors[] = $processor;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function remove(ProcessorInterface $processor)
  43. {
  44. if (false !== $index = array_search($processor, $this->processors, true)) {
  45. unset($this[$index]);
  46. }
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function process(CommandInterface $command)
  52. {
  53. for ($i = 0; $i < $count = count($this->processors); $i++) {
  54. $this->processors[$i]->process($command);
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getProcessors()
  61. {
  62. return $this->processors;
  63. }
  64. /**
  65. * Returns an iterator over the list of command processor in the chain.
  66. *
  67. * @return ArrayIterator
  68. */
  69. public function getIterator()
  70. {
  71. return new ArrayIterator($this->processors);
  72. }
  73. /**
  74. * Returns the number of command processors in the chain.
  75. *
  76. * @return int
  77. */
  78. public function count()
  79. {
  80. return count($this->processors);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function offsetExists($index)
  86. {
  87. return isset($this->processors[$index]);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function offsetGet($index)
  93. {
  94. return $this->processors[$index];
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function offsetSet($index, $processor)
  100. {
  101. if (!$processor instanceof ProcessorInterface) {
  102. throw new InvalidArgumentException(
  103. "A processor chain accepts only instances of ".
  104. "'Predis\Command\Processor\ProcessorInterface'."
  105. );
  106. }
  107. $this->processors[$index] = $processor;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function offsetUnset($index)
  113. {
  114. unset($this->processors[$index]);
  115. $this->processors = array_values($this->processors);
  116. }
  117. }