Pipeline.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Utils;
  12. use Closure;
  13. use Psr\Container\ContainerInterface;
  14. /**
  15. * This file mostly code come from illuminate/pipe,
  16. * thanks Laravel Team provide such a useful class.
  17. */
  18. class Pipeline
  19. {
  20. /**
  21. * The container implementation.
  22. *
  23. * @var ContainerInterface
  24. */
  25. protected $container;
  26. /**
  27. * The object being passed through the pipeline.
  28. *
  29. * @var mixed
  30. */
  31. protected $passable;
  32. /**
  33. * The array of class pipes.
  34. *
  35. * @var array
  36. */
  37. protected $pipes = [];
  38. /**
  39. * The method to call on each pipe.
  40. *
  41. * @var string
  42. */
  43. protected $method = 'handle';
  44. public function __construct(ContainerInterface $container)
  45. {
  46. $this->container = $container;
  47. }
  48. /**
  49. * Set the object being sent through the pipeline.
  50. * @param mixed $passable
  51. */
  52. public function send($passable): self
  53. {
  54. $this->passable = $passable;
  55. return $this;
  56. }
  57. /**
  58. * Set the array of pipes.
  59. *
  60. * @param array|mixed $pipes
  61. */
  62. public function through($pipes): self
  63. {
  64. $this->pipes = is_array($pipes) ? $pipes : func_get_args();
  65. return $this;
  66. }
  67. /**
  68. * Set the method to call on the pipes.
  69. */
  70. public function via(string $method): self
  71. {
  72. $this->method = $method;
  73. return $this;
  74. }
  75. /**
  76. * Run the pipeline with a final destination callback.
  77. */
  78. public function then(Closure $destination)
  79. {
  80. $pipeline = array_reduce(array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination));
  81. return $pipeline($this->passable);
  82. }
  83. /**
  84. * Get the final piece of the Closure onion.
  85. */
  86. protected function prepareDestination(Closure $destination): Closure
  87. {
  88. return static function ($passable) use ($destination) {
  89. return $destination($passable);
  90. };
  91. }
  92. /**
  93. * Get a Closure that represents a slice of the application onion.
  94. */
  95. protected function carry(): Closure
  96. {
  97. return function ($stack, $pipe) {
  98. return function ($passable) use ($stack, $pipe) {
  99. if (is_callable($pipe)) {
  100. // If the pipe is an instance of a Closure, we will just call it directly but
  101. // otherwise we'll resolve the pipes out of the container and call it with
  102. // the appropriate method and arguments, returning the results back out.
  103. return $pipe($passable, $stack);
  104. }
  105. if (! is_object($pipe)) {
  106. [$name, $parameters] = $this->parsePipeString($pipe);
  107. // If the pipe is a string we will parse the string and resolve the class out
  108. // of the dependency injection container. We can then build a callable and
  109. // execute the pipe function giving in the parameters that are required.
  110. $pipe = $this->container->get($name);
  111. $parameters = array_merge([$passable, $stack], $parameters);
  112. } else {
  113. // If the pipe is already an object we'll just make a callable and pass it to
  114. // the pipe as-is. There is no need to do any extra parsing and formatting
  115. // since the object we're given was already a fully instantiated object.
  116. $parameters = [$passable, $stack];
  117. }
  118. $carry = method_exists($pipe, $this->method) ? $pipe->{$this->method}(...$parameters) : $pipe(...$parameters);
  119. return $this->handleCarry($carry);
  120. };
  121. };
  122. }
  123. /**
  124. * Parse full pipe string to get name and parameters.
  125. *
  126. * @param string $pipe
  127. * @return array
  128. */
  129. protected function parsePipeString($pipe)
  130. {
  131. [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []);
  132. if (is_string($parameters)) {
  133. $parameters = explode(',', $parameters);
  134. }
  135. return [$name, $parameters];
  136. }
  137. /**
  138. * Handle the value returned from each pipe before passing it to the next.
  139. *
  140. * @param mixed $carry
  141. * @return mixed
  142. */
  143. protected function handleCarry($carry)
  144. {
  145. return $carry;
  146. }
  147. }