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