選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ContextualBindingBuilder.php 1.6KB

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