Esempio n. 1
0
 /**
  * Detect available locale
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface|string
  */
 public function detect(RequestInterface $request, ResponseInterface $response = null)
 {
     $event = new Event(Event::EVENT_DETECT, $this);
     $event->setRequest($request);
     $event->setResponse($response);
     if ($this->hasLocales()) {
         $event->setLocales($this->getLocales());
     }
     $eventManager = $this->getEventManager();
     $results = $eventManager->trigger($event, function ($r) {
         return is_string($r);
     });
     if ($results->stopped()) {
         $locale = $results->last();
     } else {
         $locale = $this->getDefault();
     }
     if ($this->hasLocales() && !$this->hasLocale($locale)) {
         $locale = $this->getDefault();
     }
     // Trigger FOUND event only when a response is given
     if ($response instanceof ResponseInterface) {
         $event->setName(Event::EVENT_FOUND);
         $event->setLocale($locale);
         $return = false;
         /**
          * The response will be returned instead of the found locale
          * only in case a adapter returned the response. This is an
          * indication the adapter has updated the response (e.g. with
          * a Location header) and as such, the response must be returned
          * instead of the locale.
          */
         $eventManager->trigger($event, function ($r) use(&$return) {
             if ($r instanceof ResponseInterface) {
                 $return = true;
             }
         });
         if ($return) {
             return $response;
         }
     }
     return $locale;
 }