Passed
Pull Request — master (#70)
by Dmitriy
03:35 queued 01:04
created

ControllerCommand::getBaseClass()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\Controller;
6
7
use Yiisoft\Strings\Inflector;
8
use Yiisoft\Strings\StringHelper;
9
use Yiisoft\Validator\Rule\Each;
10
use Yiisoft\Validator\Rule\Regex;
11
use Yiisoft\Validator\Rule\Required;
12
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
13
use Yiisoft\Yii\Gii\Validator\NewClassRule;
14
use Yiisoft\Yii\Gii\Validator\TemplateRule;
15
16
final class ControllerCommand extends AbstractGeneratorCommand
17
{
18 3
    public function __construct(
19
        #[Required]
20
        #[Regex(
21
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
22
            message: 'Invalid namespace'
23
        )]
24
        private string $controllerNamespace = 'App\\Controller',
25
        #[Required]
26
        #[Regex(
27
            pattern: '/^[A-Z][a-zA-Z0-9]*Controller$/',
28
            message: 'Only word characters are allowed, and the class name must start with a capital letter and end with "Controller".'
29
        )]
30
        #[NewClassRule]
31
        /**
32
         * @var string the controller class name
33
         */
34
        private string $controllerClass = 'IndexController',
35
        /**
36
         * @var string the controller path
37
         */
38
        private string $controllerPath = '@src/Controller',
39
        /**
40
         * @var string the controller's views path
41
         */
42
        private string $viewsPath = '@views/',
43
        #[Regex(
44
            pattern: '/^[a-z\\\\]*$/i',
45
            message: 'Only word characters and backslashes are allowed.',
46
            skipOnEmpty: true,
47
        )]
48
        /**
49
         * @var string the base class of the controller or null if no parent class present
50
         */
51
        private string $baseClass = '',
52
        #[Each([
53
            new Regex(
54
                pattern: '/^[a-z][a-z0-9]*$/',
55
                message: 'Only a-z, 0-9, dashes (-), spaces and commas are allowed.'
56
            ),
57
        ])]
58
        /**
59
         * @var string[] list of action IDs
60
         */
61
        private array $actions = ['index'],
62
        #[Required(message: 'A code template must be selected.')]
63
        #[TemplateRule]
64
        protected string $template = 'default',
65
    ) {
66 3
        parent::__construct($template);
67 3
        sort($this->actions);
68
    }
69
70
    /**
71
     * @return string the controller ID
72
     */
73 1
    public function getControllerID(): string
74
    {
75 1
        $name = StringHelper::baseName($this->controllerClass);
76 1
        return (new Inflector())->pascalCaseToId(substr($name, 0, -10));
77
    }
78
79 1
    public function getControllerClass(): string
80
    {
81 1
        return $this->controllerClass;
82
    }
83
84 1
    public function getActions(): array
85
    {
86 1
        return $this->actions;
87
    }
88
89 1
    public function getViewsPath(): string
90
    {
91 1
        return $this->viewsPath;
92
    }
93
94 1
    public function getControllerNamespace(): string
95
    {
96 1
        return $this->controllerNamespace;
97
    }
98
99 1
    public function getBaseClass(): string
100
    {
101 1
        return $this->baseClass;
102
    }
103
104 1
    public function getDirectory(): string
105
    {
106 1
        return $this->controllerPath;
107
    }
108
109
    public static function getAttributeLabels(): array
110
    {
111
        return [
112
            'controllerNamespace' => 'Controller Namespace',
113
            'controllerClass' => 'Controller Class',
114
            'baseClass' => 'Base Class',
115
            'controllerPath' => 'Controller Path',
116
            'actions' => 'Action IDs',
117
            'viewsPath' => 'Views Path',
118
            'template' => 'Action IDs',
119
        ];
120
    }
121
122
    public static function getHints(): array
123
    {
124
        return [
125
            'controllerClass' => 'This is the name of the controller class to be generated. You should
126
                provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>),
127
                and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class
128
                is using the same namespace as specified by your application\'s controllerNamespace property.',
129
            'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces.
130
                Action IDs should be in lower case. For example:
131
                <ul>
132
                    <li><code>index</code> generates <code>index()</code></li>
133
                    <li><code>create-order</code> generates <code>createOrder()</code></li>
134
                </ul>',
135
            'viewsPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g.,
136
                <code>/var/www/app/controllers/views/order</code>, <code>@app/views/order</code>. If not set, it will default
137
                to <code>@app/views/ControllerID</code>',
138
            'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
139
        ];
140
    }
141
142
    public static function getAttributes(): array
143
    {
144
        return [
145
            'controllerNamespace',
146
            'controllerClass',
147
            'baseClass',
148
            'viewsPath',
149
            'actions',
150
            'template',
151
            'controllerPath',
152
        ];
153
    }
154
}
155