mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
[1.7.0] Add tenants:run (#81)
* wip * Apply fixes from StyleCI * first implementation * Apply fixes from StyleCI * Add support for arguments and options * Apply fixes from StyleCI * Write docs, add support for = in arg/opt value * Apply fixes from StyleCI * add $ [ci skip]
This commit is contained in:
parent
85b2274d1d
commit
15f09e59df
7 changed files with 169 additions and 4 deletions
66
src/Commands/Run.php
Normal file
66
src/Commands/Run.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class Run extends Command
|
||||
{
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run a command for tenant(s)';
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = "tenants:run {commandname : The command's name.}
|
||||
{--tenants= : The tenant(s) to run the command for. Default: all}
|
||||
{--argument=* : The arguments to pass to the command. Default: none}
|
||||
{--option=* : The options to pass to the command. Default: none}";
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($tenancy_was_initialized = tenancy()->initialized) {
|
||||
$previous_tenants_domain = tenant('domain');
|
||||
}
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
|
||||
tenancy()->init($tenant['domain']);
|
||||
|
||||
$callback = function ($prefix = '') {
|
||||
return function ($arguments, $argument) use ($prefix) {
|
||||
[$key, $value] = explode('=', $argument, 2);
|
||||
$arguments[$prefix . $key] = $value;
|
||||
|
||||
return $arguments;
|
||||
};
|
||||
};
|
||||
|
||||
// Turns ['foo=bar', 'abc=xyz=zzz'] into ['foo' => 'bar', 'abc' => 'xyz=zzz']
|
||||
$arguments = array_reduce($this->option('argument'), $callback(), []);
|
||||
|
||||
// Turns ['foo=bar', 'abc=xyz=zzz'] into ['--foo' => 'bar', '--abc' => 'xyz=zzz']
|
||||
$options = array_reduce($this->option('option'), $callback('--'), []);
|
||||
|
||||
// Run command
|
||||
$this->call($this->argument('commandname'), array_merge($arguments, $options));
|
||||
|
||||
tenancy()->end();
|
||||
});
|
||||
|
||||
if ($tenancy_was_initialized) {
|
||||
tenancy()->init($previous_tenants_domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
use Stancl\Tenancy\Commands\Run;
|
||||
use Stancl\Tenancy\Commands\Seed;
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Stancl\Tenancy\Commands\Migrate;
|
||||
|
|
@ -23,9 +24,10 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([
|
||||
Run::class,
|
||||
Seed::class,
|
||||
Migrate::class,
|
||||
Rollback::class,
|
||||
Seed::class,
|
||||
TenantList::class,
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue