ProcessorChain.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Predis\Command\CommandInterface;
  12. /**
  13. * Default implementation of a command processors chain.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class ProcessorChain implements CommandProcessorChainInterface, \ArrayAccess
  18. {
  19. private $processors = array();
  20. /**
  21. * @param array $processors List of instances of CommandProcessorInterface.
  22. */
  23. public function __construct($processors = array())
  24. {
  25. foreach ($processors as $processor) {
  26. $this->add($processor);
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function add(CommandProcessorInterface $processor)
  33. {
  34. $this->processors[] = $processor;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function remove(CommandProcessorInterface $processor)
  40. {
  41. if (false !== $index = array_search($processor, $this->processors, true)) {
  42. unset($this[$index]);
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function process(CommandInterface $command)
  49. {
  50. for ($i = 0; $i < $count = count($this->processors); $i++) {
  51. $this->processors[$i]->process($command);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getProcessors()
  58. {
  59. return $this->processors;
  60. }
  61. /**
  62. * Returns an iterator over the list of command processor in the chain.
  63. *
  64. * @return \ArrayIterator
  65. */
  66. public function getIterator()
  67. {
  68. return new \ArrayIterator($this->processors);
  69. }
  70. /**
  71. * Returns the number of command processors in the chain.
  72. *
  73. * @return int
  74. */
  75. public function count()
  76. {
  77. return count($this->processors);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function offsetExists($index)
  83. {
  84. return isset($this->processors[$index]);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function offsetGet($index)
  90. {
  91. return $this->processors[$index];
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function offsetSet($index, $processor)
  97. {
  98. if (!$processor instanceof CommandProcessorInterface) {
  99. throw new \InvalidArgumentException(
  100. 'A processor chain can hold only instances of classes implementing '.
  101. 'the Predis\Command\Processor\CommandProcessorInterface interface'
  102. );
  103. }
  104. $this->processors[$index] = $processor;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function offsetUnset($index)
  110. {
  111. unset($this->processors[$index]);
  112. $this->processors = array_values($this->processors);
  113. }
  114. }