2023-06-11 12:14:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FleetCart\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use FleetCart\Scaffold\Module\ModuleScaffold;
|
2023-12-03 14:07:47 +00:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
class ScaffoldModuleCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The console command name.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name = 'scaffold:module';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Scaffold a new module';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The instance of ModuleScaffold.
|
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @var ModuleScaffold
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
private ModuleScaffold $scaffolder;
|
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
2023-12-03 14:07:47 +00:00
|
|
|
* @param ModuleScaffold $scaffolder
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
|
|
|
public function __construct(ModuleScaffold $scaffolder)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->scaffolder = $scaffolder;
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return void
|
2023-12-03 14:07:47 +00:00
|
|
|
* @throws FileNotFoundException
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
public function handle(): void
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
|
|
|
$module = $this->askModuleName();
|
|
|
|
$entities = $this->askEntities();
|
|
|
|
|
|
|
|
$this->scaffolder->scaffold($module, $entities);
|
|
|
|
|
|
|
|
$this->info('Module has been generated.');
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Ask for module name.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
private function askModuleName(): array
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
|
|
|
do {
|
2023-12-03 14:07:47 +00:00
|
|
|
do {
|
|
|
|
$moduleName = $this->ask('Please enter the module name in the following format: vendor/name');
|
|
|
|
} while (!$this->moduleNameIsValid($moduleName));
|
2023-06-11 12:14:03 +00:00
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
[$vendor, $name] = $this->extractModuleName($moduleName);
|
2023-06-11 12:14:03 +00:00
|
|
|
} while ($this->moduleExists($name));
|
|
|
|
|
|
|
|
return compact('vendor', 'name');
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
2023-12-03 14:07:47 +00:00
|
|
|
* Validate the given module name.
|
2023-06-11 12:14:03 +00:00
|
|
|
*
|
|
|
|
* @param string $moduleName
|
2023-12-03 14:07:47 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2023-06-11 12:14:03 +00:00
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
private function moduleNameIsValid(string $moduleName): bool
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
2023-12-03 14:07:47 +00:00
|
|
|
$name = explode('/', $moduleName);
|
2023-06-11 12:14:03 +00:00
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
if (count($name) !== 2) {
|
|
|
|
$this->error('Module name must be in the following format: vendor/name');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-11 12:14:03 +00:00
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the given module name.
|
|
|
|
*
|
|
|
|
* @param string $moduleName
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function extractModuleName(string $moduleName): array
|
|
|
|
{
|
|
|
|
$name = explode('/', $moduleName);
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
return [$name[0], ucfirst(camel_case($name[1]))];
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Determine the given module is exists.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
private function moduleExists($name): bool
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
|
|
|
if (is_dir(config('modules.paths.modules') . "/{$name}")) {
|
|
|
|
$this->error("The module [$name] is already exists.");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-03 14:07:47 +00:00
|
|
|
|
2023-06-11 12:14:03 +00:00
|
|
|
/**
|
|
|
|
* Ask for entities.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-12-03 14:07:47 +00:00
|
|
|
private function askEntities(): array
|
2023-06-11 12:14:03 +00:00
|
|
|
{
|
|
|
|
$entities = [];
|
|
|
|
|
|
|
|
do {
|
2023-12-03 14:07:47 +00:00
|
|
|
$entity = $this->ask('Enter entity name. To Continue Press Enter .', false);
|
2023-06-11 12:14:03 +00:00
|
|
|
|
|
|
|
if ($entity !== '') {
|
|
|
|
$entities[] = ucfirst($entity);
|
|
|
|
}
|
|
|
|
} while ($entity !== '');
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
}
|