1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Parser; |
6
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
7
|
|
|
use Spatie\SlashCommand\Request; |
8
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
9
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
10
|
|
|
use Symfony\Component\Console\Input\StringInput; |
11
|
|
|
|
12
|
|
|
abstract class SignatureHandler extends BaseHandler |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
protected $name; |
16
|
|
|
|
17
|
|
|
/** @var \Symfony\Component\Console\Input\StringInput */ |
18
|
|
|
protected $input; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $couldBindSignature = false; |
22
|
|
|
|
23
|
|
|
public function __construct(Request $request) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($request); |
26
|
|
|
|
27
|
|
|
if (empty($this->signature)) { |
|
|
|
|
28
|
|
|
throw InvalidHandler::signatureIsRequired(static::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->parseSignature(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function parseSignature() |
35
|
|
|
{ |
36
|
|
|
list($name, $arguments, $options) = Parser::parse($this->signature); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->name = $name; |
39
|
|
|
|
40
|
|
|
$inputDefinition = new InputDefinition(); |
41
|
|
|
|
42
|
|
|
foreach ($arguments as $argument) { |
43
|
|
|
$inputDefinition->addArgument($argument); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($options as $option) { |
47
|
|
|
$inputDefinition->addOption($option); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->input = new StringInput($this->request->text); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$this->input->bind($inputDefinition); |
54
|
|
|
$this->couldBindSignature = true; |
55
|
|
|
} |
56
|
|
|
catch(RuntimeException $exception) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getArgument($foo) |
63
|
|
|
{ |
64
|
|
|
return $this->input->getArgument($foo); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getOption($foo) |
68
|
|
|
{ |
69
|
|
|
return $this->input->getOption($foo); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getArguments(): array |
73
|
|
|
{ |
74
|
|
|
return $this->input->getArguments(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getOptions(): array |
78
|
|
|
{ |
79
|
|
|
return $this->input->getOptions(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function canHandle(Request $request): bool |
83
|
|
|
{ |
84
|
|
|
if (! $this->couldBindSignature) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$commandName = explode(' ', $this->signature)[0]; |
|
|
|
|
89
|
|
|
|
90
|
|
|
return strtolower($request->command) === strtolower($commandName); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.