Pipeline.php 4.2 KB

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