1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaling\Playbook\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Scaling\Playbook\Playbook; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Scaling\Playbook\PlaybookDefenition; |
9
|
|
|
|
10
|
|
|
class PlaybookCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'playbook:run {playbook?}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Run a predefined playbook to fill your application.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List of definitions that has been run. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $ranDefenitions = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function handle() |
39
|
|
|
{ |
40
|
|
|
if (! $this->isAllowedInProduction()) { |
41
|
|
|
$this->error('You can only run commands in local environment'); |
42
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (is_null($name = $this->argument('playbook'))) { |
47
|
|
|
$name = $this->askWhichBook($this->getAvailablePlaybooks()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->call('migrate:refresh'); |
51
|
|
|
|
52
|
|
|
$this->runPlaybook($this->resolvePlaybook($name)); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Run the selected playbook. |
57
|
|
|
* |
58
|
|
|
* @param \Scaling\Playbook\PlaybookDefenition $definition |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
private function runPlaybook(PlaybookDefenition $definition): void |
62
|
|
|
{ |
63
|
|
|
foreach ($definition->playbook->before() as $book) { |
64
|
|
|
$this->runPlaybook($this->resolvePlaybook($book)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
for ($i = 1; $i <= $definition->times; $i++) { |
68
|
|
|
if ($definition->once && $this->definitionHasRun($definition)) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$name = get_class($definition->playbook); |
73
|
|
|
|
74
|
|
|
$this->info("Running playbook `{$name}` (#{$i})"); |
75
|
|
|
|
76
|
|
|
$definition->playbook->run($this->input, $this->output); |
77
|
|
|
$definition->playbook->finished(); |
78
|
|
|
|
79
|
|
|
$this->ranDefinitions[$definition->id] = ($this->ranDefinitions[$definition->id] ?? 0) + 1; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($definition->playbook->after() as $book) { |
83
|
|
|
$this->runPlaybook($this->resolvePlaybook($book)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get a list of available playbooks. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
private function getAvailablePlaybooks(): array |
93
|
|
|
{ |
94
|
|
|
$files = scandir(config('laravel-playbook.path')); |
95
|
|
|
|
96
|
|
|
unset($files[0], $files[1]); |
97
|
|
|
|
98
|
|
|
return array_values(array_map(function (string $fileName) { |
99
|
|
|
return str_replace('.php', '', $fileName); |
100
|
|
|
}, $files)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Ask which playbook the developer wants to run. |
105
|
|
|
* |
106
|
|
|
* @param array $available |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
private function askWhichBook(array $available): string |
110
|
|
|
{ |
111
|
|
|
return $this->choice('Please choose a playbook', $available); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Resolve the definition for the given playbook. |
116
|
|
|
* |
117
|
|
|
* @param Scaling\Playbook\Playbook|Scaling\Playbook\PlaybookDefinition|string $class |
118
|
|
|
* @return \Scaling\Playbook\PlaybookDefenition |
119
|
|
|
*/ |
120
|
|
|
private function resolvePlaybook($class): PlaybookDefenition |
121
|
|
|
{ |
122
|
|
|
if ($class instanceof PlaybookDefenition) { |
123
|
|
|
return $class; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($class instanceof Playbook) { |
127
|
|
|
return new PlaybookDefenition(get_class($class)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$namespace = config('laravel-playbook.namespace'); |
131
|
|
|
|
132
|
|
|
if (! Str::startsWith($class, [$namespace, "\\{$namespace}"])) { |
133
|
|
|
$class = "{$namespace}\\{$class}"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new PlaybookDefenition($class); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Determine if the given definition has ran before. |
141
|
|
|
* |
142
|
|
|
* @param \Scaling\Playbook\PlaybookDefenition $definition |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
private function definitionHasRun(PlaybookDefenition $definition): bool |
146
|
|
|
{ |
147
|
|
|
return isset($this->ranDefenitions[$definition->id]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine if the action is allowed in a production environment. |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
private function isAllowedInProduction(): bool |
156
|
|
|
{ |
157
|
|
|
if (!config('laravel-playbook.production') && config('app.env') === 'production') { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.