Jarves\Router\FrontendRouter::searchDomain PHP Method

searchDomain() public method

Returns the domain if found
public searchDomain ( ) : Domain | null
return Jarves\Model\Domain | null
    public function searchDomain()
    {
        $request = $this->getRequest();
        $dispatcher = $this->eventDispatcher;
        if ($domainId = $request->get('_jarves_editor_domain')) {
            $hostname = DomainQuery::create()->select('domain')->findOneById($domainId);
        } else {
            $hostname = $request->getHost();
        }
        $title = sprintf('Searching Domain [%s]', $hostname);
        $this->stopwatch->start($title);
        /** @var \Jarves\Model\Domain $foundDomain */
        $foundDomain = null;
        $possibleLanguage = $this->getPossibleLanguage();
        $hostnameWithLanguage = $hostname . '/' . $possibleLanguage;
        $cachedDomains = $this->cacher->getDistributedCache('core/domains');
        if ($cachedDomains) {
            $cachedDomains = @unserialize($cachedDomains);
        }
        if (!is_array($cachedDomains)) {
            $cachedDomains = array();
            $domains = DomainQuery::create()->find();
            foreach ($domains as $domain) {
                $key = $domain->getDomain();
                $langKey = '';
                if (!$domain->getMaster()) {
                    $langKey = '/' . $domain->getLang();
                }
                $cachedDomains[$key . $langKey] = $domain;
                if ($domain->getRedirect()) {
                    $redirects = $domain->getRedirect();
                    $redirects = explode(',', str_replace(' ', '', $redirects));
                    foreach ($redirects as $redirectDomain) {
                        $cachedDomains['!redirects'][$redirectDomain . $langKey] = $key . $langKey;
                    }
                }
                if ($domain->getAlias()) {
                    $aliases = $domain->getAlias();
                    $aliases = explode(',', str_replace(' ', '', $aliases));
                    foreach ($aliases as $aliasDomain) {
                        $cachedDomains['!aliases'][$aliasDomain . $langKey] = $key . $langKey;
                    }
                }
            }
            $this->cacher->setDistributedCache('core/domains', serialize($cachedDomains));
        }
        //search redirect
        if (isset($cachedDomains['!redirects']) && (isset($cachedDomains['!redirects'][$hostnameWithLanguage]) && ($redirectToDomain = $cachedDomains['!redirects'][$hostnameWithLanguage])) || isset($cachedDomains['!redirects'][$hostname]) && ($redirectToDomain = $cachedDomains['!redirects'][$hostname])) {
            $foundDomain = $cachedDomains[$redirectToDomain];
            $dispatcher->dispatch('core/domain-redirect', new GenericEvent($foundDomain));
            return null;
        }
        //search alias
        if (isset($cachedDomains['!aliases']) && (isset($cachedDomains['!aliases'][$hostnameWithLanguage]) && ($aliasHostname = $cachedDomains['!aliases'][$hostnameWithLanguage]) || isset($cachedDomains['!aliases'][$hostname]) && ($aliasHostname = $cachedDomains['!aliases'][$hostname]))) {
            $foundDomain = $cachedDomains[$aliasHostname];
            $hostname = $aliasHostname;
        } else {
            if (isset($cachedDomains[$hostname])) {
                $foundDomain = $cachedDomains[$hostname];
            }
        }
        if (!$foundDomain) {
            $dispatcher->dispatch('core/domain-not-found', new GenericEvent($hostname));
            $this->stopwatch->stop($title);
            return null;
        }
        $foundDomain->setRealDomain($hostname);
        $this->stopwatch->stop($title);
        return $foundDomain;
    }

Usage Example

 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
         //we need to reset all routes. They will anyway replaced by FrontendRouter::loadRoutes,
         //but to prevent caching conflicts, when a user removes a plugin for example
         //from a page, we need to know that without using actual caching.
         $this->routes = new RouteCollection();
         $this->urlMatcher->__construct($this->routes, $this->requestContext);
         //prepare for new master request: clear the PageResponse object
         $this->pageStack->setCurrentPage(null);
         $this->pageStack->setCurrentDomain(null);
         $this->pageStack->setPageResponse($this->pageResponseFactory->create());
         $this->frontendRouter->setRequest($request);
         $editorNodeId = (int) $this->pageStack->getRequest()->get('_jarves_editor_node');
         $editorDomainId = (int) $this->pageStack->getRequest()->get('_jarves_editor_domain');
         $domain = null;
         if ($editorDomainId) {
             $domain = $this->pageStack->getDomain($editorDomainId);
             if (!$domain) {
                 //we haven't found any domain that is responsible for this request
                 return;
             }
             $this->pageStack->setCurrentDomain($domain);
         }
         if ($editorNodeId) {
             //handle jarves content editor stuff
             //access is later checked
             if (!$editorNodeId && $domain) {
                 $editorNodeId = $domain->getStartnodeId();
             }
             $page = $this->pageStack->getPage($editorNodeId);
             if (!$page || !$page->isRenderable()) {
                 //we haven't found any page that is responsible for this request
                 return;
             }
             if (!$domain) {
                 $domain = $this->pageStack->getDomain($page->getDomainId());
             }
             $this->pageStack->setCurrentPage($page);
             $this->pageStack->setCurrentDomain($domain);
             $request->attributes->set('_controller', 'jarves.page_controller:handleAction');
         } else {
             //regular frontend route search
             //search domain
             if (!$domain) {
                 $domain = $this->frontendRouter->searchDomain();
                 if (!$domain) {
                     //we haven't found any domain that is responsible for this request
                     return;
                 }
                 $this->pageStack->setCurrentDomain($domain);
             }
             //search page
             $page = $this->frontendRouter->searchPage();
             if (!$page || !$page->isRenderable()) {
                 //we haven't found any page that is responsible for this request
                 return;
             }
             $this->pageStack->setCurrentPage($page);
             if ($response = $this->frontendRouter->loadRoutes($this->routes, $page)) {
                 //loadRoutes return in case of redirects and permissions a redirect or 404 response.
                 $event->setResponse($response);
                 return;
             }
             try {
                 //check routes in $this->route
                 parent::onKernelRequest($event);
             } catch (MethodNotAllowedException $e) {
             } catch (NotFoundHttpException $e) {
             }
         }
     }
 }