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 CreateObserverCommand |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Creates a file with Seed data |
12
|
|
|
* ----------------------------- |
13
|
|
|
* Создает файл с данными Seed |
14
|
|
|
*/ |
15
|
|
|
public function actionIndex() |
16
|
|
|
{ |
17
|
|
|
Cli::printer("Enter observer name: ", "magneta"); |
18
|
|
|
$prefix = str_replace(PHP_EOL, "", Cli::reader()); |
19
|
|
|
$className = ucfirst($prefix) . 'Observer'; |
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/Observers/"), "{$className}.php"], |
28
|
|
|
$this->createClass($className, $container) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
} else { |
32
|
|
|
$this->actionIndex(); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $className |
38
|
|
|
* @param string $container |
39
|
|
|
* @return string |
40
|
|
|
* |
41
|
|
|
* Creates class data |
42
|
|
|
* ------------------ |
43
|
|
|
* Создает данные класса |
44
|
|
|
*/ |
45
|
|
|
private function createClass(string $className, string $container) |
46
|
|
|
{ |
47
|
|
|
return <<<EOT |
48
|
|
|
<?php |
49
|
|
|
|
50
|
|
|
namespace App\Containers\\{$container}\Observers; |
51
|
|
|
|
52
|
|
|
use Rudra\EventDispatcher\ObserverInterface; |
53
|
|
|
|
54
|
|
|
class {$className} implements ObserverInterface |
55
|
|
|
{ |
56
|
|
|
public function onEvent() |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
EOT; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $path |
66
|
|
|
* @param $callable |
67
|
|
|
* |
68
|
|
|
* Writes data to a file |
69
|
|
|
* --------------------- |
70
|
|
|
* Записывает данные в файл |
71
|
|
|
*/ |
72
|
|
|
private function writeFile(array $path, string $data) |
73
|
|
|
{ |
74
|
|
|
if (!is_dir($path[0])) { |
75
|
|
|
mkdir($path[0], 0755, true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$fullPath = $path[0] . $path[1]; |
79
|
|
|
|
80
|
|
|
if (!file_exists($fullPath)) { |
81
|
|
|
Cli::printer("The file ", "light_green"); |
82
|
|
|
Cli::printer($fullPath, "light_green"); |
83
|
|
|
Cli::printer(" was created" . PHP_EOL, "light_green"); |
84
|
|
|
file_put_contents($fullPath, $data); |
85
|
|
|
} else { |
86
|
|
|
Cli::printer("The file $fullPath is already exists" . PHP_EOL, "light_yellow"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|