Test Failed
Pull Request — master (#58)
by Dmitriy
02:36
created

Generator::getControllerNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
c 0
b 0
f 0
cc 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\Controller;
6
7
use InvalidArgumentException;
8
use Yiisoft\Yii\Gii\CodeFile;
9
use Yiisoft\Yii\Gii\Generator\AbstractGenerator;
10
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
11
12
/**
13
 * This generator will generate a controller and one or a few action view files.
14
 */
15
final class Generator extends AbstractGenerator
16
{
17
    public function getName(): string
18
    {
19
        return 'Controller Generator';
20
    }
21
22
    public function getDescription(): string
23
    {
24
        return 'This generator helps you to quickly generate a new controller class with
25
            one or several controller actions and their corresponding views.';
26
    }
27
28
    public function attributeLabels(): array
29
    {
30
        return [
31
            'baseClass' => 'Base Class',
32
            'controllerClass' => 'Controller Class',
33
            'viewsPath' => 'Views Path',
34
            'actions' => 'Action IDs',
35
        ];
36
    }
37
38
    public function requiredTemplates(): array
39
    {
40
        return [
41
            'controller.php',
42
            'view.php',
43
        ];
44
    }
45
46
    public function stickyAttributes(): array
47
    {
48
        return ['baseClass'];
49
    }
50
51
    public function hints(): array
52
    {
53
        return [
54 3
            'controllerClass' => 'This is the name of the controller class to be generated. You should
55
                provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>),
56 3
                and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class
57 3
                is using the same namespace as specified by your application\'s controllerNamespace property.',
58
            'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces.
59
                Action IDs should be in lower case. For example:
60 3
                <ul>
61 3
                    <li><code>index</code> generates <code>index()</code></li>
62
                    <li><code>create-order</code> generates <code>createOrder()</code></li>
63
                </ul>',
64
            'viewsPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g.,
65 3
                <code>/var/www/app/controllers/views/order</code>, <code>@app/views/order</code>. If not set, it will default
66
                to <code>@app/views/ControllerID</code>',
67
            'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
68 3
        ];
69
    }
70
71
    public function successMessage(): string
72
    {
73
        return 'The controller has been generated successfully.';
74
    }
75 3
76
    public function generate(AbstractGeneratorCommand $command): array
77
    {
78
        if (!$command instanceof ControllerCommand) {
79
            throw new InvalidArgumentException();
80
        }
81
82
        $files = [];
83
84
        $files[] = (new CodeFile(
85
            $this->getControllerFile($command),
86
            $this->render($command, 'controller')
87
        ))->withBasePath($this->aliases->get('@root'));
88
89
        foreach ($command->getActions() as $action) {
90
            $files[] = (new CodeFile(
91
                $this->getViewFile($command, $action),
92
                $this->render($command, 'view', ['action' => $action])
93
            ))->withBasePath($this->aliases->get('@root'));
94 1
        }
95
96
        return $files;
97 1
    }
98
99
    /**
100
     * Normalizes [[actions]] into an array of action IDs.
101
     *
102
     * @return array an array of action IDs entered by the user
103
     */
104
    public function getActionIDs(ControllerCommand $command): array
105
    {
106
        return $command->getActions();
107
    }
108
109
    /**
110
     * @return string the controller class file path
111
     */
112
    private function getControllerFile(ControllerCommand $command): string
113
    {
114
        return $this->aliases->get(
115
            sprintf('%s/%s.php', $this->getDirectory() ?? '', $command->getControllerClass())
116
        );
117
    }
118
119
    /**
120
     * @param string $action the action ID
121
     *
122
     * @return string the action view file path
123
     */
124
    public function getViewFile(ControllerCommand $command, string $action): string
125
    {
126
        if (empty($command->getViewsPath())) {
127
            return $this->aliases->get(
128
                '@views/' . $command->getControllerID() . "/$action.php"
129
            );
130
        }
131
132 2
        return $this->aliases->get(str_replace('\\', '/', $command->getViewsPath()) . "/$action.php");
133
    }
134
}
135