src/Service/BlockPathService.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Block;
  4. use App\Entity\Stop;
  5. use App\Repository\BlockRepository;
  6. use App\Repository\StopRepository;
  7. use App\Utils\StringUtils;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  11. use Symfony\Component\Routing\RouterInterface;
  12. class BlockPathService
  13. {
  14.     protected StopRepository $stopRepo;
  15.     protected BlockRepository $blockRepo;
  16.     private RouterInterface $router;
  17.     protected string $domainprefix;
  18.     public function __construct(EntityManagerInterface $entityManagerRouterInterface $routerstring $domainprefix) {
  19.         $this->domainprefix $domainprefix;
  20.         $this->router $router;
  21.         $this->stopRepo $entityManager->getRepository(Stop::class);
  22.         $this->blockRepo $entityManager->getRepository(Block::class);
  23.     }
  24.     public function getBlock(Request $request): ?Block {
  25.         $path $request->getPathInfo();
  26.         $host $request->getHost();
  27.         $host ltrim($host$this->domainprefix);
  28.         return $this->blockRepo->findOneByPathIgnoreEndingSlash($path$host);
  29.     }
  30.     /**
  31.      * Used in admin, to get informatin about the route
  32.      * From block.path, get intern route configuration and labels of ids
  33.      * @param $blockPath /fr/compagnie-de-bus/budgetbus or www.comparabus.com/fr/compagnie-de-bus/budgetbus (never starts with local- or test-)
  34.      */
  35.     public function getRoute(string $blockPath) : ?array {
  36.         $blockPath strtok($blockPath"#"); //remove after #
  37.         try{
  38.             $backupHost $this->router->getContext()->getHost();
  39.             [$host$pathinfo] = self::extractDomainFromPath($blockPath); //to have stuff cleaner, will must probably always get the host set
  40.             if($host$this->router->getContext()->setHost($this->domainprefix.$host);
  41.             $match $this->router->match($pathinfo);
  42.             if($host$this->router->getContext()->setHost($backupHost); //otherwise if done in a loop, will use last host when path starts with '/'
  43.             // in debug put ids label
  44.             $debug '';
  45.             $debug .= isset($match['depStopId']) ? $this->stopRepo->find($match['depStopId'])->getName().' ' '';
  46.             $debug .= isset($match['arrStopId']) ? $this->stopRepo->find($match['arrStopId'])->getName().' ' '';
  47.             $debug .= isset($match['stopId']) ? $this->stopRepo->find($match['stopId'])->getName() : '';
  48.             $match['_cbdebug']=$debug;
  49.             return $match;
  50.         } catch (ResourceNotFoundException $e) {}
  51.         return null;
  52.     }
  53.     static public function extractDomainFromPath(string $url): array {
  54.         if (!StringUtils::startsWith($url'www')) return [null$url];
  55.         if (!StringUtils::contains($url'/')) return [$url''];
  56.         $host StringUtils::substringBefore($url'/');
  57.         $url '/'.StringUtils::substringAfter($url'/');
  58.         return [$host$url];
  59.     }
  60.     public function decodeUrl(string $pathinfo): array {
  61.         $routeParam $this->router->match($pathinfo);
  62.         if (!isset($routeParam['_route'])) throw new \Exception("_route missing for path: ".$pathinfo);
  63.         $route $routeParam['_route'];
  64.         $conveyance self::routeNameToConveyance($route);
  65.         if ($conveyance) {
  66.             return [
  67.                 'depStopId' => $routeParam['depStopId'],
  68.                 'arrStopId' => $routeParam['arrStopId'],
  69.                 'conveyance' => $conveyance,
  70.                 'locale' => $routeParam['_locale'],
  71.                 'path' => null,
  72.             ];
  73.         } else {
  74.             return ['path' => $pathinfo];
  75.         }
  76.     }
  77. }