Completed
Push — master ( f0680b...bff1a4 )
by Filipe
12:26
created

CreateController::setTemplateEngine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Console\Command\Task;
11
12
use Slick\Common\Base;
13
use Slick\Mvc\Console\Command\TaskInterface;
14
use Slick\Mvc\Console\MetaDataGenerator\Composer;
15
use Slick\Mvc\Console\MetaDataGenerator\ConsoleAwareMethods;
16
use Slick\Mvc\Console\MetaDataGenerator\Controller;
17
use Slick\Template\Template;
18
use Slick\Template\TemplateEngineInterface;
19
use Symfony\Component\Console\Helper\QuestionHelper;
20
use Symfony\Component\Console\Question\ConfirmationQuestion;
21
22
/**
23
 * Class CreateController
24
 *
25
 * @package Slick\Mvc\Console\Command\Task
26
 * @author  Filipe Silva <[email protected]>
27
 *
28
 * @property string $controllerName
29
 * @property string $namespace
30
 * @property string $sourcePath
31
 * @property string $basePath
32
 */
33
class CreateController extends Base implements TaskInterface
34
{
35
    /**
36
     * @readwrite
37
     * @var string
38
     */
39
    protected $entityName;
40
41
    /**
42
     * @readwrite
43
     * @var string
44
     */
45
    protected $controllerName;
46
47
    /**
48
     * @readwrite
49
     * @var string
50
     */
51
    protected $namespace;
52
53
    /**
54
     * @readwrite
55
     * @var string
56
     */
57
    protected $sourcePath;
58
59
    /**
60
     * @var string
61
     */
62
    protected $basePath;
63
64
    /**
65
     * @var string
66
     */
67
    protected $controllerFile;
68
69
    /**
70
     * For input/output getters and setters
71
     */
72
    use ConsoleAwareMethods;
73
74
    /**
75
     * @var Controller
76
     */
77
    protected $controllerMetaData;
78
79
    /**
80
     * @var TemplateEngineInterface
81
     */
82
    protected $templateEngine;
83
84
    /**
85
     * @var QuestionHelper
86
     */
87
    protected $questionHelper;
88
89
    protected $template = 'controller.twig';
90
91
    /**
92
     * Runs this task
93
     *
94
     * @return boolean
95
     */
96
    public function run()
97
    {
98
        if (!$this->overrideFile()) {
99
            $this->output->writeln("<info>File was skipped. The controller was not created.</info>");
100
            return true;
101
        }
102
        $data = $this->getControllerMetaData()->getData();
103
        $data = array_merge(
104
            $data,
105
            [
106
                'controllerName' => $this->controllerName,
107
                'namespace' => $this->namespace,
108
                'basePath' => $this->getBasePath()
109
            ]
110
        );
111
        $content = $this->getTemplateEngine()
112
            ->parse($this->template)
113
            ->process($data);
114
        file_put_contents($this->getControllerFile(), $content);
115
        return true;
116
    }
117
118
    /**
119
     * Gets controllerMetaData property
120
     *
121
     * @return Controller
122
     */
123
    public function getControllerMetaData()
124
    {
125
        if (null == $this->controllerMetaData) {
126
            $controller = new Controller();
127
            $this->configureController($controller);
128
            $this->setControllerMetaData($controller);
129
        }
130
        return $this->controllerMetaData;
131
    }
132
133
    /**
134
     * Sets controllerMetaData property
135
     *
136
     * @param Controller $controllerMetaData
137
     *
138
     * @return CreateController
139
     */
140
    public function setControllerMetaData(Controller $controllerMetaData)
141
    {
142
        $this->controllerMetaData = $controllerMetaData;
143
        return $this;
144
    }
145
146
    /**
147
     * Gets templateEngine property
148
     *
149
     * @return TemplateEngineInterface
150
     */
151
    public function getTemplateEngine()
152
    {
153
        if (null == $this->templateEngine) {
154
            Template::addPath(dirname(dirname(__DIR__)).'/templates');
155
            $template = (new Template())->initialize();
156
            $this->setTemplateEngine($template);
157
        }
158
        return $this->templateEngine;
159
    }
160
161
    /**
162
     * Sets templateEngine property
163
     *
164
     * @param TemplateEngineInterface $templateEngine
165
     *
166
     * @return CreateController
167
     */
168
    public function setTemplateEngine(TemplateEngineInterface $templateEngine)
169
    {
170
        $this->templateEngine = $templateEngine;
171
        return $this;
172
    }
173
174
    /**
175
     * Gets basePath property
176
     *
177
     * @return string
178
     */
179
    public function getBasePath()
180
    {
181
        if (null == $this->basePath) {
182
            $this->basePath = strtolower($this->controllerName);
183
        }
184
        return $this->basePath;
185
    }
186
187
    /**
188
     * Gets controllerFile property
189
     *
190
     * @return string
191
     */
192
    public function getControllerFile()
193
    {
194
        if (null == $this->controllerFile) {
195
            $this->setControllerFile(
196
                "{$this->sourcePath}/{$this->namespace}/" .
197
                "{$this->controllerName}.php"
198
            );
199
        }
200
        return $this->controllerFile;
201
    }
202
203
    /**
204
     * Sets controllerFile property
205
     *
206
     * @param string $controllerFile
207
     *
208
     * @return CreateController
209
     */
210
    public function setControllerFile($controllerFile)
211
    {
212
        $this->controllerFile = $controllerFile;
213
        return $this;
214
    }
215
216
    /**
217
     * Gets questionHelper property
218
     *
219
     * @return QuestionHelper
220
     */
221
    public function getQuestionHelper()
222
    {
223
        if (null == $this->questionHelper) {
224
            $this->setQuestionHelper($this->getCommand()->getHelper('question'));
0 ignored issues
show
Compatibility introduced by
$this->getCommand()->getHelper('question') of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Symfony\Component...\Helper\QuestionHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
225
        }
226
        return $this->questionHelper;
227
    }
228
229
    /**
230
     * Sets questionHelper property
231
     *
232
     * @param QuestionHelper $questionHelper
233
     *
234
     * @return CreateController
235
     */
236
    public function setQuestionHelper(QuestionHelper $questionHelper)
237
    {
238
        $this->questionHelper = $questionHelper;
239
        return $this;
240
    }
241
242
    /**
243
     * Configures the controller generator
244
     *
245
     * @param Controller $controller
246
     */
247
    protected function configureController(Controller $controller)
248
    {
249
        $controller->setInput($this->getInput())
250
            ->setOutput($this->getOutput())
251
            ->setCommand($this->getCommand());
252
        $composer = new Composer();
253
        $controller->add($composer);
254
    }
255
256
    /**
257
     * Check controller file existence and ask if can be overridden
258
     *
259
     * @return bool
260
     */
261
    protected function overrideFile()
262
    {
263
        if (!is_file($this->getControllerFile())) {
264
            return true;
265
        }
266
267
        $question = new ConfirmationQuestion(
268
            "\nThe file <comment>{$this->controllerFile}</comment> already " .
269
            "exists. Override it (y,N)? ",
270
            false,
271
            '/^(y|yes)/i'
272
        );
273
        return (boolean) $this->getQuestionHelper()->ask(
274
            $this->input,
275
            $this->output,
276
            $question
277
        );
278
    }
279
}