src/Service/CanonicalUrlGenerator.php line 79

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-07-19
  5.  * Time: 13:32
  6.  */
  7. namespace App\Service;
  8. use App\Controller\ProfileListController;
  9. use App\Routing\DynamicRouter;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. class CanonicalUrlGenerator
  14. {
  15.     private string $defaultCity;
  16.     public function __construct(
  17.         protected UrlGeneratorInterface $urlGenerator,
  18.         protected RequestStack $requestStack,
  19.         private Features $features,
  20.         ParameterBagInterface $parameterBag
  21.     )
  22.     {
  23.         $this->defaultCity $parameterBag->get('default_city');
  24.     }
  25.     public function generate(?string $host null): string
  26.     {
  27.         $request $this->requestStack->getMasterRequest();
  28.         if (!$request) {
  29.             return '';
  30.         }
  31.         $routeName $request->attributes->get('_route''');
  32.         $parameters $request->attributes->get('_route_params', []);
  33.         if (!$routeName) {
  34.             return '';
  35.         }
  36.         if (ProfileListController::class.'::listByCity' === $request->attributes->get('_controller') && !$this->features->homepage_as_city_list()) {
  37.             $currentPage $this->features->canonical_url_with_pagination()
  38.                 ? (int)$request->get('page')
  39.                 : 1// При выключенной пагинации для каноникалов генерим урл для первой страницы
  40.             $currentCity $parameters['city'] ?? '';
  41.             if ($this->defaultCity === $currentCity && $currentPage 2) {
  42.                 $routeName 'homepage';
  43.             }
  44.         }
  45.         // Если открыта страница переопределенного роута, выдаем в canonical новый адрес
  46.         if ($request->attributes->has(DynamicRouter::OVERRIDDEN_ROUTE_PARAMETER)) {
  47.             $routeName $request->attributes->get(DynamicRouter::OVERRIDDEN_ROUTE_PARAMETER);
  48.         }
  49.         // Удаляем параметры, начинающиеся с "_", чтобы они не попали в query string
  50.         $parameters array_filter($parameters, function ($paramName): bool {
  51.             return '_' !== substr($paramName01);
  52.         }, ARRAY_FILTER_USE_KEY);
  53.         if (!$this->features->canonical_url_with_pagination()) {
  54.             // На страницах пагинации для canonical выдаем адрес первой страницы
  55.             $parameters['page'] = 1;
  56.             if (false !== strpos($routeNameDynamicRouter::PAGINATION_ROUTE_POSTFIX)) {
  57.                 $routeName str_replace(DynamicRouter::PAGINATION_ROUTE_POSTFIX''$routeName);
  58.             }
  59.         }
  60.         if (null !== $host) {
  61.             $context $this->urlGenerator->getContext();
  62.             $prevHost $context->getHost();
  63.             $context->setHost($host);
  64.             $url $this->urlGenerator->generate($routeName$parametersUrlGeneratorInterface::ABSOLUTE_URL);
  65.             $context->setHost($prevHost);
  66.         } else {
  67.             $url $this->urlGenerator->generate($routeName$parametersUrlGeneratorInterface::ABSOLUTE_PATH);
  68.         }
  69.         return strtolower($url);
  70.     }
  71. }