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 CreateContainerCommand 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 container name: ", "magneta"); |
||
19 | $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader())); |
||
20 | $className = $container . 'Controller'; |
||
21 | |||
22 | if (!empty($container)) { |
||
23 | |||
24 | if (is_dir(str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"))) { |
||
25 | Cli::printer("The container $container already exists" . PHP_EOL, "light_yellow"); |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | $this->writeFile( |
||
30 | [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"), "{$className}.php"], |
||
31 | $this->createContainersController($className, $container) |
||
32 | ); |
||
33 | |||
34 | $this->writeFile( |
||
35 | [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"), "routes.php"], |
||
36 | $this->createRoutes() |
||
37 | ); |
||
38 | |||
39 | $this->writeFile( |
||
40 | [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"), "config.php"], |
||
41 | $this->createConfig() |
||
42 | ); |
||
43 | |||
44 | $this->addConfig($container); |
||
45 | $this->createDirectories(str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/")); |
||
46 | Cli::printer("The container $container was created" . PHP_EOL, "light_green"); |
||
47 | |||
48 | } else { |
||
49 | $this->actionIndex(); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Creates class data |
||
55 | * ------------------ |
||
56 | * Создает данные класса |
||
57 | * |
||
58 | * @param string $className |
||
59 | * @param string $container |
||
60 | * @return string |
||
61 | */ |
||
62 | private function createContainersController(string $className, string $container): string |
||
0 ignored issues
–
show
|
|||
63 | { |
||
64 | return <<<EOT |
||
65 | <?php |
||
66 | |||
67 | namespace App\Containers\\{$container}; |
||
68 | |||
69 | use App\Ship\ShipController; |
||
70 | use Rudra\Container\Facades\Rudra; |
||
71 | use Rudra\View\ViewFacade as View; |
||
72 | use Rudra\Controller\ContainerControllerInterface; |
||
73 | |||
74 | class {$container}Controller extends ShipController implements ContainerControllerInterface |
||
75 | { |
||
76 | public function containerInit(): void |
||
77 | { |
||
78 | \$config = require_once "config.php"; |
||
79 | |||
80 | Rudra::binding()->set(\$config['contracts']); |
||
81 | Rudra::waiting()->set(\$config['services']); |
||
82 | |||
83 | View::setup(dirname(__DIR__) . '/', "$container/UI/tmpl", "$container/UI/cache"); |
||
84 | |||
85 | data([ |
||
86 | "title" => __CLASS__, |
||
87 | ]); |
||
88 | } |
||
89 | }\r\n |
||
90 | EOT; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Creates routes |
||
95 | * ------------------ |
||
96 | * Создает файл маршрутизатора |
||
97 | */ |
||
98 | private function createRoutes(): string |
||
99 | { |
||
100 | return <<<EOT |
||
101 | <?php |
||
102 | |||
103 | return [ |
||
104 | ];\r\n |
||
105 | EOT; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Creates config file |
||
110 | * ------------------- |
||
111 | * Создает файл конфигурации |
||
112 | */ |
||
113 | private function createConfig(): string |
||
114 | { |
||
115 | return <<<EOT |
||
116 | <?php |
||
117 | |||
118 | return [ |
||
119 | 'contracts' => [ |
||
120 | |||
121 | ], |
||
122 | 'services' => [ |
||
123 | |||
124 | ] |
||
125 | ];\r\n |
||
126 | EOT; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Create UI directories |
||
131 | * --------------------- |
||
132 | * Создает каталоги для UI |
||
133 | * |
||
134 | * @param string $path |
||
135 | * @return void |
||
136 | */ |
||
137 | private function createDirectories(string $path): void |
||
138 | { |
||
139 | if (!is_dir($path . 'UI')) { |
||
140 | mkdir($path . 'UI', 0755, true); |
||
141 | } |
||
142 | |||
143 | if (!is_dir($path . 'UI' . DIRECTORY_SEPARATOR . 'cache')) { |
||
144 | mkdir($path . 'UI' . DIRECTORY_SEPARATOR . 'cache', 0755, true); |
||
145 | } |
||
146 | |||
147 | if (!is_dir($path . 'UI' . DIRECTORY_SEPARATOR . 'tmpl')) { |
||
148 | mkdir($path . 'UI' . DIRECTORY_SEPARATOR . 'tmpl', 0755, true); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public function addConfig(string $container): void |
||
153 | { |
||
154 | $path = str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/config/setting.local.yml"); |
||
155 | $namespace = strtolower($container) . ": App\Containers\\{$container}\\"; |
||
156 | $contents = <<<EOT |
||
157 | \r\n $namespace |
||
158 | EOT; |
||
159 | file_put_contents($path, $contents, FILE_APPEND | LOCK_EX); |
||
160 | } |
||
161 | } |
||
162 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.