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