/
marklis
/
transport
Обзор
Документация
Войти
/
marklis
/
transport
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/Console/Commands/RepositoryMakeCommand.php
204 строки
6 KB
Мамонов Алексей Анатольевич
Заявки и пользователи
12 дек 2023, 13:21
12 дек 2023, 13:21
895fcda
Код
Авторство
О чём код?
<?php namespace App\Console\Commands; use Illuminate\Support\Str; use Illuminate\Console\GeneratorCommand; use Illuminate\Contracts\Filesystem\FileNotFoundException; class RepositoryMakeCommand extends GeneratorCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'make:repository {name}'; /** * The console command description. * * @var string */ protected $description = 'Create a new repository class'; /** * The type of class being generated. * * @var string */ protected $type = 'Service'; public function handle(): bool { // First we need to ensure that the given name is not a reserved word within the PHP // language and that the class name will actually be valid. If it is not valid we // can error now and prevent from polluting the filesystem using invalid files. if ($this->isReservedName($this->getNameInput())) { $this->components->error('The name "'.$this->getNameInput().'" is reserved by PHP.'); return false; } $name = $this->qualifyClass($this->getNameInput()); $path = $this->getPath($name); // Next, We will check to see if the class already exists. If it does, we don't want // to create the class and overwrite the user's code. So, we will bail out so the // code is untouched. Otherwise, we will continue generating this class' files. if ((! $this->hasOption('force') || ! $this->option('force')) && $this->alreadyExists($this->getNameInput())) { $this->components->error($this->type.' already exists.'); return false; } // Next, we will generate the path to the location where this class' file should get // written. Then, we will build the class and make the proper replacements on the // stub files so that it gets the correctly formatted namespace and class name. $this->makeDirectory($path); $this->makeInterface(); $this->makeCoreClass(); $this->makeEntityClass(); $this->files->put($path, $this->sortImports($this->buildClass($name))); $info = $this->type; $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); return true; } protected function getStub(): string { return app_path() . '/Console/Commands/Stubs/make-repository.stub'; } protected function getDefaultNamespace($rootNamespace): string { return $rootNamespace . '\Repositories'; } protected function replaceClass($stub, $name): array|string { $model = str_replace('Repository', '', $this->getNameInput()); $stub = parent::replaceClass($stub, $name); $stub = str_replace("CustomRepositoryContract", $this->argument('name') . 'Contract', $stub); $stub = str_replace("CustomModel", $model, $stub); return str_replace("CustomRepository", $this->argument('name'), $stub); } /** * @throws FileNotFoundException */ private function makeCoreClass(): void { if (! $this->alreadyExists('CoreRepository')) { $name = $this->qualifyClass('CoreRepository'); $path = $this->getPath($name); $this->files->put($path, $this->sortImports($this->buildCoreClass($name))); } } /** * @throws FileNotFoundException */ private function makeEntityClass(): void { if (! $this->alreadyExists('EntityRepository')) { $name = $this->qualifyClass('EntityRepository'); $path = $this->getPath($name); $this->files->put($path, $this->sortImports($this->buildEntityClass($name))); } } /** * @throws FileNotFoundException */ private function makeInterface(): void { $name = $this->qualifyClass($this->argument('name') . 'Contract'); $path = $this->getInterfacePath($name); $this->makeDirectory($path); $this->files->put($path, $this->sortImports($this->buildInterface($name))); } /** * Build the core class. * * @param string $name * @return string * * @throws FileNotFoundException */ protected function buildCoreClass(string $name): string { $stub = $this->files->get($this->getCoreStub()); return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); } protected function getCoreStub(): string { return app_path() . '/Console/Commands/Stubs/make-repository-core.stub'; } /** * Build the core class. * * @param string $name * @return string * * @throws FileNotFoundException */ protected function buildEntityClass(string $name): string { $stub = $this->files->get($this->getEntityStub()); return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); } protected function getEntityStub(): string { return app_path() . '/Console/Commands/Stubs/make-repository-entity.stub'; } /** * Build the core class. * * @param string $name * @return string * * @throws FileNotFoundException */ protected function buildInterface(string $name): string { $stub = $this->files->get($this->getInterface()); return $this->replaceNamespace($stub, $name)->replaceInterface($stub, $name); } protected function getInterface(): string { return app_path() . '/Console/Commands/Stubs/make-repository-contract.stub'; } protected function replaceInterface($stub, $name): array|string { $stub = parent::replaceClass($stub, $name); return str_replace("CustomRepositoryContract", $this->argument('name') . 'Contract', $stub); } protected function getInterfacePath($name): string { $name = Str::replaceFirst($this->rootNamespace(), '', 'Contracts/' . $name); return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; } }