master

laravel/framework

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

EngineResolver.php

TLDR

The EngineResolver.php file in the Illuminate\View\Engines namespace contains a class called EngineResolver. This class is responsible for resolving and registering engine resolvers. It provides methods to register a new engine resolver, resolve an engine instance by name, and remove a resolved engine.

Methods

register

The register method allows you to register a new engine resolver. It takes two parameters: $engine (string) and $resolver (Closure). The $engine parameter typically corresponds to a file extension. This method overwrites any existing engine resolver with the same name.

resolve

The resolve method is used to resolve an engine instance by name. It takes the $engine parameter (string) and returns an instance of the \Illuminate\Contracts\View\Engine interface. If the engine instance has been previously resolved, it will be returned from the $resolved array. If the engine resolver exists, it will be called using the call_user_func function and the resulting instance will be stored in the $resolved array. If the engine resolver does not exist, an InvalidArgumentException will be thrown.

forget

The forget method allows you to remove a resolved engine. It takes the $engine parameter (string) and removes the corresponding engine instance from the $resolved array.

Classes

No classes are defined in this file.

<?php

namespace Illuminate\View\Engines;

use Closure;
use InvalidArgumentException;

class EngineResolver
{
    /**
     * The array of engine resolvers.
     *
     * @var array
     */
    protected $resolvers = [];

    /**
     * The resolved engine instances.
     *
     * @var array
     */
    protected $resolved = [];

    /**
     * Register a new engine resolver.
     *
     * The engine string typically corresponds to a file extension.
     *
     * @param  string  $engine
     * @param  \Closure  $resolver
     * @return void
     */
    public function register($engine, Closure $resolver)
    {
        $this->forget($engine);

        $this->resolvers[$engine] = $resolver;
    }

    /**
     * Resolve an engine instance by name.
     *
     * @param  string  $engine
     * @return \Illuminate\Contracts\View\Engine
     *
     * @throws \InvalidArgumentException
     */
    public function resolve($engine)
    {
        if (isset($this->resolved[$engine])) {
            return $this->resolved[$engine];
        }

        if (isset($this->resolvers[$engine])) {
            return $this->resolved[$engine] = call_user_func($this->resolvers[$engine]);
        }

        throw new InvalidArgumentException("Engine [{$engine}] not found.");
    }

    /**
     * Remove a resolved engine.
     *
     * @param  string  $engine
     * @return void
     */
    public function forget($engine)
    {
        unset($this->resolved[$engine]);
    }
}