vendor/numero2/contao-marketing-suite/src/Routing/LinkShortenerRouteProvider.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * Copyright (c) 2005-2020 Leo Feyer
  6.  *
  7.  * @package   Contao Marketing Suite
  8.  * @author    Benny Born <benny.born@numero2.de>
  9.  * @author    Michael Bösherz <michael.boesherz@numero2.de>
  10.  * @license   Commercial
  11.  * @copyright 2020 numero2 - Agentur für digitales Marketing
  12.  */
  13. namespace numero2\MarketingSuiteBundle\Routing;
  14. use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
  15. use numero2\MarketingSuite\LinkShortenerModel;
  16. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  17. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  20. use Symfony\Component\Routing\Route;
  21. use Symfony\Component\Routing\RouteCollection;
  22. class LinkShortenerRouteProvider implements RouteProviderInterface {
  23.     /**
  24.      * @var ContaoFrameworkInterface
  25.      */
  26.     private $framework;
  27.     public function __constructContaoFrameworkInterface $framework ) {
  28.         $this->framework $framework;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getRouteCollectionForRequest(Request $request) {
  34.         if( !$this->framework->isInitialized() ) {
  35.             $this->framework->initialize(true);
  36.         }
  37.         $alias urldecode(substr($request->getPathInfo(), 1));
  38.         $oLink NULL;
  39.         $oLink LinkShortenerModel::findOneByPath($alias);
  40.         $collection NULL;
  41.         $collection = new RouteCollection();
  42.         if( $oLink ) {
  43.             foreach( ['prefix''alias'] as $field ) {
  44.                 if( !strlen($oLink->$field) ) {
  45.                     continue;
  46.                 }
  47.                 $route = new Route("/".$oLink->$field);
  48.                 $route->setDefault(RouteObjectInterface::CONTROLLER_NAME'marketing_suite.controller.link_shortener');
  49.                 $route->setDefault(RouteObjectInterface::CONTENT_OBJECT$oLink);
  50.                 $route->setHost($oLink->domain);
  51.                 // only add route if target is set
  52.                 if( $oLink->getTarget() ) {
  53.                     $collection->add("link_shortener.".$oLink->$field.".".$oLink->id$route);
  54.                 }
  55.             }
  56.         }
  57.         return $collection;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function getRouteByName($name) {
  63.         throw new RouteNotFoundException('This router does not support routes by name');
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function getRoutesByNames($names) {
  69.         return [];
  70.     }
  71. }