master

laravel/framework

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

CapsuleManagerTrait.php

TLDR

This file contains the CapsuleManagerTrait trait, which provides methods for setting up the IoC container instance, making the instance available globally, and getting or setting the container instance.

Methods

setupContainer

This method sets up the IoC container instance. It takes a Container object as a parameter and sets the container property of the class to that object. If the "config" binding is not already bound to the container, it binds it using a new Fluent object.

setAsGlobal

This method sets the current instance of the class as the globally used instance by assigning it to the static::$instance property.

getContainer

This method returns the IoC container instance.

setContainer

This method sets the IoC container instance. It takes a Container object as a parameter and sets the container property of the class to that object.

<?php

namespace Illuminate\Support\Traits;

use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Fluent;

trait CapsuleManagerTrait
{
    /**
     * The current globally used instance.
     *
     * @var object
     */
    protected static $instance;

    /**
     * The container instance.
     *
     * @var \Illuminate\Contracts\Container\Container
     */
    protected $container;

    /**
     * Setup the IoC container instance.
     *
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return void
     */
    protected function setupContainer(Container $container)
    {
        $this->container = $container;

        if (! $this->container->bound('config')) {
            $this->container->instance('config', new Fluent);
        }
    }

    /**
     * Make this capsule instance available globally.
     *
     * @return void
     */
    public function setAsGlobal()
    {
        static::$instance = $this;
    }

    /**
     * Get the IoC container instance.
     *
     * @return \Illuminate\Contracts\Container\Container
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Set the IoC container instance.
     *
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return void
     */
    public function setContainer(Container $container)
    {
        $this->container = $container;
    }
}