Passed
Push — main ( c04f85...7bc5d9 )
by Dimitri
03:09
created

Controller::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Generators;
13
14
use BlitzPHP\Cli\Console\Command;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BlitzPHP\Cli\Commands\Generators\Command. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use BlitzPHP\Cli\Traits\GeneratorTrait;
16
use BlitzPHP\Controllers\BaseController;
17
use BlitzPHP\Controllers\ResourceController;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Controllers\ResourceController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use BlitzPHP\Controllers\ResourcePresenter;
19
20
/**
21
 * Génère un fichier squelette de contrôleur.
22
 */
23
class Controller extends Command
24
{
25
    use GeneratorTrait;
26
27
    /**
28
     * @var string Groupe
29
     */
30
    protected $group = 'Generators';
31
32
    /**
33
     * @var string Nom
34
     */
35
    protected $name = 'make:controller';
36
37
    /**
38
     * @var string Description
39
     */
40
    protected $description = 'Génère un nouveau fichier de contrôleur.';
41
42
    /**
43
     * @var string
44
     */
45
    protected $service = 'Service de génération de code';
46
47
    /**
48
     * @var array Arguments de la commande
49
     */
50
    protected $arguments = [
51
        'name' => 'The controller class name.',
52
    ];
53
54
    /**
55
     * @var array Options de la commande
56
     */
57
    protected $options = [
58
        '--bare'      => 'S\'étend de BlitzPHP\Controllers\BaseController au lieu de AppController.',
59
        '--restful'   => 'S\'étend à partir d\'une ressource RESTful, Options : [controller, presenter]. Par défaut : "controller".',
60
        '--namespace' => ['Définissez l\'espace de noms racine. Par défaut : "APP_NAMESPACE".', APP_NAMESPACE],
61
        '--suffix'    => ['Ajoutez le titre du composant au nom de la classe (par exemple, User => UserController).', true],
62
        '--force'     => 'Forcer l\'écrasement du fichier existant.',
63
    ];
64
65
    /**
66
     * Exécutez réellement une commande.
67
     */
68
    public function execute(array $params)
69
    {
70
        $this->component = 'Controller';
71
        $this->directory = 'Controllers';
72
        $this->template  = 'controller.tpl.php';
73
74
        $this->classNameLang = 'CLI.generator.className.controller';
75
76
        $this->task('Creation du controleur')->eol();
77
78
        $this->runGeneration($params);
79
    }
80
81
    /**
82
     * Préparez les options et effectuez les remplacements nécessaires.
83
     */
84
    protected function prepare(string $class): string
85
    {
86
        $bare = $this->option('bare');
87
        $rest = $this->option('restful');
88
89
        $useStatement = trim(APP_NAMESPACE, '\\') . '\Controllers\AppController';
90
        $extends      = 'AppController';
91
92
        // Obtient la classe parent appropriée à étendre.
93
        if ($bare || $rest) {
94
            if ($bare) {
95
                $useStatement = BaseController::class;
96
                $extends      = 'BaseController';
97
            } elseif ($rest) {
98
                $rest = is_string($rest) ? $rest : 'controller';
99
100
                if (! in_array($rest, ['controller', 'presenter'], true)) {
101
                    // @codeCoverageIgnoreStart
102
                    $rest = $this->choice(lang('CLI.generator.parentClass'), ['controller', 'presenter']);
103
                    $this->newLine();
104
                    // @codeCoverageIgnoreEnd
105
                }
106
107
                if ($rest === 'controller') {
108
                    $useStatement = ResourceController::class;
109
                    $extends      = 'ResourceController';
110
                } elseif ($rest === 'presenter') {
111
                    $useStatement = ResourcePresenter::class;
112
                    $extends      = 'ResourcePresenter';
113
                }
114
            }
115
        }
116
117
        return $this->parseTemplate(
118
            $class,
119
            ['{useStatement}', '{extends}'],
120
            [$useStatement, $extends],
121
            ['type' => $rest]
122
        );
123
    }
124
}
125