|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TomSchlick\Townhouse; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
abstract class BaseTenantableCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Declare the handle method as final and use it to traverse through tenants. |
|
13
|
|
|
*/ |
|
14
|
|
|
final public function handle() |
|
15
|
|
|
{ |
|
16
|
|
|
if (! $this->confirmToProceed()) { |
|
|
|
|
|
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** @var Tenant $tenant */ |
|
21
|
|
|
foreach ($this->parseTenantInput() as $tenant) { |
|
|
|
|
|
|
22
|
|
|
$this->info("Starting for tenant: {$tenant->id} - {$tenant->name}"); |
|
23
|
|
|
|
|
24
|
|
|
$this->bootstrapTenant($tenant); |
|
25
|
|
|
|
|
26
|
|
|
$this->handleTenant($tenant); |
|
27
|
|
|
|
|
28
|
|
|
$this->info("\nFinished for tenant: " . $tenant->id); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \TomSchlick\Townhouse\Tenant $tenant |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract public function handleTenant(Tenant $tenant) : void; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get or ask for the tenant(s) you'd like to run the commands on. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Support\Collection|null |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function parseTenantInput() : ?Collection |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->option('all')) { |
|
47
|
|
|
return Tenant::all(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$input_ids = $this->option('tenant') |
|
51
|
|
|
? $this->option('tenant') |
|
52
|
|
|
: $this->ask('Which tenant would you like to use? [tenant_id]'); |
|
53
|
|
|
|
|
54
|
|
|
return Tenant::find( |
|
55
|
|
|
array_map( |
|
56
|
|
|
function ($id) { |
|
57
|
|
|
return trim($id); |
|
58
|
|
|
}, |
|
59
|
|
|
explode(',', $input_ids) |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the console command arguments. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getArguments() |
|
70
|
|
|
{ |
|
71
|
|
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the console command options. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getOptions() |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
|
|
['tenant', null, InputOption::VALUE_REQUIRED, 'The tenant you want to run the command as.'], |
|
83
|
|
|
['all', null, InputOption::VALUE_NONE, 'Run all tenants instead of just one.'], |
|
84
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Override the fire method as well. |
|
90
|
|
|
*/ |
|
91
|
|
|
final public function fire() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->handle(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Make Laravel ready for the tenant we want to run the command on. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \TomSchlick\Townhouse\Tenant $tenant |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function bootstrapTenant(Tenant $tenant) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->checkTenantDatabaseExists && ! $tenant->databaseExists()) { |
|
|
|
|
|
|
104
|
|
|
$this->error('Tenant ' . $tenant->id . ' does not have a database setup.'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($tenant->databaseExists()) { |
|
108
|
|
|
$tenant->setCurrentTenantConfiguration(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.