Hub.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Illuminate\Pipeline;
  3. use Closure;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Contracts\Pipeline\Hub as HubContract;
  6. class Hub implements HubContract
  7. {
  8. /**
  9. * The container implementation.
  10. *
  11. * @var \Illuminate\Contracts\Container\Container|null
  12. */
  13. protected $container;
  14. /**
  15. * All of the available pipelines.
  16. *
  17. * @var array
  18. */
  19. protected $pipelines = [];
  20. /**
  21. * Create a new Hub instance.
  22. *
  23. * @param \Illuminate\Contracts\Container\Container|null $container
  24. * @return void
  25. */
  26. public function __construct(?Container $container = null)
  27. {
  28. $this->container = $container;
  29. }
  30. /**
  31. * Define the default named pipeline.
  32. *
  33. * @param \Closure $callback
  34. * @return void
  35. */
  36. public function defaults(Closure $callback)
  37. {
  38. return $this->pipeline('default', $callback);
  39. }
  40. /**
  41. * Define a new named pipeline.
  42. *
  43. * @param string $name
  44. * @param \Closure $callback
  45. * @return void
  46. */
  47. public function pipeline($name, Closure $callback)
  48. {
  49. $this->pipelines[$name] = $callback;
  50. }
  51. /**
  52. * Send an object through one of the available pipelines.
  53. *
  54. * @param mixed $object
  55. * @param string|null $pipeline
  56. * @return mixed
  57. */
  58. public function pipe($object, $pipeline = null)
  59. {
  60. $pipeline = $pipeline ?: 'default';
  61. return call_user_func(
  62. $this->pipelines[$pipeline], new Pipeline($this->container), $object
  63. );
  64. }
  65. /**
  66. * Get the container instance used by the hub.
  67. *
  68. * @return \Illuminate\Contracts\Container\Container
  69. */
  70. public function getContainer()
  71. {
  72. return $this->container;
  73. }
  74. /**
  75. * Set the container instance used by the hub.
  76. *
  77. * @param \Illuminate\Contracts\Container\Container $container
  78. * @return $this
  79. */
  80. public function setContainer(Container $container)
  81. {
  82. $this->container = $container;
  83. return $this;
  84. }
  85. }