master

laravel/framework

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

RouteParameterBinder.php

TLDR

The RouteParameterBinder.php file is a part of the Illuminate\Routing namespace in the Demo Projects project. It contains a class called RouteParameterBinder that is responsible for binding parameters to a route. The class has various methods to handle different operations related to route parameters.

Methods

__construct

This method is the constructor of the RouteParameterBinder class. It initializes the class by accepting an instance of the Route class.

parameters

This method is used to get the parameters for a route. It takes an instance of the Request class as a parameter and returns an array of parameters.

bindPathParameters

This method is a protected method used by the parameters method. It extracts the parameter matches from the path portion of the URI based on the route's compiled regex.

bindHostParameters

This method is a protected method used by the parameters method. It extracts the parameter list from the host portion of the request.

matchToKeys

This method is a protected method used by the bindPathParameters and bindHostParameters methods. It combines a set of parameter matches with the route's keys.

replaceDefaults

This method is a protected method used by the parameters method. It replaces null parameters with their defaults.

Classes

Class: RouteParameterBinder

This class is responsible for binding parameters to a route. It takes an instance of the Route class as a constructor parameter and provides methods to get the parameters for a route.

<?php

namespace Illuminate\Routing;

use Illuminate\Support\Arr;

class RouteParameterBinder
{
    /**
     * The route instance.
     *
     * @var \Illuminate\Routing\Route
     */
    protected $route;

    /**
     * Create a new Route parameter binder instance.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @return void
     */
    public function __construct($route)
    {
        $this->route = $route;
    }

    /**
     * Get the parameters for the route.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function parameters($request)
    {
        $parameters = $this->bindPathParameters($request);

        // If the route has a regular expression for the host part of the URI, we will
        // compile that and get the parameter matches for this domain. We will then
        // merge them into this parameters array so that this array is completed.
        if (! is_null($this->route->compiled->getHostRegex())) {
            $parameters = $this->bindHostParameters(
                $request, $parameters
            );
        }

        return $this->replaceDefaults($parameters);
    }

    /**
     * Get the parameter matches for the path portion of the URI.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function bindPathParameters($request)
    {
        $path = '/'.ltrim($request->decodedPath(), '/');

        preg_match($this->route->compiled->getRegex(), $path, $matches);

        return $this->matchToKeys(array_slice($matches, 1));
    }

    /**
     * Extract the parameter list from the host part of the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  array  $parameters
     * @return array
     */
    protected function bindHostParameters($request, $parameters)
    {
        preg_match($this->route->compiled->getHostRegex(), $request->getHost(), $matches);

        return array_merge($this->matchToKeys(array_slice($matches, 1)), $parameters);
    }

    /**
     * Combine a set of parameter matches with the route's keys.
     *
     * @param  array  $matches
     * @return array
     */
    protected function matchToKeys(array $matches)
    {
        if (empty($parameterNames = $this->route->parameterNames())) {
            return [];
        }

        $parameters = array_intersect_key($matches, array_flip($parameterNames));

        return array_filter($parameters, function ($value) {
            return is_string($value) && strlen($value) > 0;
        });
    }

    /**
     * Replace null parameters with their defaults.
     *
     * @param  array  $parameters
     * @return array
     */
    protected function replaceDefaults(array $parameters)
    {
        foreach ($parameters as $key => $value) {
            $parameters[$key] = $value ?? Arr::get($this->route->defaults, $key);
        }

        foreach ($this->route->defaults as $key => $value) {
            if (! isset($parameters[$key])) {
                $parameters[$key] = $value;
            }
        }

        return $parameters;
    }
}