Hub.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. */
  25. public function __construct(?Container $container = null)
  26. {
  27. $this->container = $container;
  28. }
  29. /**
  30. * Define the default named pipeline.
  31. *
  32. * @param \Closure $callback
  33. * @return void
  34. */
  35. public function defaults(Closure $callback)
  36. {
  37. $this->pipeline('default', $callback);
  38. }
  39. /**
  40. * Define a new named pipeline.
  41. *
  42. * @param string $name
  43. * @param \Closure $callback
  44. * @return void
  45. */
  46. public function pipeline($name, Closure $callback)
  47. {
  48. $this->pipelines[$name] = $callback;
  49. }
  50. /**
  51. * Send an object through one of the available pipelines.
  52. *
  53. * @param mixed $object
  54. * @param string|null $pipeline
  55. * @return mixed
  56. */
  57. public function pipe($object, $pipeline = null)
  58. {
  59. $pipeline = $pipeline ?: 'default';
  60. return call_user_func(
  61. $this->pipelines[$pipeline], new Pipeline($this->container), $object
  62. );
  63. }
  64. /**
  65. * Get the container instance used by the hub.
  66. *
  67. * @return \Illuminate\Contracts\Container\Container
  68. */
  69. public function getContainer()
  70. {
  71. return $this->container;
  72. }
  73. /**
  74. * Set the container instance used by the hub.
  75. *
  76. * @param \Illuminate\Contracts\Container\Container $container
  77. * @return $this
  78. */
  79. public function setContainer(Container $container)
  80. {
  81. $this->container = $container;
  82. return $this;
  83. }
  84. }