/** * Handles the Request * * @param GetResponseEvent $event * * @return null */ public function handleRequest(GetResponseEvent $event) { // Some requests are not altered at all: // - The requests not flagged as MASTER (do not handle sub request like esi includes) // - The fragments request created by Symfony (not considered at SUB_REQUESTS but MASTER) // - If the device view is "not the mobile view" (e.g. we're not in the request context) $currentRequestType = $event->getRequestType(); $masterRequestType = HttpKernelInterface::MASTER_REQUEST; $isNotMasterRequest = $currentRequestType !== $masterRequestType; $isSymfonyClientRequest = $this->isSymfonyClientRequest($event->getRequest()); $isFragmentRequest = $this->fragmentPath === rawurldecode($event->getRequest()->getPathInfo()); if ($isNotMasterRequest || $isFragmentRequest || $this->deviceView->isNotMobileView() || $isSymfonyClientRequest) { return; } $this->mobileDetector->setUserAgent($event->getRequest()->headers->get('user-agent')); // Sets the flag for the response handled by the GET switch param and the type of the view. if ($this->deviceView->hasSwitchParam()) { $event->setResponse($this->getRedirectResponseBySwitchParam()); return; } // If neither the SwitchParam nor the cookie are set, detect the view... $cookieIsSet = $this->deviceView->getRequestedViewType() !== null; if (!$cookieIsSet) { if ($this->redirectConf['detect_tablet_as_mobile'] === false && $this->mobileDetector->isTablet()) { $this->deviceView->setTabletView(); } elseif ($this->mobileDetector->isMobile()) { $this->deviceView->setMobileView(); } else { $this->deviceView->setFullView(); } } // Check if we must redirect to the target view and do so if needed if ($this->mustRedirect($this->deviceView->getViewType())) { if ($response = $this->getRedirectResponse($this->deviceView->getViewType())) { $event->setResponse($response); } return; } // No need to redirect // We don't need to modify _every_ response: once the cookie is set, // save badwith and CPU cycles by just letting it expire someday. if ($cookieIsSet) { return; } // Sets the flag for the response handler and prepares the modification closure $this->needModifyResponse = true; $this->prepareResponseModification($this->deviceView->getViewType()); }
/** * Handles the Request * * @param GetResponseEvent $event * * @return null */ public function handleRequest(GetResponseEvent $event) { // only handle master request, do not handle sub request like esi includes // If the device view is "not the mobile view" (e.g. we're not in the request context) if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST || $this->deviceView->isNotMobileView()) { return; } $this->mobileDetector->setUserAgent($event->getRequest()->headers->get('user-agent')); // Sets the flag for the response handled by the GET switch param and the type of the view. if ($this->deviceView->hasSwitchParam()) { $event->setResponse($this->getRedirectResponseBySwitchParam()); return; } // If neither the SwitchParam nor the cookie are set, detect the view... $cookieIsSet = $this->deviceView->getRequestedViewType() !== null; if (!$cookieIsSet) { if ($this->redirectConf['detect_tablet_as_mobile'] === false && $this->mobileDetector->isTablet()) { $this->deviceView->setTabletView(); } elseif ($this->mobileDetector->isMobile()) { $this->deviceView->setMobileView(); } else { $this->deviceView->setFullView(); } } // Check if we must redirect to the target view and do so if needed if ($this->mustRedirect($this->deviceView->getViewType())) { if ($response = $this->getRedirectResponse($this->deviceView->getViewType())) { $event->setResponse($response); } return; } // No need to redirect // We don't need to modify _every_ response: once the cookie is set, // save badwith and CPU cycles by just letting it expire someday. if ($cookieIsSet) { return; } // Sets the flag for the response handler and prepares the modification closure $this->needModifyResponse = true; $this->prepareResponseModification($this->deviceView->getViewType()); }