SplashCreateControllerService::createDirectory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace Mouf\Mvc\Splash\Services;
3
4
use Mouf\Composer\ClassNameMapper;
5
use Mouf\Mvc\Splash\Controllers\Controller;
6
use Mouf\Html\Template\TemplateInterface;
7
use Mouf\Html\HtmlElement\HtmlBlock;
8
use Mouf\Mvc\Splash\Utils\SplashException;
9
use Psr\Log\LoggerInterface;
10
use Mouf\MoufManager;
11
use Mouf\MoufCache;
12
13
/**
14
 * The service used to create controllers in Splash.
15
 */
16
class SplashCreateControllerService
17
{
18
    /**
19
     * Generates a controller, view, and sets the instance up.
20
     *
21
     * @param string $controllerName
22
     * @param string $instanceName
23
     * @param string $namespace
24
     * @param string $injectLogger
25
     * @param string $injectTemplate
26
     * @param string $injectDaoFactory
27
     * @param array  $actions
28
     */
29
    public function generate(MoufManager $moufManager, $controllerName, $instanceName, $namespace, $injectLogger = false,
30
            $injectTemplate = false, $injectDaoFactory = false, $actions = array())
31
    {
32
        $namespace = rtrim($namespace, '\\').'\\';
33
34
        $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
35
        $possibleFileNames = $classNameMapper->getPossibleFileNames($namespace.$controllerName);
36
        if (!isset($possibleFileNames[0])) {
37
            throw new SplashException("The class '".$namespace.$controllerName."' cannot be loaded using rules defined in composer autoload section");
38
        }
39
        $fileName = $possibleFileNames[0];
40
        $controllerPhpDirectory = dirname($fileName);
41
        $errors = array();
42
        if (!preg_match('/^[a-z_]\w*$/i', $controllerName)) {
43
            $errors['controllerNameError'] = 'This is not a valid PHP class name.';
44
        }
45
        if (!preg_match('/^[a-z_][\w\\\\]*$/i', $namespace)) {
46
            $errors['namespaceError'] = 'This is not a valid PHP namespace.';
47
        }
48
49
        $namespace = trim($namespace, '\\');
50
51
        if (!file_exists(ROOT_PATH.'../database.tdbm') && $injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $injectDaoFactory = false;
53
        }
54
55
        // Check that instance does not already exists
56
        if ($moufManager->has($instanceName)) {
57
            $errors['instanceError'] = 'This instance already exists.';
58
        }
59
60
        $injectTwig = false;
61
        $importJsonResponse = false;
62
        $importHtmlResponse = false;
63
        $importRedirectResponse = false;
64
65
        foreach ($actions as $key => $action) {
66
            // Check if the view file exists
67 View Code Duplication
            if ($injectTemplate && $action['view'] == 'twig') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                $injectTwig = true;
69
                $importHtmlResponse = true;
70
                $twigFile = ltrim($action['twigFile'], '/\\');
71
72
                $viewDirName = ROOT_PATH.'../../../'.dirname($twigFile);
73
                $result = $this->createDirectory($viewDirName);
74
                if (!$result) {
75
                    $errors['actions'][$key]['twigTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"';
76
                }
77
78
                if (file_exists(ROOT_PATH.'../../../'.$twigFile)) {
79
                    $errors['actions'][$key]['twigTemplateFileError'] = 'This file already exists.';
80
                }
81
            }
82 View Code Duplication
            if ($injectTemplate && $action['view'] == 'php') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
                $importHtmlResponse = true;
84
85
                $phpFile = ltrim($action['phpFile'], '/\\');
86
87
                $viewDirName = ROOT_PATH.'../../../'.dirname($phpFile);
88
                $result = $this->createDirectory($viewDirName);
89
                if (!$result) {
90
                    $errors['actions'][$key]['phpTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"';
91
                }
92
93
                if (file_exists(ROOT_PATH.'../../../'.$phpFile)) {
94
                    $errors['actions'][$key]['phpTemplateFileError'] = 'This file already exists.';
95
                }
96
            }
97
            if ($action['view'] == 'redirect') {
98
                if (!isset($action['redirect']) || empty($action['redirect'])) {
99
                    $errors['actions'][$key]['redirectError'] = 'Redirection URL cannot be empty.';
100
                }
101
                $importRedirectResponse = true;
102
            }
103
            if ($action['view'] == 'json') {
104
                $importJsonResponse = true;
105
            }
106
        }
107
108
        // TODO: check that URLs are not in error.
109
110
111
        if (!$errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112
            $result = $this->createDirectory(ROOT_PATH.'../../../'.$controllerPhpDirectory);
113
            if (!$result) {
114
                $errors['namespaceError'] = 'Unable to create directory: "'.$controllerPhpDirectory.'"';
115
            } elseif (file_exists(ROOT_PATH.'../../../'.$controllerPhpDirectory.$controllerName.'.php')) {
116
                $errors['namespaceError'] = 'The file "'.$controllerPhpDirectory.$controllerName.'.php already exists."';
117
            } elseif (!is_writable(ROOT_PATH.'../../../'.$controllerPhpDirectory)) {
118
                $errors['namespaceError'] = 'Unable to write file in directory: "'.$controllerPhpDirectory.'"';
119
            }
120
121
            if (!$errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
                ob_start();
123
                echo '<?php
124
';
125
                ?>
126
namespace <?= $namespace ?>;
127
128
use Mouf\Mvc\Splash\Annotations\Get;
129
use Mouf\Mvc\Splash\Annotations\Post;
130
use Mouf\Mvc\Splash\Annotations\Put;
131
use Mouf\Mvc\Splash\Annotations\Delete;
132
use Mouf\Mvc\Splash\Annotations\URL;
133
<?php if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
134
    ?>
135
use Mouf\Html\Template\TemplateInterface;
136
use Mouf\Html\HtmlElement\HtmlBlock;
137
<?php
138
139
}
140
                ?>
141
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142
    ?>
143
use Psr\Log\LoggerInterface;
144
<?php
145
146
}
147
                ?>
148
<?php if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
149
    ?>
150
use <?= $moufManager->getVariable('tdbmDefaultDaoNamespace').'\\Generated\\'.$moufManager->getVariable('tdbmDefaultDaoFactoryName') ?>;
151
<?php
152
153
}
154
                ?>
155
<?php if ($injectTwig) {
156
    ?>
157
use \Twig_Environment;
158
use Mouf\Html\Renderer\Twig\TwigTemplate;
159
<?php
160
161
}
162
                ?>
163
<?php if ($importJsonResponse) {
164
    ?>
165
use Zend\Diactoros\Response\JsonResponse;
166
<?php
167
168
}
169
                ?>
170
<?php if ($importRedirectResponse) {
171
    ?>
172
use Zend\Diactoros\Response\RedirectResponse;
173
<?php
174
175
}
176
                ?>
177
<?php if ($importHtmlResponse) {
178
    ?>
179
use Mouf\Mvc\Splash\HtmlResponse;
180
<?php
181
182
}
183
                ?>
184
185
/**
186
 * TODO: write controller comment
187
 */
188
class <?= $controllerName ?> {
189
190
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
191
    ?>
192
    /**
193
     * The logger used by this controller.
194
     * @var LoggerInterface
195
     */
196
    private $logger;
197
198
<?php
199
200
}
201
                ?>
202
<?php if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
203
    ?>
204
    /**
205
     * The template used by this controller.
206
     * @var TemplateInterface
207
     */
208
    private $template;
209
210
    /**
211
     * The main content block of the page.
212
     * @var HtmlBlock
213
     */
214
    private $content;
215
216
<?php
217
218
}
219
                ?>
220
<?php if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
221
    ?>
222
    /**
223
     * The DAO factory object.
224
     * @var DaoFactory
225
     */
226
    private $daoFactory;
227
228
<?php
229
230
}
231
                ?>
232
<?php if ($injectTwig) {
233
    ?>
234
    /**
235
     * The Twig environment (used to render Twig templates).
236
     * @var Twig_Environment
237
     */
238
    private $twig;
239
240
<?php
241
242
}
243
                ?>
244
245
    /**
246
     * Controller's constructor.
247
<?php
248
if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
249
    echo "     * @param LoggerInterface \$logger The logger\n";
250
}
251
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
252
                    echo "     * @param TemplateInterface \$template The template used by this controller\n";
253
                    echo "     * @param HtmlBlock \$content The main content block of the page\n";
254
                }
255
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
256
                    echo "     * @param DaoFactory \$daoFactory The object in charge of retrieving DAOs\n";
257
                }
258
                if ($injectTwig) {
259
                    echo "     * @param Twig_Environment \$twig The Twig environment (used to render Twig templates)\n";
260
                }
261
                ?>
262
     */
263
    public function __construct(<?php
264
$parameters = array();
265
                if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
266
                    $parameters[] = 'LoggerInterface $logger';
267
                }
268
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
269
                    $parameters[] = 'TemplateInterface $template';
270
                    $parameters[] = 'HtmlBlock $content';
271
                }
272
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
273
                    $parameters[] = 'DaoFactory $daoFactory';
274
                }
275
                if ($injectTwig) {
276
                    $parameters[] = 'Twig_Environment $twig';
277
                }
278
                echo implode(', ', $parameters);
279
                ?>) {
280
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
281
    ?>
282
        $this->logger = $logger;
283
<?php
284
285
}
286
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
287
                    ?>
288
        $this->template = $template;
289
        $this->content = $content;
290
<?php
291
292
                }
293
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
294
                    ?>
295
        $this->daoFactory = $daoFactory;
296
<?php
297
298
                }
299
                if ($injectTwig) {
300
                    ?>
301
        $this->twig = $twig;
302
<?php
303
304
                }
305
                ?>
306
    }
307
308
<?php foreach ($actions as $action):
309
    // First step, let's detect the {parameters} in the URL and add them if necessary
310
    // TODO
311
    // TODO
312
    // TODO
313
    // TODO
314
315
?>
316
    /**
317
     * @URL("<?= addslashes($action['url']) ?>")
318
319
<?php if ($action['anyMethod'] == 'false') {
320
    if ($action['getMethod'] == 'true') {
321
        echo "     * @Get\n";
322
    }
323
    if ($action['postMethod'] == 'true') {
324
        echo "     * @Post\n";
325
    }
326
    if ($action['putMethod'] == 'true') {
327
        echo "     * @Put\n";
328
    }
329
    if ($action['deleteMethod'] == 'true') {
330
        echo "     * @Delete\n";
331
    }
332
}
333
                if (isset($action['parameters'])) {
334
                    $parameters = $action['parameters'];
335
                    foreach ($parameters as $parameter) {
336
                        echo '     * @param '.$parameter['type'].' $'.$parameter['name']."\n";
337
                    }
338
                } else {
339
                    $parameters = array();
340
                }
341
                ?>
342
     */
343
    public function <?= $action['method'] ?>(<?php
344
$parametersCode = array();
345
                foreach ($parameters as $parameter) {
346
                    $parameterCode = '$'.$parameter['name'];
347
                    if ($parameter['optionnal'] == 'true') {
348
                        if ($parameter['type'] == 'int') {
349
                            $defaultValue = (int) $parameter['defaultValue'];
350
                        } elseif ($parameter['type'] == 'number') {
351
                            $defaultValue = (float) $parameter['defaultValue'];
352
                        } else {
353
                            $defaultValue = $parameter['defaultValue'];
354
                        }
355
                        $parameterCode .= ' = '.var_export($defaultValue, true);
356
                    }
357
                    $parametersCode[] = $parameterCode;
358
                }
359
                echo implode(', ', $parametersCode);
360
                ?>) {
361
        // TODO: write content of action here
362
363
<?php if ($injectTemplate && $action['view'] == 'twig'): ?>
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
364
        // Let's add the twig file to the template.
365
        $this->content->addHtmlElement(new TwigTemplate($this->twig, <?php var_export($action['twigFile']);
366
                ?>, array("message"=>"world")));
367
368
        return new HtmlResponse($this->template);
369
<?php elseif ($injectTemplate && $action['view'] == 'php'): ?>
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
370
        // Let's add the view to the content.
371
        // Note: $this is passed as the scope, so in the view file, you can refer to protected
372
        // and public variables and methods of this constructor using "$this".
373
        $this->content->addFile(ROOT_PATH.<?php var_export($action['phpFile']) ?>, $this);
374
375
        return new HtmlResponse($this->template);
376
<?php elseif ($action['view'] == 'json'): ?>
377
378
        return new JsonResponse([ "status"=>"ok" ]);
379
<?php elseif ($action['view'] == 'redirect'): ?>
380
381
        return new RedirectResponse(<?php var_export($action['redirect']);
382
                ?>);
383
<?php endif;
384
                ?>
385
    }
386
<?php endforeach;
387
                ?>
388
}
389
<?php
390
                $file = ob_get_clean();
391
392
                file_put_contents(ROOT_PATH.'../../../'.$fileName, $file);
393
                chmod(ROOT_PATH.'../../../'.$fileName, 0664);
394
395
                // Now, let's create the views files
396
                foreach ($actions as $action) {
397
                    if ($injectTemplate && $action['view'] == 'twig') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
398
                        $twigTemplateFile = $this->generateTwigView();
399
400
                        $twigFile = ltrim($action['twigFile'], '/\\');
401
402
                        file_put_contents(ROOT_PATH.'../../../'.$twigFile, $twigTemplateFile);
403
                        chmod(ROOT_PATH.'../../../'.$twigFile, 0664);
404
                    } elseif ($injectTemplate && $action['view'] == 'php') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
405
                        $phpTemplateFile = $this->generatePhpView($namespace.'\\'.$controllerName);
406
407
                        $phpFile = ltrim($action['phpFile'], '/\\');
408
409
                        file_put_contents(ROOT_PATH.'../../../'.$phpFile, $phpTemplateFile);
410
                        chmod(ROOT_PATH.'../../../'.$phpFile, 0664);
411
                    }
412
                }
413
414
                // Now, let's create the instance
415
                $controllerInstance = $moufManager->createInstance($namespace.'\\'.$controllerName);
416
                $controllerInstance->setName($instanceName);
417
                if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
418
                    if ($moufManager->has('psr.errorLogLogger')) {
419
                        $controllerInstance->getProperty('logger')->setValue($moufManager->getInstanceDescriptor('psr.errorLogLogger'));
420
                    }
421
                }
422
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
423
                    if ($moufManager->has('bootstrapTemplate')) {
424
                        $controllerInstance->getProperty('template')->setValue($moufManager->getInstanceDescriptor('bootstrapTemplate'));
425
                    }
426
                    if ($moufManager->has('block.content')) {
427
                        $controllerInstance->getProperty('content')->setValue($moufManager->getInstanceDescriptor('block.content'));
428
                    }
429
                }
430
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
431
                    if ($moufManager->has('daoFactory')) {
432
                        $controllerInstance->getProperty('daoFactory')->setValue($moufManager->getInstanceDescriptor('daoFactory'));
433
                    }
434
                }
435
                if ($injectTwig) {
436
                    if ($moufManager->has('twigEnvironment')) {
437
                        $controllerInstance->getProperty('twig')->setValue($moufManager->getInstanceDescriptor('twigEnvironment'));
438
                    }
439
                }
440
441
                $moufManager->rewriteMouf();
442
443
                // There is a new class, let's purge the cache
444
                $moufCache = new MoufCache();
445
                $moufCache->purgeAll();
446
447
                // TODO: purge cache
448
            }
449
        }
450
451
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
452
            $exception = new SplashCreateControllerServiceException('Errors detected');
453
            $exception->setErrors($errors);
454
            throw $exception;
455
        }
456
    }
457
458
    private function generateTwigView()
459
    {
460
        return '<p>Hello {{message}}</p>';
461
    }
462
463
    private function generatePhpView($controllerFQCN)
464
    {
465
        return '<?php /* @var $this '.$controllerFQCN.' */ ?>
466
This is your PHP view. You can access the controller protected and public variables / functions using the $this object';
467
    }
468
469
    /**
470
     * @param string $directory
471
     *
472
     * @return bool
473
     */
474
    private function createDirectory($directory)
475
    {
476
        if (!file_exists($directory)) {
477
            // Let's create the directory:
478
            $old = umask(0);
479
            $result = @mkdir($directory, 0775, true);
480
            umask($old);
481
482
            return $result;
483
        }
484
485
        return true;
486
    }
487
}
488