forked from doppar/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeWatcherCommand.php
More file actions
89 lines (74 loc) · 2.34 KB
/
MakeWatcherCommand.php
File metadata and controls
89 lines (74 loc) · 2.34 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Phaseolies\Console\Commands;
use Phaseolies\Console\Schedule\Command;
class MakeWatcherCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:watcher {name}';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Create a new model property watcher listener class';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
return $this->executeWithTiming(function () {
$name = $this->argument('name');
$parts = explode('/', $name);
$className = array_pop($parts);
$namespace = 'App\\Watchers' . (count($parts) > 0 ? '\\' . implode('\\', $parts) : '');
$filePath = base_path('app/Watchers/' . str_replace('/', DIRECTORY_SEPARATOR, $name) . '.php');
if (file_exists($filePath)) {
$this->displayError('Watcher already exists at:');
$this->line('<fg=white>' . str_replace(base_path(), '', $filePath) . '</>');
return Command::FAILURE;
}
$directoryPath = dirname($filePath);
if (!is_dir($directoryPath)) {
mkdir($directoryPath, 0755, true);
}
file_put_contents($filePath, $this->generateWatcherContent($namespace, $className));
$this->displaySuccess('Watcher created successfully');
$this->line('<fg=yellow>👁️ File:</> <fg=white>' . str_replace(base_path('/'), '', $filePath) . '</>');
$this->newLine();
$this->line('<fg=yellow>📌 Class:</> <fg=white>' . $className . '</>');
return Command::SUCCESS;
});
}
/**
* Generate watcher listener class content.
*/
protected function generateWatcherContent(string $namespace, string $className): string
{
return <<<EOT
<?php
namespace {$namespace};
use Phaseolies\Database\Entity\Model;
class {$className}
{
/**
* Handle the watched property change.
*
* @param mixed \$old
* @param mixed \$new
* @param Model \$model
* @return void
*/
public function handle(mixed \$old, mixed \$new, Model \$model): void
{
//
}
}
EOT;
}
}