Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Foundation/Console/ObserverMakeCommand.php
Строк: 173
<?php

namespace IlluminateFoundationConsole;

use 
IlluminateConsoleGeneratorCommand;
use 
InvalidArgumentException;
use 
SymfonyComponentConsoleAttributeAsCommand;
use 
SymfonyComponentConsoleInputInputInterface;
use 
SymfonyComponentConsoleInputInputOption;
use 
SymfonyComponentConsoleOutputOutputInterface;

use function 
LaravelPromptssuggest;

#[AsCommand(name: 'make:observer')]
class ObserverMakeCommand extends GeneratorCommand
{
    
/**
     * The console command name.
     *
     * @var string
     */
    
protected $name 'make:observer';

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

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

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

        
$model $this->option('model');

        return 
$model $this->replaceModel($stub$model) : $stub;
    }

    
/**
     * Replace the model for the given stub.
     *
     * @param  string  $stub
     * @param  string  $model
     * @return string
     */
    
protected function replaceModel($stub$model)
    {
        
$modelClass $this->parseModel($model);

        
$replace = [
            
'DummyFullModelClass' => $modelClass,
            
'{{ namespacedModel }}' => $modelClass,
            
'{{namespacedModel}}' => $modelClass,
            
'DummyModelClass' => class_basename($modelClass),
            
'{{ model }}' => class_basename($modelClass),
            
'{{model}}' => class_basename($modelClass),
            
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
            
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
            
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
        ];

        return 
str_replace(
            
array_keys($replace), array_values($replace), $stub
        
);
    }

    
/**
     * Get the fully-qualified model class name.
     *
     * @param  string  $model
     * @return string
     *
     * @throws InvalidArgumentException
     */
    
protected function parseModel($model)
    {
        if (
preg_match('([^A-Za-z0-9_/\\])'$model)) {
            throw new 
InvalidArgumentException('Model name contains invalid characters.');
        }

        return 
$this->qualifyModel($model);
    }

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

    
/**
     * Get the console command arguments.
     *
     * @return array
     */
    
protected function getOptions()
    {
        return [
            [
'force''f'InputOption::VALUE_NONE'Create the class even if the observer already exists'],
            [
'model''m'InputOption::VALUE_OPTIONAL'The model that the observer applies to'],
        ];
    }

    
/**
     * Interact further with the user if they were prompted for missing arguments.
     *
     * @param  SymfonyComponentConsoleInputInputInterface  $input
     * @param  SymfonyComponentConsoleOutputOutputInterface  $output
     * @return void
     */
    
protected function afterPromptingForMissingArguments(InputInterface $inputOutputInterface $output)
    {
        if (
$this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) {
            return;
        }

        
$model suggest(
            
'What model should this observer apply to? (Optional)',
            
$this->possibleModels(),
        );

        if (
$model) {
            
$input->setOption('model'$model);
        }
    }
}
Онлайн: 0
Реклама