Hub.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }