master

laravel/framework

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

DynamicComponent.php

TLDR

This file defines the DynamicComponent class in the Illuminate\View namespace. The class extends the Component class and is responsible for rendering dynamic Blade components. It provides methods to generate the view and compile props, bindings, and slots for the component.

Methods

render

This method generates the view or contents that represent the component. It uses a template string and replaces placeholders with the component name, props, bindings, attributes, slots, and default slot. The generated content is returned as a closure function.

compileProps

This protected method compiles the @props directive for the component. It takes an array of bindings and converts them to a string representation of the props directive. If there are no bindings, an empty string is returned.

compileBindings

This protected method compiles the bindings for the component. It takes an array of bindings and converts them to a string representation of the bindings. Each binding is prefixed with a colon and the corresponding variable is assigned a value using the Str::camel helper function.

compileSlots

This protected method compiles the slots for the component. It takes an array of slots and converts them to a string representation of the slots. Each slot is wrapped in a <x-slot> tag and the corresponding variable is inserted using the {{ $var }} syntax. Slots without a name are filtered out.

classForComponent

This protected method retrieves the class for the current component. It checks if the class is already cached and returns it if so. Otherwise, it retrieves the class using the compiler() method.

bindings

This protected method retrieves the names of the variables that should be bound to the component. It takes a class name and the component attributes as arguments. It partitions the data and attributes using the compiler() method and returns the keys of the data array.

compiler

This protected method retrieves an instance of the BladeTagCompiler class. It checks if the compiler is already instantiated and returns it if so. Otherwise, it creates a new instance using the Blade compiler from the container.

<?php

namespace Illuminate\View;

use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler;

class DynamicComponent extends Component
{
    /**
     * The name of the component.
     *
     * @var string
     */
    public $component;

    /**
     * The component tag compiler instance.
     *
     * @var \Illuminate\View\Compilers\BladeTagCompiler
     */
    protected static $compiler;

    /**
     * The cached component classes.
     *
     * @var array
     */
    protected static $componentClasses = [];

    /**
     * Create a new component instance.
     *
     * @param  string  $component
     * @return void
     */
    public function __construct(string $component)
    {
        $this->component = $component;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|string
     */
    public function render()
    {
        $template = <<<'EOF'
<?php extract(collect($attributes->getAttributes())->mapWithKeys(function ($value, $key) { return [Illuminate\Support\Str::camel(str_replace([':', '.'], ' ', $key)) => $value]; })->all(), EXTR_SKIP); ?>
{{ props }}
<x-{{ component }} {{ bindings }} {{ attributes }}>
{{ slots }}
{{ defaultSlot }}
</x-{{ component }}>
EOF;

        return function ($data) use ($template) {
            $bindings = $this->bindings($class = $this->classForComponent());

            return str_replace(
                [
                    '{{ component }}',
                    '{{ props }}',
                    '{{ bindings }}',
                    '{{ attributes }}',
                    '{{ slots }}',
                    '{{ defaultSlot }}',
                ],
                [
                    $this->component,
                    $this->compileProps($bindings),
                    $this->compileBindings($bindings),
                    class_exists($class) ? '{{ $attributes }}' : '',
                    $this->compileSlots($data['__laravel_slots']),
                    '{{ $slot ?? "" }}',
                ],
                $template
            );
        };
    }

    /**
     * Compile the @props directive for the component.
     *
     * @param  array  $bindings
     * @return string
     */
    protected function compileProps(array $bindings)
    {
        if (empty($bindings)) {
            return '';
        }

        return '@props('.'[\''.implode('\',\'', collect($bindings)->map(function ($dataKey) {
            return Str::camel($dataKey);
        })->all()).'\']'.')';
    }

    /**
     * Compile the bindings for the component.
     *
     * @param  array  $bindings
     * @return string
     */
    protected function compileBindings(array $bindings)
    {
        return collect($bindings)->map(function ($key) {
            return ':'.$key.'="$'.Str::camel(str_replace([':', '.'], ' ', $key)).'"';
        })->implode(' ');
    }

    /**
     * Compile the slots for the component.
     *
     * @param  array  $slots
     * @return string
     */
    protected function compileSlots(array $slots)
    {
        return collect($slots)->map(function ($slot, $name) {
            return $name === '__default' ? null : '<x-slot name="'.$name.'" '.((string) $slot->attributes).'>{{ $'.$name.' }}</x-slot>';
        })->filter()->implode(PHP_EOL);
    }

    /**
     * Get the class for the current component.
     *
     * @return string
     */
    protected function classForComponent()
    {
        if (isset(static::$componentClasses[$this->component])) {
            return static::$componentClasses[$this->component];
        }

        return static::$componentClasses[$this->component] =
                    $this->compiler()->componentClass($this->component);
    }

    /**
     * Get the names of the variables that should be bound to the component.
     *
     * @param  string  $class
     * @return array
     */
    protected function bindings(string $class)
    {
        [$data, $attributes] = $this->compiler()->partitionDataAndAttributes($class, $this->attributes->getAttributes());

        return array_keys($data->all());
    }

    /**
     * Get an instance of the Blade tag compiler.
     *
     * @return \Illuminate\View\Compilers\ComponentTagCompiler
     */
    protected function compiler()
    {
        if (! static::$compiler) {
            static::$compiler = new ComponentTagCompiler(
                Container::getInstance()->make('blade.compiler')->getClassComponentAliases(),
                Container::getInstance()->make('blade.compiler')->getClassComponentNamespaces(),
                Container::getInstance()->make('blade.compiler')
            );
        }

        return static::$compiler;
    }
}