Passed
Pull Request — master (#58)
by Dmitriy
02:43
created

ControllerCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 19
c 0
b 0
f 0
dl 0
loc 34
ccs 0
cts 18
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getGenerator() 0 3 1
A configure() 0 8 1
A createGeneratorCommand() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Command;
6
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Yiisoft\Yii\Gii\Generator\Controller\ControllerGenerator;
10
use Yiisoft\Yii\Gii\GeneratorCommandInterface;
11
use Yiisoft\Yii\Gii\GeneratorInterface;
12
13
/**
14
 * This is the command line version of Gii - a code generator.
15
 *
16
 * You can use this command to generate controllers, actions, etc. For example,
17
 * to generate a controller with some actions, you can run:
18
 *
19
 * ```
20
 * $ ./yii gii/controller OrderController --actions=index,view,edit
21
 * ```
22
 */
23
final class ControllerCommand extends BaseGenerateCommand
24
{
25
    protected static $defaultName = 'gii/controller';
26
27
    protected function configure(): void
28
    {
29
        $this->setDescription('Gii controller generator')
30
            ->addArgument('controllerClass', InputArgument::REQUIRED, 'Name of the generated controller')
31
            ->addOption('viewsPath', 'vp', InputArgument::OPTIONAL, 'Controller views path')
32
            ->addOption('baseClass', 'b', InputArgument::OPTIONAL, 'Controller base class')
33
            ->addOption('actions', 'a', InputArgument::OPTIONAL, 'Name of the controller actions');
34
        parent::configure();
35
    }
36
37
    public function getGenerator(): GeneratorInterface
38
    {
39
        return $this->gii->getGenerator(ControllerGenerator::getId());
40
    }
41
42
    protected function createGeneratorCommand(InputInterface $input): GeneratorCommandInterface
43
    {
44
        $actions = $input->getOption('actions');
45
        $actions = $actions !== null ? explode(',', $actions) : ['index'];
46
47
        $template = $input->getOption('template');
48
        $template = $template !== null ? $template : 'default';
49
50
        // TODO: sync params
51
        return new \Yiisoft\Yii\Gii\Generator\Controller\ControllerCommand(
52
            controllerClass: $input->getArgument('controllerClass'),
53
            viewsPath: (string)$input->getOption('viewsPath'),
54
            baseClass: (string)$input->getOption('baseClass'),
55
            actions: $actions,
56
            template: $template,
57
        );
58
    }
59
}
60