PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php

https://gitlab.com/ealexis.t/trends
PHP | 170 lines | 70 code | 24 blank | 76 comment | 3 complexity | e3ae86e8e7a5146d58a6975feb3f22dc MD5 | raw file
  1. <?php
  2. namespace Illuminate\Pipeline;
  3. use Closure;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
  6. class Pipeline implements PipelineContract
  7. {
  8. /**
  9. * The container implementation.
  10. *
  11. * @var \Illuminate\Contracts\Container\Container
  12. */
  13. protected $container;
  14. /**
  15. * The object being passed through the pipeline.
  16. *
  17. * @var mixed
  18. */
  19. protected $passable;
  20. /**
  21. * The array of class pipes.
  22. *
  23. * @var array
  24. */
  25. protected $pipes = [];
  26. /**
  27. * The method to call on each pipe.
  28. *
  29. * @var string
  30. */
  31. protected $method = 'handle';
  32. /**
  33. * Create a new class instance.
  34. *
  35. * @param \Illuminate\Contracts\Container\Container $container
  36. * @return void
  37. */
  38. public function __construct(Container $container)
  39. {
  40. $this->container = $container;
  41. }
  42. /**
  43. * Set the object being sent through the pipeline.
  44. *
  45. * @param mixed $passable
  46. * @return $this
  47. */
  48. public function send($passable)
  49. {
  50. $this->passable = $passable;
  51. return $this;
  52. }
  53. /**
  54. * Set the array of pipes.
  55. *
  56. * @param array|mixed $pipes
  57. * @return $this
  58. */
  59. public function through($pipes)
  60. {
  61. $this->pipes = is_array($pipes) ? $pipes : func_get_args();
  62. return $this;
  63. }
  64. /**
  65. * Set the method to call on the pipes.
  66. *
  67. * @param string $method
  68. * @return $this
  69. */
  70. public function via($method)
  71. {
  72. $this->method = $method;
  73. return $this;
  74. }
  75. /**
  76. * Run the pipeline with a final destination callback.
  77. *
  78. * @param \Closure $destination
  79. * @return mixed
  80. */
  81. public function then(Closure $destination)
  82. {
  83. $firstSlice = $this->getInitialSlice($destination);
  84. $pipes = array_reverse($this->pipes);
  85. return call_user_func(
  86. array_reduce($pipes, $this->getSlice(), $firstSlice), $this->passable
  87. );
  88. }
  89. /**
  90. * Get a Closure that represents a slice of the application onion.
  91. *
  92. * @return \Closure
  93. */
  94. protected function getSlice()
  95. {
  96. return function ($stack, $pipe) {
  97. return function ($passable) use ($stack, $pipe) {
  98. if ($pipe instanceof Closure) {
  99. // If the pipe is an instance of a Closure, we will just call it directly but
  100. // otherwise we'll resolve the pipes out of the container and call it with
  101. // the appropriate method and arguments, returning the results back out.
  102. return call_user_func($pipe, $passable, $stack);
  103. } elseif (! is_object($pipe)) {
  104. list($name, $parameters) = $this->parsePipeString($pipe);
  105. // If the pipe is a string we will parse the string and resolve the class out
  106. // of the dependency injection container. We can then build a callable and
  107. // execute the pipe function giving in the parameters that are required.
  108. $pipe = $this->container->make($name);
  109. $parameters = array_merge([$passable, $stack], $parameters);
  110. } else {
  111. // If the pipe is already an object we'll just make a callable and pass it to
  112. // the pipe as-is. There is no need to do any extra parsing and formatting
  113. // since the object we're given was already a fully instantiated object.
  114. $parameters = [$passable, $stack];
  115. }
  116. return call_user_func_array([$pipe, $this->method], $parameters);
  117. };
  118. };
  119. }
  120. /**
  121. * Get the initial slice to begin the stack call.
  122. *
  123. * @param \Closure $destination
  124. * @return \Closure
  125. */
  126. protected function getInitialSlice(Closure $destination)
  127. {
  128. return function ($passable) use ($destination) {
  129. return call_user_func($destination, $passable);
  130. };
  131. }
  132. /**
  133. * Parse full pipe string to get name and parameters.
  134. *
  135. * @param string $pipe
  136. * @return array
  137. */
  138. protected function parsePipeString($pipe)
  139. {
  140. list($name, $parameters) = array_pad(explode(':', $pipe, 2), 2, []);
  141. if (is_string($parameters)) {
  142. $parameters = explode(',', $parameters);
  143. }
  144. return [$name, $parameters];
  145. }
  146. }