From ff64d3a15b4b76cfa07edee5008a1a0e76f9161b Mon Sep 17 00:00:00 2001 From: Mekaeil Date: Mon, 18 Jan 2021 13:31:59 +0330 Subject: [PATCH 1/4] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9bd1e185..e6240fed 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "prettus/l5-repository", + "name": "weprodev/l5-repository", "description": "Laravel 5|6|7|8 - Repositories to the database layer", "keywords": [ "laravel", From 871585a0974148b3e669c53d7d2d0953da860bcd Mon Sep 17 00:00:00 2001 From: Mekaeil Date: Wed, 20 Jan 2021 13:13:18 +0330 Subject: [PATCH 2/4] Editing the Generation Files Classes, for modular structure projects. --- README.md | 59 ++++++++++++++++ composer.json | 2 +- .../Generators/BindingsGenerator.php | 27 +++++--- .../Generators/Commands/BindingsCommand.php | 8 +++ .../Generators/Commands/ControllerCommand.php | 49 ++++++++++--- .../Generators/Commands/CriteriaCommand.php | 7 ++ .../Generators/Commands/EntityCommand.php | 21 ++++-- .../Generators/Commands/PresenterCommand.php | 12 +++- .../Generators/Commands/RepositoryCommand.php | 26 ++++--- .../Commands/TransformerCommand.php | 11 ++- .../Generators/Commands/ValidatorCommand.php | 13 +++- .../Generators/ControllerGenerator.php | 7 +- .../Repository/Generators/Generator.php | 68 ++++++++++++++----- .../Generators/MigrationGenerator.php | 20 ++++-- .../Generators/PresenterGenerator.php | 2 +- .../RepositoryEloquentGenerator.php | 19 +++--- .../RepositoryInterfaceGenerator.php | 6 +- .../Stubs/controller/controller.stub | 2 +- .../Generators/Stubs/repository/eloquent.stub | 4 +- .../Stubs/repository/interface.stub | 2 +- .../Generators/TransformerGenerator.php | 5 +- src/resources/config/repository.php | 48 ++++++++++++- 22 files changed, 327 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index c151d2e7..8824bef8 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,65 @@ Publish Configuration ```shell php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider" ``` +*Notice* now you can change your config generation files as your project structure: + +* when you want to use the default laravel structure: +``` + 'generator' => [ + 'structure' => 'default', // modular, default + 'modules' => [], + 'basePath' => app()->path(), + 'rootNamespace' => 'App\\', + 'stubsOverridePath' => app()->path(), + 'paths' => [ + 'models' => 'Entities', + 'repositories' => 'Repositories', + 'interfaces' => 'Repositories', + 'transformers' => 'Transformers', + 'presenters' => 'Presenters', + 'validators' => 'Validators', + 'controllers' => 'Http/Controllers', + 'provider' => 'RepositoryServiceProvider', + 'criteria' => 'Criteria' + ] + ], +``` +* when you want to use the `modular` structure like [nWidart Package](https://nwidart.com/laravel-modules/v6/introduction) you should change the config as below: +``` + 'generator' => [ + 'structure' => 'modular', // modular, default + 'package' => 'nwidart', // if you use the nwidart or another package, add the name of the package and the package's commands. + 'modules' => [ + 'Core', + 'User', + ], + 'packageCommands' => [ + 'nwidart' => [ + 'controllers' => 'module:make-controller', + 'requests' => 'module:make-request', + 'models' => 'module:make-model', + ] + ], + 'ORM' => 'eloquent', + + 'basePath' => base_path('Modules'), + 'rootNamespace' => 'Modules\\', + 'moduleNamespace' => 'Modules\\', // if you use the modular structure you should add this attribute + 'stubsOverridePath' => app()->path(), + 'provider' => app()->path() . "/Providers/", + 'paths' => [ + 'models' => 'Entities', + 'repositories' => 'Repositories', + 'interfaces' => 'Repositories', + 'transformers' => 'Transformers', + 'presenters' => 'Presenters', + 'validators' => 'Validators', + 'controllers' => 'Http/Controllers', + 'criteria' => 'Criteria', + 'migrations' => '/Database/Migrations/', // when you have all the migration files in default path, remove this + ], + ] +``` ## Methods diff --git a/composer.json b/composer.json index e6240fed..9bd1e185 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "weprodev/l5-repository", + "name": "prettus/l5-repository", "description": "Laravel 5|6|7|8 - Repositories to the database layer", "keywords": [ "laravel", diff --git a/src/Prettus/Repository/Generators/BindingsGenerator.php b/src/Prettus/Repository/Generators/BindingsGenerator.php index 515b2aac..62ef3a2a 100644 --- a/src/Prettus/Repository/Generators/BindingsGenerator.php +++ b/src/Prettus/Repository/Generators/BindingsGenerator.php @@ -1,6 +1,8 @@ getPath()); $repositoryInterface = '\\' . $this->getRepository() . "::class"; @@ -40,7 +40,8 @@ public function run() */ public function getPath() { - return $this->getBasePath() . '/Providers/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '.php'; + $default_path = app()->path() . "/Providers/"; + return config("repository.generator.provider", $default_path) . '/RepositoryServiceProvider.php'; } /** @@ -50,7 +51,7 @@ public function getPath() */ public function getBasePath() { - return config('repository.generator.basePath', app()->path()); + return app()->path(); } /** @@ -60,7 +61,7 @@ public function getBasePath() */ public function getPathConfigNode() { - return 'provider'; + return ''; } /** @@ -72,14 +73,14 @@ public function getRepository() { $repositoryGenerator = new RepositoryInterfaceGenerator([ 'name' => $this->name, + 'module' => $this->module, ]); $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName(); - return str_replace([ "\\", '/' - ], '\\', $repository) . 'Repository'; + ], '\\', $repository) . 'RepositoryInterface'; } /** @@ -91,14 +92,14 @@ public function getEloquentRepository() { $repositoryGenerator = new RepositoryEloquentGenerator([ 'name' => $this->name, + 'module' => $this->module, ]); $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName(); - return str_replace([ "\\", '/' - ], '\\', $repository) . 'RepositoryEloquent'; + ], '\\', $repository) . ('Repository' . $this->getORM()); } /** @@ -108,7 +109,12 @@ public function getEloquentRepository() */ public function getRootNamespace() { - return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode()); + $default_namespace = app()->getNamespace() . "\Providers"; + return str_replace([ + "\\", + '/' + ], '\\', config("repository.generator.provider", $default_namespace)); +// return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode()); } /** @@ -118,7 +124,6 @@ public function getRootNamespace() */ public function getReplacements() { - return array_merge(parent::getReplacements(), [ 'repository' => $this->getRepository(), 'eloquent' => $this->getEloquentRepository(), diff --git a/src/Prettus/Repository/Generators/Commands/BindingsCommand.php b/src/Prettus/Repository/Generators/Commands/BindingsCommand.php index 93f50d80..51cebee0 100644 --- a/src/Prettus/Repository/Generators/Commands/BindingsCommand.php +++ b/src/Prettus/Repository/Generators/Commands/BindingsCommand.php @@ -58,12 +58,14 @@ public function fire() try { $bindingGenerator = new BindingsGenerator([ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), 'force' => $this->option('force'), ]); // generate repository service provider if (!file_exists($bindingGenerator->getPath())) { $this->call('make:provider', [ 'name' => $bindingGenerator->getConfigGeneratorClassPath($bindingGenerator->getPathConfigNode()), + 'module' => $this->argument('module'), ]); // placeholder to mark the place in file where to prepend repository bindings $provider = File::get($bindingGenerator->getPath()); @@ -96,6 +98,12 @@ public function getArguments() 'The name of model for which the controller is being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/ControllerCommand.php b/src/Prettus/Repository/Generators/Commands/ControllerCommand.php index 5ceabf26..ced45300 100644 --- a/src/Prettus/Repository/Generators/Commands/ControllerCommand.php +++ b/src/Prettus/Repository/Generators/Commands/ControllerCommand.php @@ -1,4 +1,5 @@ name = ((float) app()->version() >= 5.5 ? 'make:rest-controller' : 'make:resource'); + $this->name = ((float)app()->version() >= 5.5 ? 'make:rest-controller' : 'make:resource'); parent::__construct(); } /** * Execute the command. * - * @see fire() * @return void + * @see fire() */ - public function handle(){ + public function handle() + { $this->laravel->call([$this, 'fire'], func_get_args()); } @@ -64,18 +66,39 @@ public function handle(){ public function fire() { try { - // Generate create request for controller - $this->call('make:request', [ - 'name' => $this->argument('name') . 'CreateRequest' - ]); + $available_package = config("repository.generator.package"); + $package_commands = config("repository.generator.packageCommands"); + $commands = "make:request"; + + $create_arguments = [ + 'name' => $this->argument('name') . 'CreateRequest', + ]; + $update_arguments = [ + 'name' => $this->argument('name') . 'UpdateRequest', + ]; + + if (!is_null($available_package) && trim($available_package) != '' && + !empty($package_commands) && $this->argument('module') && + $package_commands[$available_package]['requests']) { + + $commands = $package_commands[$available_package]['requests']; + $create_arguments = array_merge($create_arguments,[ + 'module' => $this->argument('module'), + ]); + $update_arguments = array_merge($update_arguments,[ + 'module' => $this->argument('module'), + ]); + } + + // Generate create request for controller + $this->call($commands, $create_arguments); // Generate update request for controller - $this->call('make:request', [ - 'name' => $this->argument('name') . 'UpdateRequest' - ]); + $this->call($commands, $update_arguments); (new ControllerGenerator([ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), 'force' => $this->option('force'), ]))->run(); @@ -103,6 +126,12 @@ public function getArguments() 'The name of model for which the controller is being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/CriteriaCommand.php b/src/Prettus/Repository/Generators/Commands/CriteriaCommand.php index 56c3005a..4ca4a7bc 100644 --- a/src/Prettus/Repository/Generators/Commands/CriteriaCommand.php +++ b/src/Prettus/Repository/Generators/Commands/CriteriaCommand.php @@ -56,6 +56,7 @@ public function fire() try { (new CriteriaGenerator([ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), 'force' => $this->option('force'), ]))->run(); @@ -80,6 +81,12 @@ public function getArguments() 'The name of class being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/EntityCommand.php b/src/Prettus/Repository/Generators/Commands/EntityCommand.php index 78613712..5df3cb82 100644 --- a/src/Prettus/Repository/Generators/Commands/EntityCommand.php +++ b/src/Prettus/Repository/Generators/Commands/EntityCommand.php @@ -54,6 +54,7 @@ public function fire() if ($this->confirm('Would you like to create a Presenter? [y|N]')) { $this->call('make:presenter', [ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), '--force' => $this->option('force'), ]); } @@ -66,24 +67,23 @@ public function fire() if ($validator == 'yes') { $this->call('make:validator', [ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), '--rules' => $this->option('rules'), '--force' => $this->option('force'), ]); } if ($this->confirm('Would you like to create a Controller? [y|N]')) { - - $resource_args = [ - 'name' => $this->argument('name') - ]; - - // Generate a controller resource $controller_command = ((float) app()->version() >= 5.5 ? 'make:rest-controller' : 'make:resource'); - $this->call($controller_command, $resource_args); + $this->call($controller_command, [ + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + ]); } $this->call('make:repository', [ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), '--fillable' => $this->option('fillable'), '--rules' => $this->option('rules'), '--validator' => $validator, @@ -92,6 +92,7 @@ public function fire() $this->call('make:bindings', [ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), '--force' => $this->option('force') ]); } @@ -111,6 +112,12 @@ public function getArguments() 'The name of class being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/PresenterCommand.php b/src/Prettus/Repository/Generators/Commands/PresenterCommand.php index 797062e5..5782ae99 100644 --- a/src/Prettus/Repository/Generators/Commands/PresenterCommand.php +++ b/src/Prettus/Repository/Generators/Commands/PresenterCommand.php @@ -58,6 +58,7 @@ public function fire() try { (new PresenterGenerator([ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), 'force' => $this->option('force'), ]))->run(); $this->info("Presenter created successfully."); @@ -65,8 +66,9 @@ public function fire() if (!\File::exists(app()->path() . '/Transformers/' . $this->argument('name') . 'Transformer.php')) { if ($this->confirm('Would you like to create a Transformer? [y|N]')) { (new TransformerGenerator([ - 'name' => $this->argument('name'), - 'force' => $this->option('force'), + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + 'force' => $this->option('force'), ]))->run(); $this->info("Transformer created successfully."); } @@ -93,6 +95,12 @@ public function getArguments() 'The name of model for which the presenter is being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/RepositoryCommand.php b/src/Prettus/Repository/Generators/Commands/RepositoryCommand.php index 012f88c8..2a99a834 100644 --- a/src/Prettus/Repository/Generators/Commands/RepositoryCommand.php +++ b/src/Prettus/Repository/Generators/Commands/RepositoryCommand.php @@ -67,9 +67,10 @@ public function fire() $this->generators = new Collection(); $migrationGenerator = new MigrationGenerator([ - 'name' => 'create_' . Str::snake(Str::plural($this->argument('name'))) . '_table', - 'fields' => $this->option('fillable'), - 'force' => $this->option('force'), + 'name' => 'create_' . Str::snake(Str::plural($this->argument('name'))) . '_table', + 'module' => $this->argument('module'), + 'fields' => $this->option('fillable'), + 'force' => $this->option('force'), ]); if (!$this->option('skip-migration')) { @@ -77,9 +78,10 @@ public function fire() } $modelGenerator = new ModelGenerator([ - 'name' => $this->argument('name'), - 'fillable' => $this->option('fillable'), - 'force' => $this->option('force') + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + 'fillable' => $this->option('fillable'), + 'force' => $this->option('force') ]); if (!$this->option('skip-model')) { @@ -87,8 +89,9 @@ public function fire() } $this->generators->push(new RepositoryInterfaceGenerator([ - 'name' => $this->argument('name'), - 'force' => $this->option('force'), + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + 'force' => $this->option('force'), ])); foreach ($this->generators as $generator) { @@ -104,6 +107,7 @@ public function fire() try { (new RepositoryEloquentGenerator([ 'name' => $this->argument('name'), + 'module' => $this->argument('module'), 'rules' => $this->option('rules'), 'validator' => $this->option('validator'), 'force' => $this->option('force'), @@ -132,6 +136,12 @@ public function getArguments() 'The name of class being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/TransformerCommand.php b/src/Prettus/Repository/Generators/Commands/TransformerCommand.php index 390ba04b..0077ff19 100644 --- a/src/Prettus/Repository/Generators/Commands/TransformerCommand.php +++ b/src/Prettus/Repository/Generators/Commands/TransformerCommand.php @@ -56,8 +56,9 @@ public function fire() { try { (new TransformerGenerator([ - 'name' => $this->argument('name'), - 'force' => $this->option('force'), + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + 'force' => $this->option('force'), ]))->run(); $this->info("Transformer created successfully."); } catch (FileAlreadyExistsException $e) { @@ -82,6 +83,12 @@ public function getArguments() 'The name of model for which the transformer is being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/Commands/ValidatorCommand.php b/src/Prettus/Repository/Generators/Commands/ValidatorCommand.php index 630a0fe0..95201297 100644 --- a/src/Prettus/Repository/Generators/Commands/ValidatorCommand.php +++ b/src/Prettus/Repository/Generators/Commands/ValidatorCommand.php @@ -56,9 +56,10 @@ public function fire() { try { (new ValidatorGenerator([ - 'name' => $this->argument('name'), - 'rules' => $this->option('rules'), - 'force' => $this->option('force'), + 'name' => $this->argument('name'), + 'module' => $this->argument('module'), + 'rules' => $this->option('rules'), + 'force' => $this->option('force'), ]))->run(); $this->info("Validator created successfully."); } catch (FileAlreadyExistsException $e) { @@ -83,6 +84,12 @@ public function getArguments() 'The name of model for which the validator is being generated.', null ], + [ + 'module', + InputArgument::OPTIONAL, + 'The module name for kind of the modular project and creating files on each module', + null + ] ]; } diff --git a/src/Prettus/Repository/Generators/ControllerGenerator.php b/src/Prettus/Repository/Generators/ControllerGenerator.php index b9385977..8977fb1a 100644 --- a/src/Prettus/Repository/Generators/ControllerGenerator.php +++ b/src/Prettus/Repository/Generators/ControllerGenerator.php @@ -65,7 +65,6 @@ public function getBasePath() */ public function getControllerName() { - return ucfirst($this->getPluralName()); } @@ -76,7 +75,6 @@ public function getControllerName() */ public function getPluralName() { - return Str::plural(lcfirst(ucwords($this->getClass()))); } @@ -87,7 +85,6 @@ public function getPluralName() */ public function getReplacements() { - return array_merge(parent::getReplacements(), [ 'controller' => $this->getControllerName(), 'plural' => $this->getPluralName(), @@ -117,6 +114,7 @@ public function getValidator() { $validatorGenerator = new ValidatorGenerator([ 'name' => $this->name, + 'module' => $this->module, ]); $validator = $validatorGenerator->getRootNamespace() . '\\' . $validatorGenerator->getName(); @@ -137,6 +135,7 @@ public function getRepository() { $repositoryGenerator = new RepositoryInterfaceGenerator([ 'name' => $this->name, + 'module' => $this->module, ]); $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName(); @@ -144,6 +143,6 @@ public function getRepository() return 'use ' . str_replace([ "\\", '/' - ], '\\', $repository) . 'Repository;'; + ], '\\', $repository) . 'RepositoryInterface;'; } } diff --git a/src/Prettus/Repository/Generators/Generator.php b/src/Prettus/Repository/Generators/Generator.php index 4a0db397..ebbab8fd 100644 --- a/src/Prettus/Repository/Generators/Generator.php +++ b/src/Prettus/Repository/Generators/Generator.php @@ -34,6 +34,12 @@ abstract class Generator */ protected $stub; + /** + * The structure type and list of the modules + * + * @var string + */ + protected $structure, $modules, $ORM; /** * Create new instance of this class. @@ -44,6 +50,9 @@ public function __construct(array $options = []) { $this->filesystem = new Filesystem; $this->options = $options; + $this->structure = config('repository.generator.structure'); + $this->modules = config('repository.generator.modules'); + $this->ORM = config("repository.generator.ORM", 'Eloquent'); } @@ -68,10 +77,28 @@ public function getFilesystem() public function setFilesystem(Filesystem $filesystem) { $this->filesystem = $filesystem; - return $this; } + /** + * get the stub name + * + * @return string + */ + public function getStubName() + { + return $this->stub; + } + + /** + * get the ORM Type + * + * @return string + */ + public function getORM() + { + return ucfirst(strtolower(config("repository.generator.ORM", 'Eloquent'))); + } /** * Get stub template for generated file. @@ -82,11 +109,11 @@ public function getStub() { $path = config('repository.generator.stubsOverridePath', __DIR__); - if(!file_exists($path . '/Stubs/' . $this->stub . '.stub')){ + if (!file_exists($path . '/Stubs/' . $this->getStubName() . '.stub')) { $path = __DIR__; } - return (new Stub($path . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render(); + return (new Stub($path . '/Stubs/' . $this->getStubName() . '.stub', $this->getReplacements()))->render(); } @@ -98,8 +125,8 @@ public function getStub() public function getReplacements() { return [ - 'class' => $this->getClass(), - 'namespace' => $this->getNamespace(), + 'class' => $this->getClass(), + 'namespace' => $this->getNamespace(), 'root_namespace' => $this->getRootNamespace() ]; } @@ -146,7 +173,7 @@ public function getName() } - /** + /** * Get application namespace * * @return string @@ -197,7 +224,7 @@ public function getRootNamespace() * * @return string */ - public function getConfigGeneratorClassPath($class, $directoryPath = false) + public function getConfigGeneratorClassPath($class, $directoryPath = false, $module = null) { switch ($class) { case ('models' === $class): @@ -221,23 +248,30 @@ public function getConfigGeneratorClassPath($class, $directoryPath = false) case ('controllers' === $class): $path = config('repository.generator.paths.controllers', 'Http\Controllers'); break; - case ('provider' === $class): - $path = config('repository.generator.paths.provider', 'RepositoryServiceProvider'); - break; case ('criteria' === $class): $path = config('repository.generator.paths.criteria', 'Criteria'); break; + case ('migrations' === $class): + $path = config('repository.generator.paths.migrations', 'database\migrations'); + break; default: $path = ''; } + // CHECKING IF WE HAVE MODULAR STRUCTURE + $is_module = false; + if($this->structure == 'modular' && !empty($this->modules) && in_array($this->module, $this->modules)){ + $is_module = true; + } + if ($directoryPath) { $path = str_replace('\\', '/', $path); + $path = $is_module ? ($this->module . "/" . $path) : $path; } else { $path = str_replace('/', '\\', $path); + $path = $is_module ? ($this->module . "\\" . $path) : $path; } - return $path; } @@ -308,7 +342,7 @@ public function getOptions() /** * Determinte whether the given key exist in options array. * - * @param string $key + * @param string $key * * @return boolean */ @@ -321,8 +355,8 @@ public function hasOption($key) /** * Get value from options by given key. * - * @param string $key - * @param string|null $default + * @param string $key + * @param string|null $default * * @return string */ @@ -339,8 +373,8 @@ public function getOption($key, $default = null) /** * Helper method for "getOption". * - * @param string $key - * @param string|null $default + * @param string $key + * @param string|null $default * * @return string */ @@ -353,7 +387,7 @@ public function option($key, $default = null) /** * Handle call to __get method. * - * @param string $key + * @param string $key * * @return string|mixed */ diff --git a/src/Prettus/Repository/Generators/MigrationGenerator.php b/src/Prettus/Repository/Generators/MigrationGenerator.php index 0b950ecd..29a2e689 100644 --- a/src/Prettus/Repository/Generators/MigrationGenerator.php +++ b/src/Prettus/Repository/Generators/MigrationGenerator.php @@ -29,7 +29,7 @@ class MigrationGenerator extends Generator */ public function getBasePath() { - return base_path() . '/database/migrations/'; + return config('repository.generator.basePath', app()->path()); } @@ -40,7 +40,7 @@ public function getBasePath() */ public function getPath() { - return $this->getBasePath() . $this->getFileName() . '.php'; + return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getFileName() . '.php'; } @@ -51,7 +51,7 @@ public function getPath() */ public function getPathConfigNode() { - return ''; + return 'migrations'; } @@ -73,6 +73,10 @@ public function getRootNamespace() */ public function getMigrationName() { + if ($this->structure == 'modular' && $this->module){ + return strtolower($this->module) . "_" . strtolower($this->name); + } + return strtolower($this->name); } @@ -118,6 +122,10 @@ public function getNameParser() public function getStub() { $parser = $this->getNameParser(); + $table_name = $parser->getTable(); + if ($this->structure == 'modular' && $this->module){ + $table_name = strtolower($this->module) . "_" . $parser->getTable(); + } $action = $parser->getAction(); switch ($action) { @@ -128,7 +136,7 @@ public function getStub() $file = 'change'; $replacements = [ 'class' => $this->getClass(), - 'table' => $parser->getTable(), + 'table' => $table_name, 'fields_up' => $this->getSchemaParser()->up(), 'fields_down' => $this->getSchemaParser()->down(), ]; @@ -140,7 +148,7 @@ public function getStub() $file = 'change'; $replacements = [ 'class' => $this->getClass(), - 'table' => $parser->getTable(), + 'table' => $table_name, 'fields_down' => $this->getSchemaParser()->up(), 'fields_up' => $this->getSchemaParser()->down(), ]; @@ -149,7 +157,7 @@ public function getStub() $file = 'create'; $replacements = [ 'class' => $this->getClass(), - 'table' => $parser->getTable(), + 'table' => $table_name, 'fields' => $this->getSchemaParser()->up(), ]; break; diff --git a/src/Prettus/Repository/Generators/PresenterGenerator.php b/src/Prettus/Repository/Generators/PresenterGenerator.php index e9dfaf02..fc77b489 100644 --- a/src/Prettus/Repository/Generators/PresenterGenerator.php +++ b/src/Prettus/Repository/Generators/PresenterGenerator.php @@ -50,7 +50,7 @@ public function getReplacements() "\\", '/' ], '\\', $transformer); - echo $transformer; +// echo $transformer; return array_merge(parent::getReplacements(), [ 'transformer' => $transformer diff --git a/src/Prettus/Repository/Generators/RepositoryEloquentGenerator.php b/src/Prettus/Repository/Generators/RepositoryEloquentGenerator.php index c39a56a7..c51b5bbb 100644 --- a/src/Prettus/Repository/Generators/RepositoryEloquentGenerator.php +++ b/src/Prettus/Repository/Generators/RepositoryEloquentGenerator.php @@ -11,12 +11,11 @@ class RepositoryEloquentGenerator extends Generator { - /** - * Get stub name. - * - * @var string - */ - protected $stub = 'repository/eloquent'; + public function getStubName() + { + $this->stub = 'repository/' . strtolower($this->ORM); + return $this->stub; + } /** * Get root namespace. @@ -25,7 +24,7 @@ class RepositoryEloquentGenerator extends Generator */ public function getRootNamespace() { - return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode()); + return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode()) . "\\" . $this->getORM(); } /** @@ -45,7 +44,9 @@ public function getPathConfigNode() */ public function getPath() { - return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getName() . 'RepositoryEloquent.php'; + $get_orm = $this->getORM(); + return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . + "/{$get_orm}/" . $this->getName() . "Repository{$get_orm}.php"; } /** @@ -65,7 +66,7 @@ public function getBasePath() */ public function getReplacements() { - $repository = parent::getRootNamespace() . parent::getConfigGeneratorClassPath('interfaces') . '\\' . $this->name . 'Repository;'; + $repository = parent::getRootNamespace() . parent::getConfigGeneratorClassPath('interfaces') . '\\Contracts\\' . $this->name . 'RepositoryInterface;'; $repository = str_replace([ "\\", '/' diff --git a/src/Prettus/Repository/Generators/RepositoryInterfaceGenerator.php b/src/Prettus/Repository/Generators/RepositoryInterfaceGenerator.php index f4bb0bc9..016d8b9b 100644 --- a/src/Prettus/Repository/Generators/RepositoryInterfaceGenerator.php +++ b/src/Prettus/Repository/Generators/RepositoryInterfaceGenerator.php @@ -2,6 +2,7 @@ namespace Prettus\Repository\Generators; +use Illuminate\Support\Facades\Log; use Prettus\Repository\Generators\Migrations\SchemaParser; /** @@ -26,7 +27,8 @@ class RepositoryInterfaceGenerator extends Generator */ public function getRootNamespace() { - return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode()); + Log::info($this->module); + return parent::getRootNamespace() . parent::getConfigGeneratorClassPath(($pathConfigNode ?? $this->getPathConfigNode())) . '\\Contracts'; } /** @@ -46,7 +48,7 @@ public function getPathConfigNode() */ public function getPath() { - return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getName() . 'Repository.php'; + return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/Contracts/' . $this->getName() . 'RepositoryInterface.php'; } /** diff --git a/src/Prettus/Repository/Generators/Stubs/controller/controller.stub b/src/Prettus/Repository/Generators/Stubs/controller/controller.stub index d1747192..d4bcc119 100644 --- a/src/Prettus/Repository/Generators/Stubs/controller/controller.stub +++ b/src/Prettus/Repository/Generators/Stubs/controller/controller.stub @@ -3,8 +3,8 @@ $NAMESPACE$ use Illuminate\Http\Request; +use Illuminate\Routing\Controller; -use $APPNAME$Http\Requests; use Prettus\Validator\Contracts\ValidatorInterface; use Prettus\Validator\Exceptions\ValidatorException; use $APPNAME$Http\Requests\$CLASS$CreateRequest; diff --git a/src/Prettus/Repository/Generators/Stubs/repository/eloquent.stub b/src/Prettus/Repository/Generators/Stubs/repository/eloquent.stub index cd5aa206..53767f34 100644 --- a/src/Prettus/Repository/Generators/Stubs/repository/eloquent.stub +++ b/src/Prettus/Repository/Generators/Stubs/repository/eloquent.stub @@ -13,7 +13,7 @@ $USE_VALIDATOR$ * * @package $NAMESPACE$ */ -class $CLASS$RepositoryEloquent extends BaseRepository implements $CLASS$Repository +class $CLASS$RepositoryEloquent extends BaseRepository implements $CLASS$RepositoryInterface { /** * Specify Model class name @@ -34,5 +34,5 @@ class $CLASS$RepositoryEloquent extends BaseRepository implements $CLASS$Reposit { $this->pushCriteria(app(RequestCriteria::class)); } - + } diff --git a/src/Prettus/Repository/Generators/Stubs/repository/interface.stub b/src/Prettus/Repository/Generators/Stubs/repository/interface.stub index c5e7ff39..e98b2ca4 100644 --- a/src/Prettus/Repository/Generators/Stubs/repository/interface.stub +++ b/src/Prettus/Repository/Generators/Stubs/repository/interface.stub @@ -9,7 +9,7 @@ use Prettus\Repository\Contracts\RepositoryInterface; * * @package $NAMESPACE$ */ -interface $CLASS$Repository extends RepositoryInterface +interface $CLASS$RepositoryInterface extends RepositoryInterface { // } diff --git a/src/Prettus/Repository/Generators/TransformerGenerator.php b/src/Prettus/Repository/Generators/TransformerGenerator.php index 3bff7d71..0fe4b735 100644 --- a/src/Prettus/Repository/Generators/TransformerGenerator.php +++ b/src/Prettus/Repository/Generators/TransformerGenerator.php @@ -63,9 +63,10 @@ public function getBasePath() public function getReplacements() { $modelGenerator = new ModelGenerator([ - 'name' => $this->name + 'name' => $this->name, + 'module' => $this->module, ]); - $model = $modelGenerator->getRootNamespace() . '\\' . $modelGenerator->getName(); + $model = $modelGenerator->getRootNamespace() . '\\' . $modelGenerator->getName(); $model = str_replace([ "\\", '/' diff --git a/src/resources/config/repository.php b/src/resources/config/repository.php index f8498b8d..71000de5 100644 --- a/src/resources/config/repository.php +++ b/src/resources/config/repository.php @@ -223,11 +223,13 @@ ], /* |-------------------------------------------------------------------------- - | Generator Config + | Generator Config: for default structure |-------------------------------------------------------------------------- | */ 'generator' => [ + 'structure' => 'default', // modular, default + 'modules' => [], 'basePath' => app()->path(), 'rootNamespace' => 'App\\', 'stubsOverridePath' => app()->path(), @@ -242,5 +244,47 @@ 'provider' => 'RepositoryServiceProvider', 'criteria' => 'Criteria' ] - ] + ], + /* + |-------------------------------------------------------------------------- + | Generator Config: for modular structure + |-------------------------------------------------------------------------- + | IF THE TYPE IS = 'default' THE MODULES VALUE IS AN EMPTY ARRAY, + | IF THE TYPE IS = 'modular' YOU CAN ADD LIST OF THE MODULES AS AN ARRAY + | + */ +// 'generator' => [ +// 'structure' => 'modular', // modular, default +// 'package' => 'nwidart', // if you use the nwidart or another package, add the name of the package and the package's commands. +// 'modules' => [ +// 'Core', +// 'User', +// ], +// 'packageCommands' => [ +// 'nwidart' => [ +// 'controllers' => 'module:make-controller', +// 'requests' => 'module:make-request', +// 'models' => 'module:make-model', +// ] +// ], +// 'ORM' => 'eloquent', +// +// 'basePath' => base_path('Modules'), +// 'rootNamespace' => 'Modules\\', +// 'moduleNamespace' => 'Modules\\', // if you use the modular structure you should add this attribute +// 'stubsOverridePath' => app()->path(), +// 'provider' => app()->path() . "/Providers/", +// 'paths' => [ +// 'models' => 'Entities', +// 'repositories' => 'Repositories', +// 'interfaces' => 'Repositories', +// 'transformers' => 'Transformers', +// 'presenters' => 'Presenters', +// 'validators' => 'Validators', +// 'controllers' => 'Http/Controllers', +// 'criteria' => 'Criteria', +// 'migrations' => '/Database/Migrations/', // when you have all the migration files in default path, remove this +// ], +// ], + ]; From a7982203934443d02bde30220430dc1f7cc3ee61 Mon Sep 17 00:00:00 2001 From: Mekaeil Date: Wed, 20 Jan 2021 13:19:22 +0330 Subject: [PATCH 3/4] removing log from some generation files --- src/Prettus/Repository/Generators/BindingsGenerator.php | 2 -- src/Prettus/Repository/Generators/Generator.php | 2 +- .../Repository/Generators/RepositoryInterfaceGenerator.php | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Prettus/Repository/Generators/BindingsGenerator.php b/src/Prettus/Repository/Generators/BindingsGenerator.php index 62ef3a2a..c03cac84 100644 --- a/src/Prettus/Repository/Generators/BindingsGenerator.php +++ b/src/Prettus/Repository/Generators/BindingsGenerator.php @@ -1,8 +1,6 @@ module); return parent::getRootNamespace() . parent::getConfigGeneratorClassPath(($pathConfigNode ?? $this->getPathConfigNode())) . '\\Contracts'; } From 0a23b318797f74c075d5e11d6a1246db5ff0a2c1 Mon Sep 17 00:00:00 2001 From: Mekaeil Date: Wed, 20 Jan 2021 14:47:49 +0330 Subject: [PATCH 4/4] bugfixed on creating repository serve provider --- src/Prettus/Repository/Generators/Commands/BindingsCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Prettus/Repository/Generators/Commands/BindingsCommand.php b/src/Prettus/Repository/Generators/Commands/BindingsCommand.php index 51cebee0..e17c75d4 100644 --- a/src/Prettus/Repository/Generators/Commands/BindingsCommand.php +++ b/src/Prettus/Repository/Generators/Commands/BindingsCommand.php @@ -64,8 +64,7 @@ public function fire() // generate repository service provider if (!file_exists($bindingGenerator->getPath())) { $this->call('make:provider', [ - 'name' => $bindingGenerator->getConfigGeneratorClassPath($bindingGenerator->getPathConfigNode()), - 'module' => $this->argument('module'), + 'name' => 'RepositoryServiceProvider', ]); // placeholder to mark the place in file where to prepend repository bindings $provider = File::get($bindingGenerator->getPath());