|
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; |
|
|
|
|
|
|
15
|
|
|
use BlitzPHP\Cli\Traits\GeneratorTrait; |
|
16
|
|
|
use BlitzPHP\Controllers\BaseController; |
|
17
|
|
|
use BlitzPHP\Controllers\ResourceController; |
|
|
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: