master

laravel/framework

Last updated at: 29/12/2023 09:20

ContextualBindingBuilder.php

TLDR

The ContextualBindingBuilder.php file in Illuminate\Container namespace contains a class ContextualBindingBuilder that is responsible for defining contextual bindings in the Laravel container.

Methods

__construct

The __construct method is the constructor for the ContextualBindingBuilder class. It initializes the $container and $concrete properties of the class.

needs

The needs method is used to define the abstract target that depends on the context. It takes in a string representing the abstract target and returns the instance of the ContextualBindingBuilder class.

give

The give method is used to define the implementation for the contextual binding. It takes in a Closure, string, or array representing the implementation and adds the contextual binding to the container.

giveTagged

The giveTagged method is used to define tagged services to be used as the implementation for the contextual binding. It takes in a string representing the tag and adds the tagged services as the implementation to the container.

giveConfig

The giveConfig method is used to specify a configuration item to bind as a primitive. It takes in a string representing the configuration key and an optional default value.

Classes

No classes to document.

<?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
     * @return void
     */
    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 void
     */
    public function give($implementation)
    {
        foreach (Util::arrayWrap($this->concrete) as $concrete) {
            $this->container->addContextualBinding($concrete, $this->needs, $implementation);
        }
    }

    /**
     * Define tagged services to be used as the implementation for the contextual binding.
     *
     * @param  string  $tag
     * @return void
     */
    public function giveTagged($tag)
    {
        $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 void
     */
    public function giveConfig($key, $default = null)
    {
        $this->give(fn ($container) => $container->get('config')->get($key, $default));
    }
}