Skip to content

Commit

Permalink
Code Import Optimization and Refactoring Style
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Jul 6, 2024
1 parent f6a027f commit 7b4371a
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 220 deletions.
4 changes: 3 additions & 1 deletion config/crud.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use App\Http\Controllers\Controller;

return [

/*
Expand All @@ -17,7 +19,7 @@
| This setting will be used to set which controller class that all api
| controller will inherit.
*/
'parent_controller' => \App\Http\Controllers\Controller::class,
'parent_controller' => Controller::class,

/*
|--------------------------------------------------------------------------
Expand Down
24 changes: 12 additions & 12 deletions src/Abstracts/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function handle(): int
* Disable Script on Testing environment
* also Disable through configuration
*/
if (app()->environment('testing') || ! config('crud.enabled', false)) {
if (app()->environment('testing') || !config('crud.enabled', false)) {
return self::SUCCESS;
}

$path = str_replace('\\', '/', $this->getDestinationFilePath());

if (! $this->laravel['files']->isDirectory($dir = dirname($path))) {
if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
$this->laravel['files']->makeDirectory($dir, 0777, true);
}

Expand Down Expand Up @@ -70,9 +70,9 @@ protected function getDestinationFilePath(): string
{
$config = GenerateConfigReader::read($this->type);

return config('crud.root_path', 'app').'/'
.$config->getPath().'/'
.$this->getFileName();
return config('crud.root_path', 'app') . '/'
. $config->getPath() . '/'
. $this->getFileName();
}

/**
Expand All @@ -82,7 +82,7 @@ protected function getFileName()
{
$type = Str::studly($this->type);

return str_replace($type, '', Str::studly($this->argument('name')))."{$type}.php";
return str_replace($type, '', Str::studly($this->argument('name'))) . "{$type}.php";
}

/**
Expand All @@ -104,9 +104,9 @@ public function getClassNamespace(?string $module = null): string

$namespace = config('crud.namespace');

$namespace .= '\\'.$this->getDefaultNamespace();
$namespace .= '\\' . $this->getDefaultNamespace();

$namespace .= '\\'.$extra;
$namespace .= '\\' . $extra;

$namespace = str_replace('/', '\\', $namespace);

Expand All @@ -124,23 +124,23 @@ public function getClass(): string
/**
* Get default namespace.
*
* @param null $type
* @param null $type
*
* @throws GeneratorException
*/
public function getDefaultNamespace($type = null): string
{
if (! $type) {
if (!$type) {
if (property_exists($this, 'type')) {
$type = $this->type;
}
}

if (! $type) {
if (!$type) {
throw new GeneratorException('Stub type argument or property is not configured.');
}

if (! config("crud.templates.{$type}")) {
if (!config("crud.templates.{$type}")) {
throw new InvalidArgumentException("Generator is missing [{$type}] config, check generators.php file.");
}

Expand Down
60 changes: 30 additions & 30 deletions src/Commands/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function getTemplateContents(): string
'MESSAGE_VARIABLE' => Str::title(Str::replace('-', ' ', Str::kebab($this->getResourceVariableName()))),
'RESOURCE_NAMESPACES' => '',
'REQUEST_NAMESPACES' => '',
'MODEL' => str_replace('/', '\\', $this->getDefaultNamespace('model').'\\'.$this->option('model')),
'MODEL' => str_replace('/', '\\', $this->getDefaultNamespace('model') . '\\' . $this->option('model')),
'STORE_REQUEST' => '',
'UPDATE_REQUEST' => '',
'INDEX_REQUEST' => '',
Expand All @@ -74,24 +74,6 @@ protected function getTemplateContents(): string
return (new Stub('/controller-crud.stub', $replacements))->render();
}

/**
* @throws GeneratorException
*/
private function getParentController(): string
{
$controllerName = config('crud.parent_controller');

if (! $controllerName || ! class_exists($controllerName)) {
throw new GeneratorException("Parent Controller class {$controllerName} does not exist.");
}

if (class_basename($controllerName) != 'Controller') {
$controllerName .= 'as Controller';
}

return "use {$controllerName};";
}

public function getClass(): string
{
return class_basename($this->getControllerName());
Expand All @@ -111,6 +93,24 @@ protected function getControllerName()
return $controller;
}

/**
* @throws GeneratorException
*/
private function getParentController(): string
{
$controllerName = config('crud.parent_controller');

if (!$controllerName || !class_exists($controllerName)) {
throw new GeneratorException("Parent Controller class {$controllerName} does not exist.");
}

if (class_basename($controllerName) != 'Controller') {
$controllerName .= 'as Controller';
}

return "use {$controllerName};";
}

/**
* @return string
*/
Expand All @@ -132,9 +132,9 @@ private function setRequestNamespaces(array &$replacements)
$namespaces = [];

foreach (['Store', 'Update', 'Index'] as $prefix) {
$path = $this->getModuleName().'/'
.$this->getDefaultNamespace('request').'/'
.dirname($this->option('model')).'/'.$prefix.class_basename($this->option('model')).'Request';
$path = $this->getModuleName() . '/'
. $this->getDefaultNamespace('request') . '/'
. dirname($this->option('model')) . '/' . $prefix . class_basename($this->option('model')) . 'Request';

$path = str_replace('/./', '/', $path);
match ($prefix) {
Expand All @@ -143,7 +143,7 @@ private function setRequestNamespaces(array &$replacements)
'Update' => $replacements['UPDATE_REQUEST'] = basename($path),
};

$namespaces[] = ('use '.implode('\\', explode('/', $path)).';');
$namespaces[] = ('use ' . implode('\\', explode('/', $path)) . ';');

}

Expand All @@ -158,11 +158,11 @@ private function setResourceNamespaces(array &$replacements)
$namespaces = [];

foreach (['Resource', 'Collection'] as $suffix) {
$path = $this->getModuleName().'/'
.$this->getDefaultNamespace('resource').'/'
.$this->option('model').$suffix;
$path = $this->getModuleName() . '/'
. $this->getDefaultNamespace('resource') . '/'
. $this->option('model') . $suffix;

$namespaces[] = ('use '.implode('\\', explode('/', $path)).';');
$namespaces[] = ('use ' . implode('\\', explode('/', $path)) . ';');

}

Expand All @@ -171,15 +171,15 @@ private function setResourceNamespaces(array &$replacements)

protected function getClassPath(string $prefix = '', string $suffix = 'Request')
{
$resourcePath = $this->argument('name').$suffix;
$resourcePath = $this->argument('name') . $suffix;

$dir = dirname($resourcePath);

$dir = ($dir == '.') ? '' : $dir.'/';
$dir = ($dir == '.') ? '' : $dir . '/';

$resource = basename($resourcePath);

return $dir.$prefix.$resource;
return $dir . $prefix . $resource;
}

/**
Expand Down
Loading

0 comments on commit 7b4371a

Please sign in to comment.