ProcessorChain.php 2.8 KB

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