1 | <?php |
||
2 | |||
3 | namespace Apps\Console; |
||
4 | |||
5 | |||
6 | use Ffcms\Console\Command; |
||
7 | use Ffcms\Core\Helper\FileSystem\File; |
||
8 | use Ffcms\Core\Helper\Type\Arr; |
||
9 | use Ffcms\Core\Helper\Type\Str; |
||
10 | use Symfony\Component\Console\Input\InputInterface; |
||
11 | use Symfony\Component\Console\Input\InputOption; |
||
12 | use Symfony\Component\Console\Output\OutputInterface; |
||
13 | |||
14 | /** |
||
15 | * Class GeneratorModelCommand. Add new model based on carcase template |
||
16 | * @package Apps\Console |
||
17 | */ |
||
18 | class GeneratorModelCommand extends Command |
||
19 | { |
||
20 | /** |
||
21 | * Register command and options |
||
22 | */ |
||
23 | public function configure() |
||
24 | { |
||
25 | $this->setName('generator:model') |
||
26 | ->setDescription('Generate default model template') |
||
27 | ->addOption('loader', 'loader', InputOption::VALUE_OPTIONAL, 'Set which loader will be used to create model. Example: front, admin, install') |
||
28 | ->addOption('controller', 'controller', InputOption::VALUE_OPTIONAL, 'Set related controller for this model. This value will be used as sub-directory for model') |
||
29 | ->addOption('name', 'name', InputOption::VALUE_OPTIONAL, 'Set model name. Example: FormUserCreate, EntityContentShow'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Generate model from template carcase |
||
34 | * @param InputInterface $input |
||
35 | * @param OutputInterface $output |
||
36 | * @throws \Exception |
||
37 | * @return void |
||
38 | */ |
||
39 | public function execute(InputInterface $input, OutputInterface $output) |
||
40 | { |
||
41 | // get options from input |
||
42 | $loader = $this->optionOrAsk('loader', 'Loader name(front|admin|api)', 'front'); |
||
43 | $controller = $this->optionOrAsk('controller', 'Related controller'); |
||
44 | $name = $this->optionOrAsk('name', 'Controller name'); |
||
45 | |||
46 | $loader = ucfirst(Str::lowerCase($loader)); |
||
47 | $controller = ucfirst(Str::lowerCase($controller)); |
||
48 | $name = ucfirst(Str::lowerCase($name)); |
||
49 | |||
50 | // check loader definition |
||
51 | if (!Arr::in($loader, ['Front', 'Admin', 'Api'])) { |
||
52 | throw new \Exception('Wrong definition for loader. You shoud use front, admin, api'); |
||
53 | } |
||
54 | |||
55 | $namespace = 'Apps\Model\\' . $loader . '\\' . $controller; |
||
56 | $tpl = File::read('/Private/Carcase/Model.tphp'); |
||
57 | $code = Str::replace(['%namespace%', '%name%'], [$namespace, $name], $tpl); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
58 | $path = '/Apps/Model/' . $loader . '/' . $controller . '/' . $name . '.php'; |
||
59 | File::write($path, $code); |
||
60 | $output->writeln('Model are successful created: ' . $path); |
||
61 | } |
||
62 | |||
63 | } |