1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel\App\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Faker\Generator as Faker; |
7
|
|
|
|
8
|
|
|
class CreateTenant extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'tenant:create-tenant {--fake}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Create a new tenant'; |
23
|
|
|
|
24
|
|
|
protected $faker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct(Faker $faker) |
32
|
|
|
{ |
33
|
|
|
$this->faker = $faker; |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handle() |
44
|
|
|
{ |
45
|
|
|
$count = (int) $this->ask('How many would you like to create?'); |
46
|
|
|
|
47
|
|
|
while ($count > 0) { |
48
|
|
|
if(!$this->option('fake')) { |
49
|
|
|
$this->createNewTenant(); |
50
|
|
|
} else { |
51
|
|
|
$this->createFakeTenant(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$count--; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Setup a fake user using faker |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function createFakeTenant() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$name = $this->faker->name; |
67
|
|
|
|
68
|
|
|
$create_new = $this->anticipate('Would you like to create a new user, or use an existing?', ['New', 'Existing'], 'New'); |
69
|
|
|
|
70
|
|
|
if ($create_new === "New") { |
71
|
|
|
$name = (string) $this->ask('Name'); |
72
|
|
|
$email = (string) $this->ask('E-Mail'); |
73
|
|
|
$user = config('multi-tenant.user_class')::create([ |
74
|
|
|
'name' => $name, |
75
|
|
|
'email' => $email, |
76
|
|
|
'password' => bcrypt('tester'), |
77
|
|
|
]); |
78
|
|
|
} else { |
79
|
|
|
$headers = ['Name', 'ID']; |
80
|
|
|
$tenants = config('multi-tenant.user_class')::all('name', 'id'); |
81
|
|
|
$this->table($headers, $tenants->toArray()); |
82
|
|
|
|
83
|
|
|
$user_id = (int) $this->ask('Please enter the id of the desired user.'); |
84
|
|
|
|
85
|
|
|
$user = config('multi-tenant.user_class')::findOrFail($user_id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$tenant = config('multi-tenant.tenant_class')::create([ |
89
|
|
|
'name' => $name, |
90
|
|
|
'owner_id' => $user->id, |
91
|
|
|
'slug' => str_slug($name) |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Setup a new tenant with user supplied settings for the tenant |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
private function createNewTenant() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
$tenant_name = (string) $this->ask('Please enter a name for your new tenant.'); |
106
|
|
|
|
107
|
|
|
$create_new = $this->anticipate('Would you like to create a new user, or use an existing?', ['New', 'Existing'], 'New'); |
108
|
|
|
|
109
|
|
|
if ($create_new === "New") { |
110
|
|
|
$user_name = (string) $this->ask('Please enter a name for the new user'); |
111
|
|
|
$user_email = (string) $this->ask('Please enter an e-mail for the new user'); |
112
|
|
|
$user = config('multi-tenant.user_class')::create([ |
113
|
|
|
'name' => $user_name, |
114
|
|
|
'email' => $user_email, |
115
|
|
|
'password' => bcrypt('tester'), |
116
|
|
|
]); |
117
|
|
|
} else { |
118
|
|
|
$headers = ['Name', 'ID']; |
119
|
|
|
$tenants = config('multi-tenant.user_class')::all('name', 'id'); |
120
|
|
|
$this->table($headers, $tenants->toArray()); |
121
|
|
|
|
122
|
|
|
$user_id = (int) $this->ask('Please enter the id of the desired user.'); |
123
|
|
|
|
124
|
|
|
$user = config('multi-tenant.user_class')::findOrFail($user_id); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$tenant = config('multi-tenant.tenant_class')::create([ |
128
|
|
|
'name' => $tenant_name, |
129
|
|
|
'owner_id' => $user->id, |
130
|
|
|
'slug' => str_slug($tenant_name) |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.