master

laravel/framework

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

MiddlewareMakeCommand.php

TLDR

This file (MiddlewareMakeCommand.php) is part of the Illuminate\Routing\Console namespace and contains the MiddlewareMakeCommand class. This class is responsible for generating a new middleware class using a stub file.

Methods

getStub

This method returns the path to the stub file used for generating the middleware class.

resolveStubPath

This method resolves the fully-qualified path to the stub file. It checks if a custom stub file exists in the Laravel project and returns its path, or falls back to the default stub path.

getDefaultNamespace

This method returns the default namespace for the generated middleware class. It appends Http\Middleware to the root namespace.

Classes

MiddlewareMakeCommand

This class extends the GeneratorCommand class and is responsible for generating a new middleware class. It sets the console command name, description, and type. It also uses the CreatesMatchingTest trait. The class provides the implementation for the getStub, resolveStubPath, and getDefaultNamespace methods.

<?php

namespace Illuminate\Routing\Console;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:middleware')]
class MiddlewareMakeCommand extends GeneratorCommand
{
    use CreatesMatchingTest;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:middleware';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new middleware class';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Middleware';

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return $this->resolveStubPath('/stubs/middleware.stub');
    }

    /**
     * Resolve the fully-qualified path to the stub.
     *
     * @param  string  $stub
     * @return string
     */
    protected function resolveStubPath($stub)
    {
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
            ? $customPath
            : __DIR__.$stub;
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Http\Middleware';
    }
}