1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Ship\Command; |
4
|
|
|
|
5
|
|
|
use App\Ship\Utils\FileCreator; |
6
|
|
|
use Rudra\Container\Facades\Rudra; |
7
|
|
|
use Rudra\Cli\ConsoleFacade as Cli; |
8
|
|
|
|
9
|
|
|
class CreateControllerCommand extends FileCreator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Creates a file with Seed data |
13
|
|
|
* ----------------------------- |
14
|
|
|
* Создает файл с данными Seed |
15
|
|
|
*/ |
16
|
|
|
public function actionIndex(): void |
17
|
|
|
{ |
18
|
|
|
Cli::printer("Enter controller name: ", "magneta"); |
19
|
|
|
$controllerPrefix = ucfirst(str_replace(PHP_EOL, "", Cli::reader())); |
20
|
|
|
|
21
|
|
|
Cli::printer("Enter container: ", "magneta"); |
22
|
|
|
$container = ucfirst(str_replace(PHP_EOL, "", Cli::reader())); |
23
|
|
|
|
24
|
|
|
if (!empty($container)) { |
25
|
|
|
|
26
|
|
|
$this->writeFile( |
27
|
|
|
[str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Controller/"), "{$controllerPrefix}Controller.php"], |
28
|
|
|
$this->createClass($controllerPrefix, $container) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->addRoute($container, $controllerPrefix); |
32
|
|
|
|
33
|
|
|
} else { |
34
|
|
|
$this->actionIndex(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates class data |
40
|
|
|
* ------------------ |
41
|
|
|
* Создает данные класса |
42
|
|
|
* |
43
|
|
|
* @param string $controllerPrefix |
44
|
|
|
* @param string $container |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
private function createClass(string $controllerPrefix, string $container): string |
48
|
|
|
{ |
49
|
|
|
$url = strtolower("$container/$controllerPrefix"); |
50
|
|
|
|
51
|
|
|
if (Rudra::config()->get("attributes")) { |
52
|
|
|
return <<<EOT |
53
|
|
|
<?php |
54
|
|
|
|
55
|
|
|
namespace App\Containers\\{$container}\Controller; |
56
|
|
|
|
57
|
|
|
use App\Containers\\{$container}\\{$container}Controller; |
58
|
|
|
|
59
|
|
|
class {$controllerPrefix}Controller extends {$container}Controller |
60
|
|
|
{ |
61
|
|
|
#[Routing(url: '{$url}', method: 'GET')] |
62
|
|
|
public function actionIndex(): void |
63
|
|
|
{ |
64
|
|
|
dd(__CLASS__); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
EOT; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return <<<EOT |
72
|
|
|
<?php |
73
|
|
|
|
74
|
|
|
namespace App\Containers\\{$container}\Controller; |
75
|
|
|
|
76
|
|
|
use App\Containers\\{$container}\\{$container}Controller; |
77
|
|
|
|
78
|
|
|
class {$controllerPrefix}Controller extends {$container}Controller |
79
|
|
|
{ |
80
|
|
|
/** |
81
|
|
|
* @Routing(url = '{$url}', method = 'GET') |
82
|
|
|
*/ |
83
|
|
|
public function actionIndex(): void |
84
|
|
|
{ |
85
|
|
|
dd(__CLASS__); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
EOT; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $container |
94
|
|
|
* @param string $controllerPrefix |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function addRoute(string $container, string $controllerPrefix): void |
98
|
|
|
{ |
99
|
|
|
$path = str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/routes.php"); |
100
|
|
|
$routes = require_once $path; |
101
|
|
|
$namespace = "\App\Containers\\{$container}\\Controller\\{$controllerPrefix}Controller"; |
102
|
|
|
|
103
|
|
|
if (!in_array($namespace, $routes)) { |
104
|
|
|
$contents = file_get_contents($path); |
105
|
|
|
$contents = str_replace("];", '', $contents); |
106
|
|
|
file_put_contents($path, $contents); |
107
|
|
|
$contents = <<<EOT |
108
|
|
|
\t$namespace::class, |
109
|
|
|
]; |
110
|
|
|
EOT; |
111
|
|
|
file_put_contents($path, $contents, FILE_APPEND | LOCK_EX); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|