Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 14 additions & 11 deletions src/Prettus/Repository/Generators/BindingsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class BindingsGenerator extends Generator

public function run()
{


// Add entity repository binding to the repository service provider
$provider = \File::get($this->getPath());
$repositoryInterface = '\\' . $this->getRepository() . "::class";
Expand All @@ -40,7 +38,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';
}

/**
Expand All @@ -50,7 +49,7 @@ public function getPath()
*/
public function getBasePath()
{
return config('repository.generator.basePath', app()->path());
return app()->path();
}

/**
Expand All @@ -60,7 +59,7 @@ public function getBasePath()
*/
public function getPathConfigNode()
{
return 'provider';
return '';
}

/**
Expand All @@ -72,14 +71,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';
}

/**
Expand All @@ -91,14 +90,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());
}

/**
Expand All @@ -108,7 +107,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());
}

/**
Expand All @@ -118,7 +122,6 @@ public function getRootNamespace()
*/
public function getReplacements()
{

return array_merge(parent::getReplacements(), [
'repository' => $this->getRepository(),
'eloquent' => $this->getEloquentRepository(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ 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()),
'name' => 'RepositoryServiceProvider',
]);
// placeholder to mark the place in file where to prepend repository bindings
$provider = File::get($bindingGenerator->getPath());
Expand Down Expand Up @@ -96,6 +97,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
]
];
}

Expand Down
49 changes: 39 additions & 10 deletions src/Prettus/Repository/Generators/Commands/ControllerCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prettus\Repository\Generators\Commands;

use Illuminate\Console\Command;
Expand Down Expand Up @@ -42,17 +43,18 @@ class ControllerCommand extends Command
*/
public function __construct()
{
$this->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());
}

Expand All @@ -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();

Expand Down Expand Up @@ -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
]
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function fire()
try {
(new CriteriaGenerator([
'name' => $this->argument('name'),
'module' => $this->argument('module'),
'force' => $this->option('force'),
]))->run();

Expand All @@ -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
]
];
}

Expand Down
21 changes: 14 additions & 7 deletions src/Prettus/Repository/Generators/Commands/EntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
}
Expand All @@ -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,
Expand All @@ -92,6 +92,7 @@ public function fire()

$this->call('make:bindings', [
'name' => $this->argument('name'),
'module' => $this->argument('module'),
'--force' => $this->option('force')
]);
}
Expand All @@ -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
]
];
}

Expand Down
12 changes: 10 additions & 2 deletions src/Prettus/Repository/Generators/Commands/PresenterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ public function fire()
try {
(new PresenterGenerator([
'name' => $this->argument('name'),
'module' => $this->argument('module'),
'force' => $this->option('force'),
]))->run();
$this->info("Presenter created successfully.");

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.");
}
Expand All @@ -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
]
];
}

Expand Down
Loading