Skip to content

Commit

Permalink
Merge pull request #8 from hungthai1401/feature/db-seed-command
Browse files Browse the repository at this point in the history
[IMPLEMENT] add module db seed command
  • Loading branch information
hungthai1401 authored Jul 17, 2020
2 parents 470ab01 + c1757d7 commit 150e716
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 54 deletions.
17 changes: 14 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ php artisan module:remove <module-name>
```
php artisan module:make:controller <module-name> <controller-name>
```
#### Create a model
```
php artisan module:make:model <module-name> <model-name>
```
#### Create a scope
```
php artisan module:make:scope <module-name> <scope-name>
```
#### Create a provider
```
php artisan module:make:provider <module-name> <provider-name>
```
#### Create a facade
```
php artisan module:make:facade <module-name> <model-name>
php artisan module:make:facade <module-name> <facade-name>
```
#### Create a policy
```
php artisan module:make:policy <module-name> <policy-name>
```
#### Create a event
#### Create an event
```
php artisan module:make:event <module-name> <event-name>
```
Expand Down Expand Up @@ -103,4 +111,7 @@ php artisan module:make:seed <module-name> <seeder-name>
```
php artisan module:make:factory <module-name> <factory-name>
```

#### Run seeders in a module
```
php artisan module:db:seed <module-name> {--class}
```
18 changes: 5 additions & 13 deletions src/Console/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ protected function buildClass($name): string
protected function getArguments(): array
{
return [
['name', InputArgument::REQUIRED, 'The name of the class'],
['module', InputArgument::REQUIRED, 'The alias of the module', ],
['name', InputArgument::REQUIRED, 'The name of the class', ],
];
}

Expand Down Expand Up @@ -189,9 +190,7 @@ protected function parseName($name): string
*/
protected function getPath($name): string
{
$path = $this->modulePath() . 'src/' . str_replace('\\', '/', $name) . '.php';

return $path;
return $this->modulePath() . 'src/' . str_replace('\\', '/', $name) . '.php';
}

/**
Expand Down Expand Up @@ -232,9 +231,7 @@ protected function replaceNamespace(&$stub, $name): string
*/
protected function getNamespace($name): string
{
$namespace = trim(implode('\\', array_slice(explode('\\', config('modules.namespace') . '\\' . Str::studly($this->getModuleName()) . '\\' . str_replace('/', '\\', $name)), 0, -1)), '\\');

return $namespace;
return trim(implode('\\', array_slice(explode('\\', config('modules.namespace') . '\\' . Str::studly($this->getModuleName()) . '\\' . str_replace('/', '\\', $name)), 0, -1)), '\\');
}

/**
Expand All @@ -261,11 +258,6 @@ abstract protected function getStub(): string;
*/
protected function getClass(string $name): string
{
$className = $name;
if (! Str::endsWith($name, $this->type)) {
$className = $name . $this->type;
}

return Str::plural($this->type) . '\\' . $className;
return Str::plural($this->type) . '\\' . $name;
}
}
1 change: 1 addition & 0 deletions src/Console/CreateModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private function generateComposerJson(): void
$composerJson['description'] = $this->container['description'];
$composerJson['authors'][] = $this->container['authors'];
$composerJson['autoload']['psr-4'][$this->container['namespace'] . '\\'] = 'src/';
$composerJson['autoload']['classmap'] = [['database/factories', 'database/seeds']];
$composerJson['require'] = new stdClass();
$composerJson['require-dev'] = new stdClass();
$composerJson['extra']['laravel']['providers'] = $this->container['namespace'] . '\\Providers\\ModuleServiceProvider';
Expand Down
20 changes: 5 additions & 15 deletions src/Console/MakeFactory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<?php
/**
* Created by PhpStorm.
* User: mac
* Date: 2020-07-01
* Time: 23:12
*/

namespace HT\Modules\Console;


use Illuminate\Support\Str;

/**
* Command: MakeFactory
* @package HT\Modules\Console
*/
class MakeFactory extends AbstractGenerator
{
/**
Expand Down Expand Up @@ -63,11 +58,6 @@ protected function getPath($name): string
*/
protected function getClass(string $name): string
{
$className = $name;
if (! Str::endsWith($name, $this->type)) {
$className = $name . $this->type;
}

return $className;
return $name;
}
}
2 changes: 1 addition & 1 deletion src/Console/MakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getStub(): string
*/
protected function getClass(string $name): string
{
return 'Entities\\' . $this->getNameInput();
return 'Entities\\' . $name;
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/Console/MakeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace HT\Modules\Console;

use Illuminate\Support\Str;

/**
* Command: MakeProvider
* @package HT\Modules\Console
Expand Down Expand Up @@ -49,12 +47,6 @@ protected function getStub(): string
*/
protected function getClass(string $name): string
{
$className = $name;
$type = 'ServiceProvider';
if (! Str::endsWith($name, $type)) {
$className = $name . $type;
}

return 'Providers\\' . $className;
return 'Providers\\' . $name;
}
}
52 changes: 52 additions & 0 deletions src/Console/MakeScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace HT\Modules\Console;

/**
* Command: MakeScope
* @package HT\Modules\Console
*/
class MakeScope extends AbstractGenerator
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:make:scope
{module : The alias of the module}
{name : The class name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new scope for the specified module.';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Scope';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub(): string
{
return __DIR__ . '/partial_stubs/scope.stub';
}

/**
* @param string $name
* @return string
*/
protected function getClass(string $name): string
{
return 'Entities\\Scopes\\' . $name;
}
}
19 changes: 12 additions & 7 deletions src/Console/MakeSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HT\Modules\Console;

use Illuminate\Support\Str;
use Symfony\Component\Process\Process;

/**
* Command: MakeSeeder
Expand Down Expand Up @@ -43,6 +43,16 @@ protected function getStub(): string
return __DIR__ . '/partial_stubs/seeder.stub';
}

/**
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle(): void
{
parent::handle();
$process = Process::fromShellCommandline('composer du');
$process->run();
}

/**
* Get the destination class path.
*
Expand All @@ -60,11 +70,6 @@ protected function getPath($name): string
*/
protected function getClass(string $name): string
{
$className = $name;
if (! Str::endsWith($name, $this->type)) {
$className = $name . $this->type;
}

return $className;
return $name;
}
}
2 changes: 1 addition & 1 deletion src/Console/MakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function getStub(): string
*/
protected function getClass(string $name): string
{
return $this->getNameInput();
return $name;
}

/**
Expand Down
127 changes: 127 additions & 0 deletions src/Console/RunSeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace HT\Modules\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

/**
* Command: RunSeed
* @package HT\Modules\Console
*/
class RunSeed extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:db:seed
{module : The alias of the module}
{--class= : The specified seeder need to run}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Run database seeder from the specified module.';

/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* @var string
*/
protected $moduleName;

/**
* Create a new controller creator command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}

/**
* Execute the console command.
*/
public function handle()
{
try {
$this->moduleName = $this->getModuleName();
if (! $this->moduleExists()) {
$this->error('Module ' . $this->getModuleName() . ' does not exists!');

return;
}
$this->moduleSeed();
} catch (\Throwable $exception) {
$this->error($exception->getMessage());
exit();
}
}

/**
* @return string
*/
protected function getModuleName(): string
{
return trim($this->argument('module'));
}

/**
* @return bool
*/
protected function moduleExists(): bool
{
return $this->files->exists($this->modulePath());
}

/**
* @return string
*/
protected function modulePath(): string
{
return base_path(config('modules.directory') . '/' . $this->moduleName . '/');
}

/**
* module seed
*/
public function moduleSeed()
{
$result = [];
if ($option = $this->option('class')) {
$result[] = $option;
} else {
$seeders = $this->files->allFiles($this->modulePath() . '/database/seeds');
foreach ($seeders as $seeder) {
$result[] = Str::replaceArray('.php', [''], $seeder->getFilename());
}
}
if (count($result) > 0) {
array_walk($result, [$this, 'dbSeed']);
$this->info("Module [{$this->getModuleName()}] seeded.");
}
}

/**
* Seed the specified module.
*
* @param string $class
*/
protected function dbSeed($class)
{
$this->call('db:seed', ['--class' => $class]);
}
}
Loading

0 comments on commit 150e716

Please sign in to comment.