Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/ts.corals.io/corals-api/app/Console/Commands/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/ts.corals.io/corals-api/app/Console/Commands/CreateTenantCommand.php
<?php

namespace App\Console\Commands;

use Corals\Settings\Facades\Modules;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Spatie\Multitenancy\Models\Tenant;

class CreateTenantCommand extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tenant:create {--name=} {--domain=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create new tenant';

    /**
     * @var Tenant
     */
    protected $tenant;

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        try {

            $this->createTenant()
                ->configureTenantDatabase()
                ->installTimesheetModule();

        } catch (\Exception $e) {
            $this->error($e->getMessage());
            report($e);
        } finally {
            Tenant::forgetCurrent();
        }

    }

    /**
     * @return $this
     */
    protected function createTenant(): self
    {
        Tenant::unguarded(function () {
            $this->tenant = Tenant::query()->create([
                'name' => $this->option('name'),
                'domain' => $this->option('domain'),
                'database' => $this->validDataBasename()
            ]);
        });

        return $this;
    }

    /**
     * @return string
     */
    protected function validDataBasename(): string
    {
        $domain = $this->option('name');

        $originalDBName = explode('.', $domain)[0];
        $databaseName = $originalDBName;

        $index = 0;

        while (Tenant::query()->where('database', $databaseName)->exists()) {
            $databaseName = sprintf("%s_%s", $originalDBName, $index++);
        }

        return $databaseName;
    }

    /**
     * @return $this
     */
    protected function configureTenantDatabase(): self
    {
        Schema::createDatabase($this->tenant->getDatabaseName());

        $laravelCommands = [
            'c:c',
            "migrate:fresh --seed",
            'passport:install'
        ];

        foreach ($laravelCommands as $laravelCommand) {
            $this->line("run $laravelCommand command");

            // ->makeCurrent(), will be called inside tenants command
            // no need to be called out
            Artisan::call('tenants:artisan', [
                'artisanCommand' => $laravelCommand,
                '--tenant' => $this->tenant->id
            ]);

        }

        return $this;
    }

    /**
     *
     */
    protected function installTimesheetModule()
    {
        $this->tenant->makeCurrent();

        $this->line('run clear caches command');
        Artisan::call('migrate');
        $this->line(Artisan::output());

        $availableModules = Modules::getModulesSettings();

        $this->line('Check and update core modules');

        $availableModules->where('type', 'core')->each(function ($module) {
            Modules::update($module->code);
        });

        $sortedModules = [
            'corals-utility',
            'corals-timesheet',
        ];

        $this->line('Update or install required modules');


        foreach ($sortedModules as $moduleCode) {
            if ($availableModules->where('code', $moduleCode)->where('installed')->isEmpty()) {
                $this->line(sprintf('%s:: %s', $moduleCode, 'install'));
                try {
                    Modules::install($moduleCode);
                } catch (\Exception $exception) {
                    $this->line(sprintf(
                        '%s:: %s because of: %s',
                        $moduleCode,
                        'retry install',
                        $exception->getMessage()
                    ));
                    Modules::uninstall($moduleCode);
                    //do another try in case of failure
                    Modules::install($moduleCode);
                }
            } else {
                $this->line(sprintf('%s:: %s', $moduleCode, 'update'));
                Modules::update($moduleCode);
            }
        }

        $this->line('run clear caches command');
        Artisan::call('c:c');

        return $this;
    }

    /**
     * @param $connection
     */
    protected function setDefaultConnection($connection): void
    {
        DB::setDefaultConnection($connection);
    }
}

Spamworldpro Mini