1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Toolbelt; |
6
|
|
|
|
7
|
|
|
use FondBot\Application\Kernel; |
8
|
|
|
use League\Flysystem\Filesystem; |
9
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
13
|
|
|
|
14
|
|
|
abstract class Command extends SymfonyCommand |
15
|
|
|
{ |
16
|
|
|
protected $kernel; |
17
|
|
|
|
18
|
|
|
/** @var InputInterface */ |
19
|
|
|
protected $input; |
20
|
|
|
|
21
|
|
|
/** @var SymfonyStyle */ |
22
|
|
|
protected $output; |
23
|
|
|
|
24
|
1 |
|
public function __construct(Kernel $kernel) |
25
|
|
|
{ |
26
|
1 |
|
parent::__construct(); |
27
|
|
|
|
28
|
1 |
|
$this->kernel = $kernel; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
32
|
|
|
{ |
33
|
|
|
$this->input = $input; |
34
|
|
|
$this->output = new SymfonyStyle($input, $output); |
35
|
|
|
|
36
|
|
|
$this->handle(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get argument value. |
41
|
|
|
* |
42
|
|
|
* @param string $name |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
* |
46
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
protected function getArgument(string $name) |
49
|
|
|
{ |
50
|
|
|
return $this->input->getArgument($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Write a message. |
55
|
|
|
* |
56
|
|
|
* @param string $message |
57
|
|
|
*/ |
58
|
|
|
protected function line(string $message): void |
59
|
|
|
{ |
60
|
|
|
$this->output->writeln($message); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Display success message. |
65
|
|
|
* |
66
|
|
|
* @param string $message |
67
|
|
|
*/ |
68
|
|
|
protected function success(string $message): void |
69
|
|
|
{ |
70
|
|
|
$this->output->success($message); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Display info message. |
75
|
|
|
* |
76
|
|
|
* @param string $message |
77
|
|
|
*/ |
78
|
|
|
protected function info(string $message): void |
79
|
|
|
{ |
80
|
|
|
$this->output->writeln('<info>'.$message.'</info>'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Display warning message. |
85
|
|
|
* |
86
|
|
|
* @param string $message |
87
|
|
|
*/ |
88
|
|
|
protected function warning(string $message): void |
89
|
|
|
{ |
90
|
|
|
$this->output->writeln('<warning>'.$message.'</warning>'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Display error message. |
95
|
|
|
* |
96
|
|
|
* @param string $message |
97
|
|
|
*/ |
98
|
|
|
protected function error(string $message): void |
99
|
|
|
{ |
100
|
|
|
$this->output->writeln('<error>'.$message.'</error>'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Prompt the user for input. |
105
|
|
|
* |
106
|
|
|
* @param string $message |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function input(string $message): string |
111
|
|
|
{ |
112
|
|
|
return $this->output->ask($message); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Display confirmation input. |
117
|
|
|
* |
118
|
|
|
* @param string $message |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
protected function confirm(string $message): bool |
123
|
|
|
{ |
124
|
|
|
return $this->output->confirm($message, false); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get filesystem instance. |
129
|
|
|
* |
130
|
|
|
* @return Filesystem |
131
|
|
|
* |
132
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
133
|
|
|
*/ |
134
|
|
|
protected function filesystem(): Filesystem |
135
|
|
|
{ |
136
|
|
|
return $this->kernel->resolve(Filesystem::class); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Handle command. |
141
|
|
|
*/ |
142
|
|
|
abstract public function handle(): void; |
143
|
|
|
} |
144
|
|
|
|