Example #1
0
 /**
  * Подставить сегмент языка в ссылку
  * @param string $path
  * @param $locale
  * @return string
  */
 public function filterLocaleLink($path, $locale = null)
 {
     if (strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0) {
         return $path;
     }
     if ($locale) {
         $office = $this->officeManager->getByLocale($locale);
     } else {
         $office = $this->officeManager->getCurrentOffice();
         if (!$office) {
             $office = $this->officeManager->getDefault();
         }
     }
     if (empty($this->offices)) {
         $this->offices = $this->officeManager->getRoutingOffices();
     }
     $languages = array_keys($this->offices);
     $resultUrl = $path;
     if ($office->getIncludeLangInUrl()) {
         $arrPath = explode('/', ltrim($path, '/'));
         if (empty($arrPath[0]) || !in_array($arrPath[0], $languages)) {
             $resultUrl = '/' . $office->getDefaultLanguage() . '/' . ltrim($path, '/');
         }
     }
     return $resultUrl;
 }
Example #2
0
 public function getOfficeLocales()
 {
     $office = $this->officeManager->getCurrentOffice();
     $locales = [];
     if ($office) {
         $locales = $this->officeManager->getAvailableLanguageEntities($office);
     }
     return $locales;
 }
Example #3
0
 public function onKernelRequest(GetResponseEvent $event)
 {
     if ($this->officeManager->getCurrentOffice()) {
         return;
     }
     $request = $event->getRequest();
     //Disable office check for control panel and web-profiler view
     foreach ($this->urlIgnorePrefixes as $prefix) {
         if (strpos($request->getPathInfo(), $prefix) === 0) {
             return;
         }
     }
     $officeNotFoundException = new OfficeNotFoundException('Not found office for host `' . $request->getHost() . '`.' . 'Maybe you need to create this office from Control Panel.');
     $officeByHostLocale = [];
     foreach ($this->officeManager->getOffices() as $office) {
         if (empty($officeByHostLocale[$office->getHost()])) {
             $officeByHostLocale[$office->getHost()] = [];
         }
         $officeByHostLocale[$office->getHost()][$office->getDefaultLanguage()] = $office;
     }
     if (empty($officeByHostLocale[$request->getHost()])) {
         throw $officeNotFoundException;
     }
     /** @var Office[] $offices */
     $offices = $officeByHostLocale[$request->getHost()];
     $route = $request->attributes->get('_route');
     $currentOffice = null;
     // офис определили по домену
     if (count($offices) == 1) {
         list(, $currentOffice) = each($offices);
     }
     if (!$route) {
         // обрабатываем 404 ошибку
         $expectedLocale = null;
         // пытаемся по урлу определить офис
         if (preg_match('!^/([a-z]{2})/!', $request->getRequestUri(), $a)) {
             $expectedLocale = $a[1];
             if (!empty($offices[$expectedLocale])) {
                 $currentOffice = $offices[$expectedLocale];
             }
         }
         if (!$currentOffice instanceof Office) {
             foreach ($offices as $office) {
                 // берем офис по умолчанию
                 if (!$office->getIncludeLangInUrl()) {
                     $currentOffice = $office;
                     break;
                 }
             }
             // ничего не нашли, берем первый офис
             if (!$currentOffice instanceof Office) {
                 list(, $currentOffice) = each($offices);
             }
         }
     } elseif (substr($route, 0, 1) == '_') {
         foreach ($offices as $office) {
             // берем офис по умолчанию
             if (!$office->getIncludeLangInUrl()) {
                 $currentOffice = $office;
                 break;
             }
         }
     }
     if (!$currentOffice instanceof Office && !empty($offices[$request->getLocale()])) {
         $currentOffice = $offices[$request->getLocale()];
     }
     if ($currentOffice instanceof Office) {
         $this->officeManager->setCurrentOffice($currentOffice);
         $request->setLocale($currentOffice->getDefaultLanguage());
     } else {
         throw $officeNotFoundException;
     }
 }