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