1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SmartWeb\ModuleTesting\Console\Command; |
5
|
|
|
|
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use SmartWeb\ModuleTesting\Console\CommandArguments; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class BaseCommand |
12
|
|
|
* |
13
|
|
|
* @package SmartWeb\ModuleTesting\Console\Command |
14
|
|
|
*/ |
15
|
|
|
abstract class BaseCommand extends Command |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Namespace for the command. |
20
|
|
|
*/ |
21
|
|
|
const COMMAND_NAMESPACE = 'smartweb-testing'; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->configureName(); |
26
|
|
|
|
27
|
|
|
parent::__construct(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute the console command. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
abstract public function handle(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return CommandArguments |
39
|
|
|
*/ |
40
|
|
|
abstract protected function provideArguments() : CommandArguments; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the value of a command option, with an optional fallback if the option is not set. |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* @param string|array $default |
47
|
|
|
* |
48
|
|
|
* @return string|array |
49
|
|
|
*/ |
50
|
|
|
final protected function getOption(string $key, $default) |
51
|
|
|
{ |
52
|
|
|
return $this->hasOption($key) |
53
|
|
|
? $this->option($key) |
54
|
|
|
: $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the console command arguments. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
final protected function getArguments() |
63
|
|
|
{ |
64
|
|
|
return $this->provideArguments()->toArray(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function nameIsNotConfigured() : bool |
71
|
|
|
{ |
72
|
|
|
return !Str::startsWith($this->name, self::COMMAND_NAMESPACE); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function configureName() |
76
|
|
|
{ |
77
|
|
|
if ($this->nameIsNotConfigured()) { |
78
|
|
|
static::$defaultName = $this->name; |
79
|
|
|
$this->name = self::COMMAND_NAMESPACE . ':' . $this->name; |
80
|
|
|
} else { |
81
|
|
|
static::$defaultName = Str::replaceFirst(self::COMMAND_NAMESPACE . ':', '', $this->name); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|