-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControllerGenerator.php
52 lines (44 loc) · 1.88 KB
/
ControllerGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* @author Mohammed Moussaoui
* @license MIT license. For more license information, see the LICENSE file in the root directory.
* @link https://github.com/DevNet-Framework
*/
namespace DevNet\Core\Tools;
use DevNet\CLI\Plugin\CodeModel;
use DevNet\CLI\Plugin\ICodeGenerator;
use DevNet\System\Text\StringBuilder;
class ControllerGenerator implements ICodeGenerator
{
private StringBuilder $content;
public function __construct()
{
$this->content = new StringBuilder();
}
public function generate(array $parameters): array
{
$name = $parameters['--name'] ?? 'MyController';
$output = $parameters['--output'] ?? 'Controllers';
$namespace = $parameters['--prefix'] ?? 'Application';
$namespace = $namespace .'\\' . str_replace('/', '\\', $output);
$namespace = trim($namespace, '\\');
$this->content->clear();
$this->content->appendLine('<?php');
$this->content->appendLine();
$this->content->appendLine("namespace {$namespace};");
$this->content->appendLine();
$this->content->appendLine('use DevNet\Core\Endpoint\Controller;');
$this->content->appendLine('use DevNet\Core\Endpoint\IActionResult;');
$this->content->appendLine('use DevNet\Core\Endpoint\Route;');
$this->content->appendLine();
$this->content->appendLine("class {$name} extends Controller");
$this->content->appendLine('{');
$this->content->appendLine(" #[Route('/')]");
$this->content->appendLine(' public function index(): IActionResult');
$this->content->appendLine(' {');
$this->content->appendLine(' return $this->content("Hello World!");');
$this->content->appendLine(' }');
$this->content->appendLine('}');
return [new CodeModel($name . '.php', $this->content, $output)];
}
}