master

laravel/framework

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

NotificationMakeCommand.php

TLDR

This file is the NotificationMakeCommand class in the Illuminate\Foundation\Console namespace. It extends the GeneratorCommand class and is responsible for creating a new notification class. It also provides an option to create a new Markdown template for the notification.

Methods

handle

This method is responsible for executing the console command. It calls the handle method of the parent class and checks if the --force option is specified. If the parent method returns false and the --force option is not present, the method returns. If the --markdown option is specified, the method calls the writeMarkdownTemplate method.

writeMarkdownTemplate

This method is responsible for writing the Markdown template for the notification. It determines the path of the template based on the --markdown option value. If the directory for the template does not exist, it creates the directory. Then it writes the contents of the Markdown template stub to the file.

buildClass

This method overrides the buildClass method of the parent class. It replaces the DummyView placeholder in the generated class with the value of the --markdown option.

getStub

This method returns the stub file path for the generator. If the --markdown option is specified, it returns the path to the Markdown notification stub file. Otherwise, it returns the path to the regular notification stub file.

resolveStubPath

This method resolves the fully-qualified path to the stub file. It first checks if a custom path exists based on the provided stub path. If not, it returns the default stub path.

getDefaultNamespace

This method returns the default namespace for the generated class. It appends Notifications to the root namespace.

getOptions

This method returns an array of console command options. It includes options for creating the class even if it already exists (--force) and creating a new Markdown template for the notification (--markdown).

<?php

namespace Illuminate\Foundation\Console;

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

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

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

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

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        if (parent::handle() === false && ! $this->option('force')) {
            return;
        }

        if ($this->option('markdown')) {
            $this->writeMarkdownTemplate();
        }
    }

    /**
     * Write the Markdown template for the mailable.
     *
     * @return void
     */
    protected function writeMarkdownTemplate()
    {
        $path = $this->viewPath(
            str_replace('.', '/', $this->option('markdown')).'.blade.php'
        );

        if (! $this->files->isDirectory(dirname($path))) {
            $this->files->makeDirectory(dirname($path), 0755, true);
        }

        $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub'));
    }

    /**
     * Build the class with the given name.
     *
     * @param  string  $name
     * @return string
     */
    protected function buildClass($name)
    {
        $class = parent::buildClass($name);

        if ($this->option('markdown')) {
            $class = str_replace(['DummyView', '{{ view }}'], $this->option('markdown'), $class);
        }

        return $class;
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return $this->option('markdown')
            ? $this->resolveStubPath('/stubs/markdown-notification.stub')
            : $this->resolveStubPath('/stubs/notification.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.'\Notifications';
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the notification already exists'],
            ['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the notification'],
        ];
    }
}