/** * Loads Hostname models based on request * @param HostnameRepositoryContract $hostname * @return \HynMe\MultiTenant\Models\Hostname */ public static function hostname(HostnameRepositoryContract $hostname) { $tenant_hostname = null; try { if (!App::runningUnitTests() && !App::runningInConsole()) { $tenant_hostname = $hostname->findByHostname(Request::getHttpHost()); } if (!$tenant_hostname) { $tenant_hostname = $hostname->getDefault(); } } catch (QueryException $e) { // table not found, set up not yet done if (preg_match('/\\Qtable or view not found\\E/', $e->getMessage())) { return null; } } return $tenant_hostname; }
/** * Handles the set up */ public function handle() { $this->configuration = Config::get('webserver'); $this->comment('Welcome to hyn multi tenancy.'); $this->comment('First off, migrations for the packages will run.'); $this->runMigrations(); $this->comment('In the following steps you will be asked to set up your first tenant website.'); $tenantDirectory = Config::get('multi-tenant.tenant-directory') ? Config::get('multi-tenant.tenant-directory') : storage_path('multi-tenant'); if (!File::isDirectory($tenantDirectory) && File::makeDirectory($tenantDirectory, 0755, true)) { $this->comment("The directory to hold your tenant websites has been created under {$tenantDirectory}."); } $name = $this->option('tenant'); $email = $this->option('email'); $hostname = $this->option('hostname'); if (empty($name)) { throw new TenantPropertyException("No tenant name given; use --tenant"); } if (empty($email)) { throw new TenantPropertyException("No tenant email given; use --email"); } if (empty($hostname)) { throw new TenantPropertyException("No tenant hostname given; use --hostname"); } $webservice = null; /* * Setup webserver */ if ($this->helper) { $webservice = $this->option('webserver') ?: 'no'; if ($webservice != 'no') { $webserviceConfiguration = array_get($this->configuration, $webservice); $webserviceClass = array_get($webserviceConfiguration, 'class'); } else { $webservice = null; } /* * Create the first tenant configurations */ DB::beginTransaction(); $tenant = $this->tenant->create(compact('name', 'email')); $identifier = substr(str_replace(['.'], '-', $hostname), 0, 10); $website = $this->website->create(['tenant_id' => $tenant->id, 'identifier' => $identifier]); $host = $this->hostname->create(['hostname' => $hostname, 'website_id' => $website->id, 'tenant_id' => $tenant->id]); DB::commit(); // hook into the webservice of choice once object creation succeeded if (isset($webserviceClass)) { (new $webserviceClass($website))->register(); } if ($tenant->exists && $website->exists && $host->exists) { $this->info("Configuration succesful"); } } else { $this->error('The hyn-me/webserver package is not installed. Visit http://hyn.me/packages/webserver for more information.'); } }
/** * Return default website * * @return \HynMe\MultiTenant\Models\Website */ public function getDefault() { return $this->hostname->getDefault()->website; }