File: D:/HostingSpaces/SBogers10/helder.komma.pro/app/Http/Middleware/SiteResolver.php
<?php
namespace App\Http\Middleware;
use App\Komma\Sites\SiteServiceInterface;
/**
* Class SiteResolver
*
* Does set the current site depending on the host / domain name using the site config file
*
* @package App\Http\Middleware
*/
class SiteResolver
{
/** @var SiteServiceInterface */
private $siteService;
public function __construct()
{
$this->siteService = \App::make(SiteServiceInterface::class);
}
/**
* Set site on Application.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
//Skip this when it is an Kms route
if($request->segment(1) == 'kms') return $next($request);
$site = null;
if(!\Config::get('app.multipleSites')){
$this->siteService->setCurrentSiteToDefault();
// If there is only one site, the redirect should be handled by the web.config
}
else{
$root = $request->root();
$path = $request->path();
$siteDomains = config('domains.' . \App::environment());
$activeSiteSlug = '';
if(!$siteDomains) throw new \RuntimeException('Please add the environment "'.\App::environment().'" to the domains config file');
foreach ($siteDomains as $siteKey => $domains){
foreach ($domains as $domain){
if(str_contains($root, $domain)){
if($activeSiteSlug == '') $activeSiteSlug = $siteKey;
else throw new \InvalidArgumentException("Domain isn't specific enough because active site key is overruled by other site, best practice is to define domains with there top-level domain in the site config");
}
}
}
// Find the site by the active site slug
$this->siteService->setCurrentSiteBySlug($activeSiteSlug);
// Check if the set site is on the main domain and has https
$secureMainRoot = 'https://' . config('domains.' . \App::environment() . '.' . $activeSiteSlug)[0];
if (\App::environment() == 'production' && $root !== $secureMainRoot) {
// If there is a path defined, append it to the redirect string
$redirectPath = $secureMainRoot;
if (isset($path) && ! in_array($path, ['', '/', 'index.php'])) {
$redirectPath .= '/' . $path;
}
return redirect($redirectPath);
}
}
$site = $this->siteService->getCurrentSite();
if(!$site->exists) throw new \InvalidArgumentException("This site doesn't exists. Make a new Site with this slug:'".$activeSiteSlug."' or rename the desired site to this slug.");
// Check if defined language is one of the languages of this site
// if not throw error, because it shouldn't happen...
if(!$site->languages->contains(\App::getLanguage())){
throw new \InvalidArgumentException("Defined app language doesn't exist in this site, so check how this could happen.");
}
//Return the Request, and check the other routes
return $next($request);
}
}