diff --git a/config/crud.php b/config/crud.php index 380ed15..1613ec0 100644 --- a/config/crud.php +++ b/config/crud.php @@ -1,5 +1,7 @@ \App\Http\Controllers\Controller::class, + 'parent_controller' => Controller::class, /* |-------------------------------------------------------------------------- diff --git a/src/Abstracts/GeneratorCommand.php b/src/Abstracts/GeneratorCommand.php index d892310..5672b2e 100644 --- a/src/Abstracts/GeneratorCommand.php +++ b/src/Abstracts/GeneratorCommand.php @@ -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); } @@ -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(); } /** @@ -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"; } /** @@ -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); @@ -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."); } diff --git a/src/Commands/ControllerMakeCommand.php b/src/Commands/ControllerMakeCommand.php index 1d9de16..e5bcf8c 100644 --- a/src/Commands/ControllerMakeCommand.php +++ b/src/Commands/ControllerMakeCommand.php @@ -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' => '', @@ -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()); @@ -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 */ @@ -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) { @@ -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)) . ';'); } @@ -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)) . ';'); } @@ -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; } /** diff --git a/src/Commands/CrudMakeCommand.php b/src/Commands/CrudMakeCommand.php index 5a005de..30981ef 100644 --- a/src/Commands/CrudMakeCommand.php +++ b/src/Commands/CrudMakeCommand.php @@ -67,35 +67,6 @@ public function handle() return self::FAILURE; } - /** - * Get the console command arguments. - */ - protected function getArguments(): array - { - return [ - ['name', InputArgument::REQUIRED, 'The resource name will be created.'], - ['module', InputArgument::OPTIONAL, 'The name of module where will be created. (Experimental.)'], - ]; - } - - /** - * Get the console command options. - */ - protected function getOptions(): array - { - return [ - ['fields', null, InputOption::VALUE_OPTIONAL, 'The specified table fields (Experimental).', null], - ]; - } - - /** - * Get the console command options. - */ - private function getResourceName(): string - { - return Str::studly($this->argument('name')); - } - /** * Create Request classes for resource store, * update and index operation. @@ -104,21 +75,21 @@ private function getResourceName(): string */ private function createRequest(): void { - if (! config('crud.templates.request.generate', true)) { + if (!config('crud.templates.request.generate', true)) { return; } foreach (['Index', 'Store', 'Update'] as $prefix) { - $resourcePath = $this->getResourceName().'Request'; + $resourcePath = $this->getResourceName() . 'Request'; $dir = dirname($resourcePath); - $dir = ($dir == '.') ? '' : $dir.'/'; + $dir = ($dir == '.') ? '' : $dir . '/'; $resource = basename($resourcePath); $options = [ - 'name' => $dir.$prefix.$resource, + 'name' => $dir . $prefix . $resource, 'module' => $this->getModuleName(), '--fields' => $this->option('fields'), ]; @@ -131,6 +102,14 @@ private function createRequest(): void } } + /** + * Get the console command options. + */ + private function getResourceName(): string + { + return Str::studly($this->argument('name')); + } + /** * Create Resource classes for resource list and * show response. @@ -139,16 +118,16 @@ private function createRequest(): void */ private function createResource(): void { - if (! config('crud.templates.resource.generate', true)) { + if (!config('crud.templates.resource.generate', true)) { return; } $this->call('laraflow:make-resource', [ - 'name' => $this->getResourceName().'Resource', + 'name' => $this->getResourceName() . 'Resource', 'module' => $this->getModuleName(), ]); $this->call('laraflow:make-resource', [ - 'name' => $this->getResourceName().'Collection', + 'name' => $this->getResourceName() . 'Collection', 'module' => $this->getModuleName(), '--collection' => true, ]); @@ -162,7 +141,7 @@ private function createResource(): void private function createModel(): void { - if (! config('crud.templates.model.generate', true)) { + if (!config('crud.templates.model.generate', true)) { return; } @@ -181,7 +160,7 @@ private function createModel(): void private function createMigration(): void { - if (! config('crud.templates.migration.generate', true)) { + if (!config('crud.templates.migration.generate', true)) { return; } @@ -200,12 +179,12 @@ private function createMigration(): void */ private function createController(): void { - if (! config('crud.templates.controller.generate', true)) { + if (!config('crud.templates.controller.generate', true)) { return; } $this->call('laraflow:make-controller', [ - 'name' => $this->getResourceName().'Controller', + 'name' => $this->getResourceName() . 'Controller', '--model' => $this->getResourceName(), 'module' => $this->getModuleName(), '--crud' => true, @@ -219,13 +198,13 @@ private function updateRouteFile(): void { $filePath = base_path(config('crud.route_path', 'routes/api.php')); - if (! file_exists($filePath)) { + if (!file_exists($filePath)) { throw new InvalidArgumentException("Route file location doesn't exist"); } $fileContent = file_get_contents($filePath); - if (! str_contains($fileContent, '//DO NOT REMOVE THIS LINE//')) { + if (!str_contains($fileContent, '//DO NOT REMOVE THIS LINE//')) { throw new GeneratorException('Route file missing the CRUD Pointer comment.'); } @@ -236,12 +215,12 @@ private function updateRouteFile(): void $controller = GeneratorPath::convertPathToNamespace( '\\' - .$this->getModuleName() - .'\\' - .GenerateConfigReader::read('controller')->getNamespace() - .'\\' - .$this->getResourceName() - .'Controller::class' + . $this->getModuleName() + . '\\' + . GenerateConfigReader::read('controller')->getNamespace() + . '\\' + . $this->getResourceName() + . 'Controller::class' ); $template = <<type); - return $config->getPath().'/'.$this->getFileName(); + return $config->getPath() . '/' . $this->getFileName(); } protected function getFileName() { - return date('Y_m_d_His_\c\r\e\a\t\e_').Str::snake($this->argument('name')).'_table.php'; + return date('Y_m_d_His_\c\r\e\a\t\e_') . Str::snake($this->argument('name')) . '_table.php'; } /** diff --git a/src/Commands/ModelMakeCommand.php b/src/Commands/ModelMakeCommand.php index f63b0f4..7786dbb 100644 --- a/src/Commands/ModelMakeCommand.php +++ b/src/Commands/ModelMakeCommand.php @@ -42,14 +42,6 @@ class ModelMakeCommand extends GeneratorCommand */ protected $description = 'Create a new model for the specified resource.'; - /** - * @return mixed|string - */ - private function getModelName() - { - return Str::studly($this->argument('name')); - } - /** * Get the console command arguments. */ @@ -91,6 +83,14 @@ protected function getTemplateContents(): string ]))->render(); } + /** + * @return mixed|string + */ + private function getModelName() + { + return Str::studly($this->argument('name')); + } + /** * @experimental * @@ -102,13 +102,13 @@ private function getFillable() $fillable = $this->option('fillable'); - if (! is_null($fillable)) { + if (!is_null($fillable)) { $arrays = array_map(function ($column) { - return (string) $column; + return (string)$column; }, explode(',', trim($fillable))); - $field = 'fillable = '.json_encode($arrays, JSON_PRETTY_PRINT); + $field = 'fillable = ' . json_encode($arrays, JSON_PRETTY_PRINT); } return $field; @@ -116,6 +116,6 @@ private function getFillable() protected function getFileName(): string { - return Str::studly($this->argument('name')).'.php'; + return Str::studly($this->argument('name')) . '.php'; } } diff --git a/src/Commands/RequestMakeCommand.php b/src/Commands/RequestMakeCommand.php index 57766ef..952e1c5 100644 --- a/src/Commands/RequestMakeCommand.php +++ b/src/Commands/RequestMakeCommand.php @@ -98,7 +98,7 @@ protected function getRules(): string $fields = $this->option('fields'); - if (! is_null($fields)) { + if (!is_null($fields)) { foreach (explode(',', trim($fields)) as $field) { $rules[$field] = ['string', 'nullable', 'max:255']; } diff --git a/src/Commands/ResourceMakeCommand.php b/src/Commands/ResourceMakeCommand.php index 171d2fc..ac6dc2a 100644 --- a/src/Commands/ResourceMakeCommand.php +++ b/src/Commands/ResourceMakeCommand.php @@ -92,6 +92,6 @@ protected function isCollection(): bool protected function getFileName(): string { - return Str::studly($this->argument('name')).'.php'; + return Str::studly($this->argument('name')) . '.php'; } } diff --git a/src/Exceptions/CreateOperationException.php b/src/Exceptions/CreateOperationException.php index 576d819..365db34 100644 --- a/src/Exceptions/CreateOperationException.php +++ b/src/Exceptions/CreateOperationException.php @@ -18,8 +18,8 @@ class CreateOperationException extends Exception /** * CreateOperationException constructor. * - * @param string $message - * @param int $code + * @param string $message + * @param int $code */ public function __construct($message = '', $code = 0, ?Throwable $previous = null) { diff --git a/src/Exceptions/FileAlreadyExistException.php b/src/Exceptions/FileAlreadyExistException.php index 4ff08bf..8f657aa 100644 --- a/src/Exceptions/FileAlreadyExistException.php +++ b/src/Exceptions/FileAlreadyExistException.php @@ -7,4 +7,6 @@ /** * Class FileAlreadyExistException */ -class FileAlreadyExistException extends Exception {} +class FileAlreadyExistException extends Exception +{ +} diff --git a/src/Exceptions/GeneratorException.php b/src/Exceptions/GeneratorException.php index 3301874..2a6a3f3 100644 --- a/src/Exceptions/GeneratorException.php +++ b/src/Exceptions/GeneratorException.php @@ -7,4 +7,6 @@ /** * Class GeneratorException */ -class GeneratorException extends Exception {} +class GeneratorException extends Exception +{ +} diff --git a/src/Exceptions/UpdateOperationException.php b/src/Exceptions/UpdateOperationException.php index 57b960e..174018d 100644 --- a/src/Exceptions/UpdateOperationException.php +++ b/src/Exceptions/UpdateOperationException.php @@ -18,8 +18,8 @@ class UpdateOperationException extends Exception /** * UpdateOperationException constructor. * - * @param string $message - * @param int $code + * @param string $message + * @param int $code */ public function __construct($message = '', $code = 0, ?Throwable $previous = null) { diff --git a/src/Generators/FileGenerator.php b/src/Generators/FileGenerator.php index 8fc8ead..8651fca 100644 --- a/src/Generators/FileGenerator.php +++ b/src/Generators/FileGenerator.php @@ -36,7 +36,7 @@ class FileGenerator /** * The constructor. * - * @param null $filesystem + * @param null $filesystem */ public function __construct($path, $contents, $filesystem = null) { @@ -83,7 +83,7 @@ public function withFileOverwrite(bool $overwrite): FileGenerator public function generate() { $path = $this->getPath(); - if (! $this->filesystem->exists($path)) { + if (!$this->filesystem->exists($path)) { return $this->filesystem->put($path, $this->getContents()); } if ($this->overwriteFile === true) { @@ -106,7 +106,7 @@ public function getPath() /** * Set path. * - * @param mixed $path + * @param mixed $path * @return $this */ public function setPath($path) @@ -129,7 +129,7 @@ public function getContents() /** * Set contents. * - * @param mixed $contents + * @param mixed $contents * @return $this */ public function setContents($contents) diff --git a/src/Providers/CrudServiceProvider.php b/src/Providers/CrudServiceProvider.php index eeadd70..4654e39 100644 --- a/src/Providers/CrudServiceProvider.php +++ b/src/Providers/CrudServiceProvider.php @@ -20,7 +20,7 @@ class CrudServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom( - __DIR__.'/../../config/crud.php', 'crud' + __DIR__ . '/../../config/crud.php', 'crud' ); $this->app->register(MacroServiceProvider::class); @@ -32,17 +32,17 @@ public function register(): void public function boot(): void { $this->publishes([ - __DIR__.'/../../config/crud.php' => config_path('crud.php'), + __DIR__ . '/../../config/crud.php' => config_path('crud.php'), ], 'crud-config'); - $this->loadTranslationsFrom(__DIR__.'/../../lang', 'crud'); + $this->loadTranslationsFrom(__DIR__ . '/../../lang', 'crud'); $this->publishes([ - __DIR__.'/../../lang' => $this->app->langPath('vendor/crud'), + __DIR__ . '/../../lang' => $this->app->langPath('vendor/crud'), ], 'crud-lang'); $this->publishes([ - __DIR__.'/../../stubs' => base_path('stubs/crud'), + __DIR__ . '/../../stubs' => base_path('stubs/crud'), ], 'crud-stubs'); $this->loadCommands(); diff --git a/src/Providers/MacroServiceProvider.php b/src/Providers/MacroServiceProvider.php index 5a408fa..c24f9b2 100644 --- a/src/Providers/MacroServiceProvider.php +++ b/src/Providers/MacroServiceProvider.php @@ -19,7 +19,7 @@ public function boot(): void * resource * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('deleted', function ($data, array $headers = []) { @@ -31,7 +31,7 @@ public function boot(): void * resource restored * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('restored', function ($data, array $headers = []) { @@ -43,7 +43,7 @@ public function boot(): void * created on server * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('created', function ($data, array $headers = []) { @@ -55,7 +55,7 @@ public function boot(): void * request accepted * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('updated', function ($data, array $headers = []) { @@ -67,7 +67,7 @@ public function boot(): void * request accepted * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('exported', function ($data, array $headers = []) { @@ -79,7 +79,7 @@ public function boot(): void * logic exception * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('failed', function ($data, array $headers = []) { @@ -90,7 +90,7 @@ public function boot(): void * return response with http 200 for all success status * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('success', function ($data, array $headers = []) { @@ -102,7 +102,7 @@ public function boot(): void * token or ip banned * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('banned', function ($data, array $headers = []) { @@ -114,7 +114,7 @@ public function boot(): void * to that request * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('forbidden', function ($data, array $headers = []) { @@ -125,7 +125,7 @@ public function boot(): void * return response with http 404 not found * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('notfound', function ($data, array $headers = []) { @@ -136,7 +136,7 @@ public function boot(): void * return response with http 423 attempt locked * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('locked', function ($data, array $headers = []) { @@ -147,7 +147,7 @@ public function boot(): void * return response with http 429 too many requests code * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('overflow', function ($data, array $headers = []) { diff --git a/src/Support/Config/GeneratorPath.php b/src/Support/Config/GeneratorPath.php index 3bde124..8854727 100644 --- a/src/Support/Config/GeneratorPath.php +++ b/src/Support/Config/GeneratorPath.php @@ -20,7 +20,7 @@ public function __construct($config) return; } $this->path = $config; - $this->generate = (bool) $config; + $this->generate = (bool)$config; $this->namespace = $config; } diff --git a/src/Support/Migrations/NameParser.php b/src/Support/Migrations/NameParser.php index 8ceea2e..be079f6 100644 --- a/src/Support/Migrations/NameParser.php +++ b/src/Support/Migrations/NameParser.php @@ -47,7 +47,7 @@ class NameParser /** * The constructor. * - * @param string $name + * @param string $name */ public function __construct($name) { @@ -113,19 +113,13 @@ public function getPattern() case 'insert': return "/{$action}_(.*)_to_(.*)_table/"; - break; - case 'delete': case 'remove': case 'alter': return "/{$action}_(.*)_from_(.*)_table/"; - break; - default: return "/{$action}_(.*)_table/"; - - break; } } diff --git a/src/Support/Migrations/SchemaParser.php b/src/Support/Migrations/SchemaParser.php index 0257647..ed22f73 100644 --- a/src/Support/Migrations/SchemaParser.php +++ b/src/Support/Migrations/SchemaParser.php @@ -37,7 +37,7 @@ class SchemaParser implements Arrayable /** * Create new instance. * - * @param string|null $schema + * @param string|null $schema */ public function __construct($schema = null) { @@ -83,7 +83,7 @@ public function toArray() /** * Parse a string to array of formatted schema. * - * @param string $schema + * @param string $schema * @return array */ public function parse($schema) @@ -121,7 +121,7 @@ public function getSchemas() /** * Get column name from schema. * - * @param string $schema + * @param string $schema * @return string */ public function getColumn($schema) @@ -129,53 +129,17 @@ public function getColumn($schema) return Arr::get(explode(':', $schema), 0); } - /** - * Get column attributes. - * - * @param string $column - * @param string $schema - * @return array - */ - public function getAttributes($column, $schema) - { - $fields = str_replace($column.':', '', $schema); - - return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields); - } - - /** - * Determine whether the given column is exist in customAttributes array. - * - * @param string $column - * @return bool - */ - public function hasCustomAttribute($column) - { - return array_key_exists($column, $this->customAttributes); - } - - /** - * Get custom attributes value. - * - * @param string $column - * @return array - */ - public function getCustomAttribute($column) - { - return (array) $this->customAttributes[$column]; - } - /** * Create field. * - * @param string $column - * @param array $attributes - * @param string $type + * @param string $column + * @param array $attributes + * @param string $type * @return string */ public function createField($column, $attributes, $type = 'add') { - $results = "\t\t\t".'$table'; + $results = "\t\t\t" . '$table'; foreach ($attributes as $key => $field) { $results .= (in_array($column, $this->relationshipKeys)) @@ -183,23 +147,23 @@ public function createField($column, $attributes, $type = 'add') : $this->{"{$type}Column"}($key, $field, $column); } - return $results.';'.PHP_EOL; + return $results . ';' . PHP_EOL; } /** * Add relation column. * - * @param int $key - * @param string $field - * @param string $column + * @param int $key + * @param string $field + * @param string $column * @return string */ protected function addRelationColumn($key, $field, $column) { if ($key === 0) { - $relatedColumn = Str::snake(class_basename($field)).'_id'; + $relatedColumn = Str::snake(class_basename($field)) . '_id'; - return "->integer('{$relatedColumn}')->unsigned();".PHP_EOL."\t\t\t"."\$table->foreign('{$relatedColumn}')"; + return "->integer('{$relatedColumn}')->unsigned();" . PHP_EOL . "\t\t\t" . "\$table->foreign('{$relatedColumn}')"; } if ($key === 1) { return "->references('{$field}')"; @@ -208,10 +172,46 @@ protected function addRelationColumn($key, $field, $column) return "->on('{$field}')"; } if (Str::contains($field, '(')) { - return '->'.$field; + return '->' . $field; } - return '->'.$field.'()'; + return '->' . $field . '()'; + } + + /** + * Get column attributes. + * + * @param string $column + * @param string $schema + * @return array + */ + public function getAttributes($column, $schema) + { + $fields = str_replace($column . ':', '', $schema); + + return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields); + } + + /** + * Determine whether the given column is exist in customAttributes array. + * + * @param string $column + * @return bool + */ + public function hasCustomAttribute($column) + { + return array_key_exists($column, $this->customAttributes); + } + + /** + * Get custom attributes value. + * + * @param string $column + * @return array + */ + public function getCustomAttribute($column) + { + return (array)$this->customAttributes[$column]; } /** @@ -234,25 +234,25 @@ public function down() /** * Format field to script. * - * @param int $key - * @param string $field - * @param string $column + * @param int $key + * @param string $field + * @param string $column * @return string */ protected function addColumn($key, $field, $column) { if ($this->hasCustomAttribute($column)) { - return '->'.$field; + return '->' . $field; } if ($key == 0) { - return '->'.$field."('".$column."')"; + return '->' . $field . "('" . $column . "')"; } if (Str::contains($field, '(')) { - return '->'.$field; + return '->' . $field; } - return '->'.$field.'()'; + return '->' . $field . '()'; } } diff --git a/src/Support/Stub.php b/src/Support/Stub.php index 266ddbe..d5d945f 100644 --- a/src/Support/Stub.php +++ b/src/Support/Stub.php @@ -28,7 +28,7 @@ class Stub /** * The constructor. * - * @param string $path + * @param string $path */ public function __construct($path, array $replaces = []) { @@ -42,7 +42,7 @@ public function __construct($path, array $replaces = []) /** * Create new self instance. * - * @param string $path + * @param string $path * @return self */ public static function create($path, array $replaces = []) @@ -53,13 +53,13 @@ public static function create($path, array $replaces = []) /** * Save stub to specific path. * - * @param string $path - * @param string $filename + * @param string $path + * @param string $filename * @return bool */ public function saveTo($path, $filename) { - return file_put_contents($path.'/'.$filename, $this->getContents()); + return file_put_contents($path . '/' . $filename, $this->getContents()); } /** @@ -72,7 +72,7 @@ public function getContents() $contents = file_get_contents($this->getPath()); foreach ($this->replaces as $search => $replace) { - $contents = str_replace('$'.strtoupper($search).'$', $replace, $contents); + $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents); } return $contents; @@ -85,15 +85,15 @@ public function getContents() */ public function getPath() { - $path = static::getBasePath().$this->path; + $path = static::getBasePath() . $this->path; - return file_exists($path) ? $path : __DIR__.'/../../stubs'.$this->path; + return file_exists($path) ? $path : __DIR__ . '/../../stubs' . $this->path; } /** * Set stub path. * - * @param string $path + * @param string $path * @return self */ public function setPath($path) @@ -116,7 +116,7 @@ public static function getBasePath() /** * Set base path. * - * @param string $path + * @param string $path */ public static function setBasePath($path) { diff --git a/src/Traits/ModelExceptionTrait.php b/src/Traits/ModelExceptionTrait.php index 33ea162..eca04db 100644 --- a/src/Traits/ModelExceptionTrait.php +++ b/src/Traits/ModelExceptionTrait.php @@ -29,8 +29,8 @@ public function getModel() /** * Set the affected Eloquent model and instance ids. * - * @param class-string $model - * @param null $id + * @param class-string $model + * @param null $id * @return $this */ public function setModel($model, $id = null) diff --git a/src/Traits/ModuleCommandTrait.php b/src/Traits/ModuleCommandTrait.php index f3830a5..8005d4d 100644 --- a/src/Traits/ModuleCommandTrait.php +++ b/src/Traits/ModuleCommandTrait.php @@ -19,11 +19,11 @@ public function getModuleName(): string $module = config('crud.namespace', 'App'); - if (! $module) { + if (!$module) { throw new GeneratorException('Invalid Root namespace on config.'); } - if (! is_dir($fallbackPath)) { + if (!is_dir($fallbackPath)) { throw new GeneratorException('Invalid Root Path on config.'); } diff --git a/src/helpers.php b/src/helpers.php index dcfa88b..5ed8fc2 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,6 +1,6 @@