Pipeline.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace Illuminate\Pipeline;
  3. use Closure;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
  6. use Illuminate\Support\Traits\Conditionable;
  7. use RuntimeException;
  8. use Throwable;
  9. class Pipeline implements PipelineContract
  10. {
  11. use Conditionable;
  12. /**
  13. * The container implementation.
  14. *
  15. * @var \Illuminate\Contracts\Container\Container|null
  16. */
  17. protected $container;
  18. /**
  19. * The object being passed through the pipeline.
  20. *
  21. * @var mixed
  22. */
  23. protected $passable;
  24. /**
  25. * The array of class pipes.
  26. *
  27. * @var array
  28. */
  29. protected $pipes = [];
  30. /**
  31. * The method to call on each pipe.
  32. *
  33. * @var string
  34. */
  35. protected $method = 'handle';
  36. /**
  37. * Create a new class instance.
  38. *
  39. * @param \Illuminate\Contracts\Container\Container|null $container
  40. * @return void
  41. */
  42. public function __construct(?Container $container = null)
  43. {
  44. $this->container = $container;
  45. }
  46. /**
  47. * Set the object being sent through the pipeline.
  48. *
  49. * @param mixed $passable
  50. * @return $this
  51. */
  52. public function send($passable)
  53. {
  54. $this->passable = $passable;
  55. return $this;
  56. }
  57. /**
  58. * Set the array of pipes.
  59. *
  60. * @param array|mixed $pipes
  61. * @return $this
  62. */
  63. public function through($pipes)
  64. {
  65. $this->pipes = is_array($pipes) ? $pipes : func_get_args();
  66. return $this;
  67. }
  68. /**
  69. * Push additional pipes onto the pipeline.
  70. *
  71. * @param array|mixed $pipes
  72. * @return $this
  73. */
  74. public function pipe($pipes)
  75. {
  76. array_push($this->pipes, ...(is_array($pipes) ? $pipes : func_get_args()));
  77. return $this;
  78. }
  79. /**
  80. * Set the method to call on the pipes.
  81. *
  82. * @param string $method
  83. * @return $this
  84. */
  85. public function via($method)
  86. {
  87. $this->method = $method;
  88. return $this;
  89. }
  90. /**
  91. * Run the pipeline with a final destination callback.
  92. *
  93. * @param \Closure $destination
  94. * @return mixed
  95. */
  96. public function then(Closure $destination)
  97. {
  98. $pipeline = array_reduce(
  99. array_reverse($this->pipes()), $this->carry(), $this->prepareDestination($destination)
  100. );
  101. return $pipeline($this->passable);
  102. }
  103. /**
  104. * Run the pipeline and return the result.
  105. *
  106. * @return mixed
  107. */
  108. public function thenReturn()
  109. {
  110. return $this->then(function ($passable) {
  111. return $passable;
  112. });
  113. }
  114. /**
  115. * Get the final piece of the Closure onion.
  116. *
  117. * @param \Closure $destination
  118. * @return \Closure
  119. */
  120. protected function prepareDestination(Closure $destination)
  121. {
  122. return function ($passable) use ($destination) {
  123. try {
  124. return $destination($passable);
  125. } catch (Throwable $e) {
  126. return $this->handleException($passable, $e);
  127. }
  128. };
  129. }
  130. /**
  131. * Get a Closure that represents a slice of the application onion.
  132. *
  133. * @return \Closure
  134. */
  135. protected function carry()
  136. {
  137. return function ($stack, $pipe) {
  138. return function ($passable) use ($stack, $pipe) {
  139. try {
  140. if (is_callable($pipe)) {
  141. // If the pipe is a callable, then we will call it directly, but otherwise we
  142. // will resolve the pipes out of the dependency container and call it with
  143. // the appropriate method and arguments, returning the results back out.
  144. return $pipe($passable, $stack);
  145. } elseif (! is_object($pipe)) {
  146. [$name, $parameters] = $this->parsePipeString($pipe);
  147. // If the pipe is a string we will parse the string and resolve the class out
  148. // of the dependency injection container. We can then build a callable and
  149. // execute the pipe function giving in the parameters that are required.
  150. $pipe = $this->getContainer()->make($name);
  151. $parameters = array_merge([$passable, $stack], $parameters);
  152. } else {
  153. // If the pipe is already an object we'll just make a callable and pass it to
  154. // the pipe as-is. There is no need to do any extra parsing and formatting
  155. // since the object we're given was already a fully instantiated object.
  156. $parameters = [$passable, $stack];
  157. }
  158. $carry = method_exists($pipe, $this->method)
  159. ? $pipe->{$this->method}(...$parameters)
  160. : $pipe(...$parameters);
  161. return $this->handleCarry($carry);
  162. } catch (Throwable $e) {
  163. return $this->handleException($passable, $e);
  164. }
  165. };
  166. };
  167. }
  168. /**
  169. * Parse full pipe string to get name and parameters.
  170. *
  171. * @param string $pipe
  172. * @return array
  173. */
  174. protected function parsePipeString($pipe)
  175. {
  176. [$name, $parameters] = array_pad(explode(':', $pipe, 2), 2, []);
  177. if (is_string($parameters)) {
  178. $parameters = explode(',', $parameters);
  179. }
  180. return [$name, $parameters];
  181. }
  182. /**
  183. * Get the array of configured pipes.
  184. *
  185. * @return array
  186. */
  187. protected function pipes()
  188. {
  189. return $this->pipes;
  190. }
  191. /**
  192. * Get the container instance.
  193. *
  194. * @return \Illuminate\Contracts\Container\Container
  195. *
  196. * @throws \RuntimeException
  197. */
  198. protected function getContainer()
  199. {
  200. if (! $this->container) {
  201. throw new RuntimeException('A container instance has not been passed to the Pipeline.');
  202. }
  203. return $this->container;
  204. }
  205. /**
  206. * Set the container instance.
  207. *
  208. * @param \Illuminate\Contracts\Container\Container $container
  209. * @return $this
  210. */
  211. public function setContainer(Container $container)
  212. {
  213. $this->container = $container;
  214. return $this;
  215. }
  216. /**
  217. * Handle the value returned from each pipe before passing it to the next.
  218. *
  219. * @param mixed $carry
  220. * @return mixed
  221. */
  222. protected function handleCarry($carry)
  223. {
  224. return $carry;
  225. }
  226. /**
  227. * Handle the given exception.
  228. *
  229. * @param mixed $passable
  230. * @param \Throwable $e
  231. * @return mixed
  232. *
  233. * @throws \Throwable
  234. */
  235. protected function handleException($passable, Throwable $e)
  236. {
  237. throw $e;
  238. }
  239. }