ContextualBindingBuilder.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Illuminate\Container;
  3. use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
  4. class ContextualBindingBuilder implements ContextualBindingBuilderContract
  5. {
  6. /**
  7. * The underlying container instance.
  8. *
  9. * @var \Illuminate\Container\Container
  10. */
  11. protected $container;
  12. /**
  13. * The concrete instance.
  14. *
  15. * @var string
  16. */
  17. protected $concrete;
  18. /**
  19. * The abstract target.
  20. *
  21. * @var string
  22. */
  23. protected $needs;
  24. /**
  25. * Create a new contextual binding builder.
  26. *
  27. * @param \Illuminate\Container\Container $container
  28. * @param string $concrete
  29. * @return void
  30. */
  31. public function __construct(Container $container, $concrete)
  32. {
  33. $this->concrete = $concrete;
  34. $this->container = $container;
  35. }
  36. /**
  37. * Define the abstract target that depends on the context.
  38. *
  39. * @param string $abstract
  40. * @return $this
  41. */
  42. public function needs($abstract)
  43. {
  44. $this->needs = $abstract;
  45. return $this;
  46. }
  47. /**
  48. * Define the implementation for the contextual binding.
  49. *
  50. * @param \Closure|string $implementation
  51. * @return void
  52. */
  53. public function give($implementation)
  54. {
  55. $this->container->addContextualBinding(
  56. $this->concrete, $this->needs, $implementation
  57. );
  58. }
  59. }