1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-15 00:04:04 +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:
Samuel Štancl 2019-08-09 19:06:00 +02:00 committed by GitHub
parent 85b2274d1d
commit 15f09e59df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 169 additions and 4 deletions

66
src/Commands/Run.php Normal file
View 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);
}
}
}