diff --git a/src/Abstracts/GeneratorCommand.php b/src/Abstracts/GeneratorCommand.php index 35b651b..c5dae20 100644 --- a/src/Abstracts/GeneratorCommand.php +++ b/src/Abstracts/GeneratorCommand.php @@ -27,7 +27,7 @@ public function handle(): int $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); } @@ -60,9 +60,19 @@ protected function getDestinationFilePath(): string { $config = GenerateConfigReader::read($this->type); - return config('api-crud.root_path', 'app').'/' - .$config->getPath().'/' - .$this->getFileName(); + return config('api-crud.root_path', 'app') . '/' + . $config->getPath() . '/' + . $this->getFileName(); + } + + /** + * @return string + */ + protected function getFileName() + { + $type = Str::studly($this->type); + + return str_replace($type, '', Str::studly($this->argument('name'))) . "{$type}.php"; } /** @@ -84,9 +94,9 @@ public function getClassNamespace(?string $module = null): string $namespace = config('api-crud.namespace'); - $namespace .= '\\'.$this->getDefaultNamespace(); + $namespace .= '\\' . $this->getDefaultNamespace(); - $namespace .= '\\'.$extra; + $namespace .= '\\' . $extra; $namespace = str_replace('/', '\\', $namespace); @@ -104,23 +114,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("api-crud.templates.{$type}")) { + if (!config("api-crud.templates.{$type}")) { throw new InvalidArgumentException("Generator is missing [{$type}] config, check generators.php file."); } @@ -129,14 +139,4 @@ public function getDefaultNamespace($type = null): string return $config['namespace'] ?? $config['path']; } - - /** - * @return string - */ - protected function getFileName() - { - $type = Str::studly($this->type); - - return str_replace($type, '', Str::studly($this->argument('name')))."{$type}.php"; - } } diff --git a/src/Commands/ControllerMakeCommand.php b/src/Commands/ControllerMakeCommand.php index 170c2af..d8ca2a2 100644 --- a/src/Commands/ControllerMakeCommand.php +++ b/src/Commands/ControllerMakeCommand.php @@ -42,20 +42,6 @@ class ControllerMakeCommand extends GeneratorCommand */ protected $description = 'Generate new restful controller for the specified package.'; - /** - * @return array|string - */ - protected function getControllerName() - { - $controller = Str::studly($this->argument('name')); - - if (Str::contains(strtolower($controller), 'controller') === false) { - $controller .= 'Controller'; - } - - return $controller; - } - /** * @throws GeneratorException */ @@ -74,7 +60,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' => '', @@ -95,6 +81,20 @@ public function getClass(): string return class_basename($this->getControllerName()); } + /** + * @return array|string + */ + protected function getControllerName() + { + $controller = Str::studly($this->argument('name')); + + if (Str::contains(strtolower($controller), 'controller') === false) { + $controller .= 'Controller'; + } + + return $controller; + } + /** * @return string */ @@ -111,35 +111,23 @@ protected function getResourceVariableName() return Str::camel(basename($this->getResourceName())); } - protected function getClassPath(string $prefix = '', string $suffix = 'Request') - { - $resourcePath = $this->argument('name').$suffix; - - $dir = dirname($resourcePath); - - $dir = ($dir == '.') ? '' : $dir.'/'; - - $resource = basename($resourcePath); - - return $dir.$prefix.$resource; - } - 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) { 'Store' => $replacements['STORE_REQUEST'] = basename($path), 'Index' => $replacements['INDEX_REQUEST'] = basename($path), 'Update' => $replacements['UPDATE_REQUEST'] = basename($path), }; - $namespaces[] = ('use '.implode('\\', explode('/', $path)).';'); + $namespaces[] = ('use ' . implode('\\', explode('/', $path)) . ';'); } @@ -154,17 +142,30 @@ 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)) . ';'); } $replacements['RESOURCE_NAMESPACES'] = implode("\n", $namespaces); } + protected function getClassPath(string $prefix = '', string $suffix = 'Request') + { + $resourcePath = $this->argument('name') . $suffix; + + $dir = dirname($resourcePath); + + $dir = ($dir == '.') ? '' : $dir . '/'; + + $resource = basename($resourcePath); + + return $dir . $prefix . $resource; + } + /** * Get the console command arguments. * diff --git a/src/Commands/CrudMakeCommand.php b/src/Commands/CrudMakeCommand.php index 07ffa75..b50f353 100644 --- a/src/Commands/CrudMakeCommand.php +++ b/src/Commands/CrudMakeCommand.php @@ -68,21 +68,21 @@ public function handle() private function createRequest() { - if (! config('api-crud.templates.request.generate', true)) { + if (!config('api-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(), ]; @@ -105,42 +105,28 @@ private function getResourceName() private function createResource() { - if (! config('api-crud.templates.resource.generate', true)) { + if (!config('api-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, ]); } - private function createController() - { - if (! config('api-crud.templates.controller.generate', true)) { - return; - } - - $this->call('laraflow:make-controller', [ - 'name' => $this->getResourceName().'Controller', - '--model' => $this->getResourceName(), - 'module' => $this->getModuleName(), - '--crud' => true, - ]); - } - /** * @throws GeneratorException */ private function createModel() { - if (! config('api-crud.templates.model.generate', true)) { + if (!config('api-crud.templates.model.generate', true)) { return; } $this->call('laraflow:make-model', [ @@ -150,6 +136,20 @@ private function createModel() ]); } + private function createController() + { + if (!config('api-crud.templates.controller.generate', true)) { + return; + } + + $this->call('laraflow:make-controller', [ + 'name' => $this->getResourceName() . 'Controller', + '--model' => $this->getResourceName(), + 'module' => $this->getModuleName(), + '--crud' => true, + ]); + } + /** * @throws GeneratorException */ @@ -157,13 +157,13 @@ private function updateRouteFile() { $filePath = base_path(config('api-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.'); } @@ -174,12 +174,12 @@ private function updateRouteFile() $controller = GeneratorPath::convertPathToNamespace( '\\' - .$this->getModuleName() - .'\\' - .GenerateConfigReader::read('controller')->getNamespace() - .'\\' - .$this->getResourceName() - .'Controller::class' + . $this->getModuleName() + . '\\' + . GenerateConfigReader::read('controller')->getNamespace() + . '\\' + . $this->getResourceName() + . 'Controller::class' ); $template = <<components->twoColumnDetail($exception->getMessage(), 'ERROR'); @@ -54,11 +55,11 @@ private function configureRouteFile(): void { $routeFilePath = base_path(config('api-crud.route_path', 'routes/api.php')); - if (! is_file($routeFilePath)) { + if (!is_file($routeFilePath)) { throw new InvalidArgumentException("Invalid API route file path: ({$routeFilePath})."); } - if (! is_readable($routeFilePath)) { + if (!is_readable($routeFilePath)) { throw new AccessDeniedException('Unable to read from route file.'); } @@ -74,7 +75,7 @@ private function configureRouteFile(): void $content .= "\n//DO NOT REMOVE THIS LINE//\n"; - if (! is_writable($routeFilePath)) { + if (!is_writable($routeFilePath)) { throw new CannotWriteFileException('Unable to write on route file.'); } @@ -97,6 +98,21 @@ private function confirmConfigPublish(): void } } + private function vendorPublish(string $tag, bool $forced = false): void + { + if ($forced) { + + if ($this->confirm('Already Published. Overwrite?', true)) { + + $this->call('vendor:publish', ['--tag' => $tag, '--force' => true]); + } + + return; + } + + $this->call('vendor:publish', ['--tag' => $tag]); + } + private function confirmLanguagePublish(): void { if ($this->confirm('Publish Language Files', false)) { @@ -120,19 +136,4 @@ private function confirmStubsPublish(): void 'SKIPPED'); } } - - private function vendorPublish(string $tag, bool $forced = false): void - { - if ($forced) { - - if ($this->confirm('Already Published. Overwrite?', true)) { - - $this->call('vendor:publish', ['--tag' => $tag, '--force' => true]); - } - - return; - } - - $this->call('vendor:publish', ['--tag' => $tag]); - } } diff --git a/src/Commands/MigrationMakeCommand.php b/src/Commands/MigrationMakeCommand.php index b8a6987..54a6b7f 100644 --- a/src/Commands/MigrationMakeCommand.php +++ b/src/Commands/MigrationMakeCommand.php @@ -126,23 +126,23 @@ public function getSchemaParser() return new SchemaParser($this->option('fields')); } - /** - * @return array|string - */ - private function getSchemaName() - { - return $this->argument('name'); - } - protected function getDestinationFilePath(): string { $config = GenerateConfigReader::read($this->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'; + } + + /** + * @return array|string + */ + private function getSchemaName() + { + return $this->argument('name'); } } diff --git a/src/Commands/ModelMakeCommand.php b/src/Commands/ModelMakeCommand.php index 747fd1c..2953740 100644 --- a/src/Commands/ModelMakeCommand.php +++ b/src/Commands/ModelMakeCommand.php @@ -65,6 +65,11 @@ private function handleOptionalMigrationOption() } } + private function GetTableName() + { + return Str::replace('/', '', Str::lower(Str::snake(Str::plural($this->getModelName())))); + } + /** * @return mixed|string */ @@ -129,7 +134,7 @@ protected function getTemplateContents(): string return (new Stub('/model.stub', [ 'NAME' => $this->getModelName(), 'ROUTE_NAME' => Str::plural(Str::lower(Str::kebab($this->getModelName()))), - 'JSON_NAME' => Str::lower(Str::snake($this->getModelName())).'_data', + 'JSON_NAME' => Str::lower(Str::snake($this->getModelName())) . '_data', 'TABLE' => $this->getTableName(), 'FILLABLE' => $this->getFillable(), 'NAMESPACE' => $this->getClassNamespace($this->getModuleName()), @@ -142,11 +147,6 @@ protected function getTemplateContents(): string ]))->render(); } - private function GetTableName() - { - return Str::replace('/', '', Str::lower(Str::snake(Str::plural($this->getModelName())))); - } - /** * @return string */ @@ -154,7 +154,7 @@ private function getFillable() { $fillable = $this->option('fillable'); - if (! is_null($fillable)) { + if (!is_null($fillable)) { $arrays = explode(',', $fillable); return json_encode($arrays); @@ -168,6 +168,6 @@ private function getFillable() */ protected function getFileName() { - return Str::studly($this->argument('name')).'.php'; + return Str::studly($this->argument('name')) . '.php'; } } diff --git a/src/Commands/ResourceMakeCommand.php b/src/Commands/ResourceMakeCommand.php index 1e4ce94..8aeb4c8 100644 --- a/src/Commands/ResourceMakeCommand.php +++ b/src/Commands/ResourceMakeCommand.php @@ -97,6 +97,6 @@ protected function isCollection(): bool */ protected function getFileName() { - return Str::studly($this->argument('name')).'.php'; + return Str::studly($this->argument('name')) . '.php'; } } diff --git a/src/Exceptions/RestoreOperationException.php b/src/Exceptions/RestoreOperationException.php index ee1f5d6..feaae7c 100644 --- a/src/Exceptions/RestoreOperationException.php +++ b/src/Exceptions/RestoreOperationException.php @@ -18,8 +18,8 @@ class RestoreOperationException extends Exception /** * RestoreOperationException 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/StoreOperationException.php b/src/Exceptions/StoreOperationException.php index ea90f99..347f74e 100644 --- a/src/Exceptions/StoreOperationException.php +++ b/src/Exceptions/StoreOperationException.php @@ -18,8 +18,8 @@ class StoreOperationException extends Exception /** * StoreOperationException 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/UpdateOperationException.php b/src/Exceptions/UpdateOperationException.php index f7438e5..57c5138 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 26dd5cb..30e65df 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) { @@ -81,7 +81,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) { @@ -104,7 +104,7 @@ public function getPath() /** * Set path. * - * @param mixed $path + * @param mixed $path * @return $this */ public function setPath($path) @@ -127,7 +127,7 @@ public function getContents() /** * Set contents. * - * @param mixed $contents + * @param mixed $contents * @return $this */ public function setContents($contents) diff --git a/src/Providers/ApiCrudServiceProvider.php b/src/Providers/ApiCrudServiceProvider.php index c767fb2..e8bb348 100644 --- a/src/Providers/ApiCrudServiceProvider.php +++ b/src/Providers/ApiCrudServiceProvider.php @@ -4,6 +4,13 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; +use Laraflow\ApiCrud\Commands\ControllerMakeCommand; +use Laraflow\ApiCrud\Commands\CrudMakeCommand; +use Laraflow\ApiCrud\Commands\InstallCommand; +use Laraflow\ApiCrud\Commands\MigrationMakeCommand; +use Laraflow\ApiCrud\Commands\ModelMakeCommand; +use Laraflow\ApiCrud\Commands\RequestMakeCommand; +use Laraflow\ApiCrud\Commands\ResourceMakeCommand; class ApiCrudServiceProvider extends ServiceProvider { @@ -13,7 +20,7 @@ class ApiCrudServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom( - __DIR__.'/../../config/api-crud.php', 'api-crud' + __DIR__ . '/../../config/api-crud.php', 'api-crud' ); $this->app->register(MacroServiceProvider::class); @@ -25,17 +32,17 @@ public function register(): void public function boot(): void { $this->publishes([ - __DIR__.'/../../config/api-crud.php' => config_path('api-crud.php'), + __DIR__ . '/../../config/api-crud.php' => config_path('api-crud.php'), ], 'api-crud-config'); - $this->loadTranslationsFrom(__DIR__.'/../../lang', 'api-crud'); + $this->loadTranslationsFrom(__DIR__ . '/../../lang', 'api-crud'); $this->publishes([ - __DIR__.'/../../lang' => $this->app->langPath('vendor/api-crud'), + __DIR__ . '/../../lang' => $this->app->langPath('vendor/api-crud'), ], 'api-crud-lang'); $this->publishes([ - __DIR__.'/../../stubs' => base_path('stubs/api-crud'), + __DIR__ . '/../../stubs' => base_path('stubs/api-crud'), ], 'api-crud-stubs'); $this->loadCommands(); @@ -52,13 +59,13 @@ private function loadCommands(): void * Using full namespace and runtime call. */ $this->commands([ - \Laraflow\ApiCrud\Commands\InstallCommand::class, - \Laraflow\ApiCrud\Commands\ControllerMakeCommand::class, - \Laraflow\ApiCrud\Commands\MigrationMakeCommand::class, - \Laraflow\ApiCrud\Commands\ModelMakeCommand::class, - \Laraflow\ApiCrud\Commands\RequestMakeCommand::class, - \Laraflow\ApiCrud\Commands\ResourceMakeCommand::class, - \Laraflow\ApiCrud\Commands\CrudMakeCommand::class, + InstallCommand::class, + ControllerMakeCommand::class, + MigrationMakeCommand::class, + ModelMakeCommand::class, + RequestMakeCommand::class, + ResourceMakeCommand::class, + CrudMakeCommand::class, ]); } } diff --git a/src/Providers/MacroServiceProvider.php b/src/Providers/MacroServiceProvider.php index d227911..411e728 100644 --- a/src/Providers/MacroServiceProvider.php +++ b/src/Providers/MacroServiceProvider.php @@ -15,7 +15,7 @@ class MacroServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom( - __DIR__.'/../../config/api-crud.php', 'api-crud' + __DIR__ . '/../../config/api-crud.php', 'api-crud' ); } @@ -29,7 +29,7 @@ public function boot(): void * resource * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('deleted', function ($data, array $headers = []) { @@ -41,7 +41,7 @@ public function boot(): void * resource restored * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('restored', function ($data, array $headers = []) { @@ -53,7 +53,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 = []) { @@ -65,7 +65,7 @@ public function boot(): void * request accepted * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('updated', function ($data, array $headers = []) { @@ -77,7 +77,7 @@ public function boot(): void * request accepted * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('exported', function ($data, array $headers = []) { @@ -89,7 +89,7 @@ public function boot(): void * logic exception * * @param $data - * @param array $headers + * @param array $headers * @return JsonResponse */ ResponseFacade::macro('failed', function ($data, array $headers = []) { @@ -100,7 +100,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 = []) { @@ -112,7 +112,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 = []) { @@ -124,7 +124,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 = []) { @@ -135,7 +135,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 = []) { @@ -146,7 +146,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 = []) { @@ -157,7 +157,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 04f4ffe..f45d199 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 38a3c24..f78e2ce 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) { diff --git a/src/Support/Migrations/SchemaParser.php b/src/Support/Migrations/SchemaParser.php index b834a01..2e0a35c 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) @@ -120,7 +120,7 @@ public function getSchemas() /** * Get column name from schema. * - * @param string $schema + * @param string $schema * @return string */ public function getColumn($schema) @@ -131,13 +131,13 @@ public function getColumn($schema) /** * Get column attributes. * - * @param string $column - * @param string $schema + * @param string $column + * @param string $schema * @return array */ public function getAttributes($column, $schema) { - $fields = str_replace($column.':', '', $schema); + $fields = str_replace($column . ':', '', $schema); return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields); } @@ -145,7 +145,7 @@ public function getAttributes($column, $schema) /** * Determine whether the given column is exist in customAttributes array. * - * @param string $column + * @param string $column * @return bool */ public function hasCustomAttribute($column) @@ -156,25 +156,25 @@ public function hasCustomAttribute($column) /** * Get custom attributes value. * - * @param string $column + * @param string $column * @return array */ public function getCustomAttribute($column) { - return (array) $this->customAttributes[$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) { if (in_array($column, $this->relationshipKeys)) { @@ -184,23 +184,23 @@ public function createField($column, $attributes, $type = 'add') } } - 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}')"; @@ -209,10 +209,10 @@ protected function addRelationColumn($key, $field, $column) return "->on('{$field}')"; } if (Str::contains($field, '(')) { - return '->'.$field; + return '->' . $field; } - return '->'.$field.'()'; + return '->' . $field . '()'; } /** @@ -235,42 +235,42 @@ 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 . '()'; } /** * 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 removeColumn($key, $field, $column) { if ($this->hasCustomAttribute($column)) { - return '->'.$field; + return '->' . $field; } - return '->dropColumn('."'".$column."')"; + return '->dropColumn(' . "'" . $column . "')"; } } diff --git a/src/Support/Stub.php b/src/Support/Stub.php index 74c255e..7e0ee06 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 3fac4a2..7740cce 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 f34cd33..6f0a538 100644 --- a/src/Traits/ModuleCommandTrait.php +++ b/src/Traits/ModuleCommandTrait.php @@ -6,24 +6,6 @@ trait ModuleCommandTrait { - /** - * return module path with start and closing slash - * - * @return string - * - * @throws GeneratorException - */ - public function getModulePath(?string $module = null) - { - if ($module == null) { - $module = $this->getModuleName(); - } - - $rootPath = config('fintech.generators.paths.modules'); - - return $rootPath.'/'.$module.'/'; - } - /** * Get the module name. * @@ -36,30 +18,14 @@ public function getModuleName(): string $module = config('api-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.'); } return $module; } - - /** - * return module namespace with start and closing slash - * - * @throws GeneratorException - */ - public function getModuleNS(?string $module = null): string - { - if ($module == null) { - $module = $this->getModuleName(); - } - - $rootPath = config('api-crud.namespace', 'App'); - - return '\\'.$rootPath.'\\'.$module.'\\'; - } } diff --git a/src/helpers.php b/src/helpers.php index d1263a6..e772e48 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,6 +1,6 @@ set('app.env', 'testing'); + config()->set('database.default', 'testing'); + + $migrations = [ + ]; + foreach ($migrations as $migration) { + $migration->up(); + } + } + protected function setUp(): void { parent::setUp(); @@ -24,16 +36,4 @@ protected function getPackageProviders($app): array ApiCrudServiceProvider::class, ]; } - - public function getEnvironmentSetUp($app): void - { - config()->set('app.env', 'testing'); - config()->set('database.default', 'testing'); - - $migrations = [ - ]; - foreach ($migrations as $migration) { - $migration->up(); - } - } }