|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Console\Command\Abstracts\HopliteCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class MakeHookCommand extends HopliteCommand |
|
10
|
|
|
{ |
|
11
|
|
|
protected const STUB_NAMESPACE = 'Leonidas\\Console\\Stubs\\Hook'; |
|
12
|
|
|
|
|
13
|
|
|
protected static $defaultName = 'make:hook'; |
|
14
|
|
|
|
|
15
|
|
|
protected static $defaultDescription = 'Creates a hook helper class'; |
|
16
|
|
|
|
|
17
|
|
|
protected function configure(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this |
|
20
|
|
|
->addArgument('tag', InputArgument::REQUIRED) |
|
21
|
|
|
->addArgument('type', InputArgument::OPTIONAL, 'The type of hook to create', 'action') |
|
22
|
|
|
->addOption('path', 'p', InputOption::VALUE_OPTIONAL); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function handle(): int |
|
26
|
|
|
{ |
|
27
|
|
|
$tag = $this->input->getArgument('tag'); |
|
28
|
|
|
$type = $this->input->getArgument('type'); |
|
29
|
|
|
$converted = $this->convert($tag)->toPascal(); |
|
30
|
|
|
|
|
31
|
|
|
$parts = explode('/', $this->config('make.hook.path')); |
|
|
|
|
|
|
32
|
|
|
$root = array_shift($parts); |
|
33
|
|
|
$namespace = implode('\\', $parts); |
|
34
|
|
|
$dir = $root . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array_slice($parts, 1)); |
|
35
|
|
|
$filename = 'Targets' . $converted . 'Hook.php'; |
|
36
|
|
|
$file = getcwd() . '/' . $dir . '/' . $filename; |
|
37
|
|
|
|
|
38
|
|
|
$template = $this->getTemplate($type); |
|
39
|
|
|
|
|
40
|
|
|
$replacements = [ |
|
41
|
|
|
static::STUB_NAMESPACE => $namespace, |
|
42
|
|
|
'Dummy' . ucfirst($type) => $converted, |
|
43
|
|
|
'dummy_' . strtolower($type) => $tag, |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($replacements as $key => $value) { |
|
47
|
|
|
$template = str_replace($key, $value, $template); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!file_exists($file)) { |
|
51
|
|
|
touch($file); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
file_put_contents($file, $template); |
|
55
|
|
|
|
|
56
|
|
|
return self::SUCCESS; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function getTemplate(string $type): string |
|
60
|
|
|
{ |
|
61
|
|
|
return file_get_contents($this->getTemplateFile($type)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getTemplateFile(string $type): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->internal('/Stubs/Hook/TargetsDummy' . ucfirst($type) . 'Hook.php'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|