| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace Illuminate\Container;
- use Illuminate\Contracts\Container\Container;
- use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
- class ContextualBindingBuilder implements ContextualBindingBuilderContract
- {
- /**
- * The underlying container instance.
- *
- * @var \Illuminate\Contracts\Container\Container
- */
- protected $container;
- /**
- * The concrete instance.
- *
- * @var string|array
- */
- protected $concrete;
- /**
- * The abstract target.
- *
- * @var string
- */
- protected $needs;
- /**
- * Create a new contextual binding builder.
- *
- * @param \Illuminate\Contracts\Container\Container $container
- * @param string|array $concrete
- */
- public function __construct(Container $container, $concrete)
- {
- $this->concrete = $concrete;
- $this->container = $container;
- }
- /**
- * Define the abstract target that depends on the context.
- *
- * @param string $abstract
- * @return $this
- */
- public function needs($abstract)
- {
- $this->needs = $abstract;
- return $this;
- }
- /**
- * Define the implementation for the contextual binding.
- *
- * @param \Closure|string|array $implementation
- * @return $this
- */
- public function give($implementation)
- {
- foreach (Util::arrayWrap($this->concrete) as $concrete) {
- $this->container->addContextualBinding($concrete, $this->needs, $implementation);
- }
- return $this;
- }
- /**
- * Define tagged services to be used as the implementation for the contextual binding.
- *
- * @param string $tag
- * @return $this
- */
- public function giveTagged($tag)
- {
- return $this->give(function ($container) use ($tag) {
- $taggedServices = $container->tagged($tag);
- return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
- });
- }
- /**
- * Specify the configuration item to bind as a primitive.
- *
- * @param string $key
- * @param mixed $default
- * @return $this
- */
- public function giveConfig($key, $default = null)
- {
- return $this->give(fn ($container) => $container->get('config')->get($key, $default));
- }
- }
|