vendor/wearemarketing/routingbundle/Sitemap/ChainAlternatesGenerator.php line 79

Open in your IDE?
  1. <?php
  2. namespace WAM\Bundle\RoutingBundle\Sitemap;
  3. use WAM\Bundle\RoutingBundle\Sitemap\Interfaces\AlternatesGeneratorInterface;
  4. /**
  5.  * Class ChainAlternateRoutesGenerator.
  6.  *
  7.  * @author Santiago GarcĂ­a <sangarbe@gmail.com>
  8.  */
  9. class ChainAlternatesGenerator implements AlternatesGeneratorInterface
  10. {
  11.     /**
  12.      * @var array
  13.      */
  14.     private $generators;
  15.     /**
  16.      * @var array
  17.      */
  18.     private $sortedGenerators;
  19.     /**
  20.      * AlternateRoutesGeneratorChain constructor.
  21.      */
  22.     public function __construct()
  23.     {
  24.         $this->generators = [];
  25.         $this->sortedGenerators = [];
  26.     }
  27.     public function add(
  28.         AlternatesGeneratorInterface $alternateRoutesGenerator,
  29.         int $priority 100
  30.     )
  31.     {
  32.         if (!isset($this->generators[$priority])) {
  33.             $this->generators[$priority] = [];
  34.         }
  35.         $this->generators[$priority][] = $alternateRoutesGenerator;
  36.         $this->sortedGenerators = [];
  37.     }
  38.     /**
  39.      * @return AlternatesGeneratorInterface[]
  40.      */
  41.     private function all()
  42.     {
  43.         if (!empty($this->sortedGenerators)) {
  44.             return $this->sortedGenerators;
  45.         }
  46.         $this->sortedGenerators $this->sortGenerators();
  47.         return $this->sortedGenerators;
  48.     }
  49.     private function sortGenerators()
  50.     {
  51.         if (empty($this->generators)) {
  52.             return [];
  53.         }
  54.         krsort($this->generators);
  55.         return array_merge(...$this->generators);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function getAlternateRoutes($route$excludeCurrent true)
  61.     {
  62.         foreach ($this->all() as $generator) {
  63.             if ($generator->handles($route)) {
  64.                 return $generator->getAlternateRoutes($route);
  65.             }
  66.         }
  67.         return [];
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function handles($route)
  73.     {
  74.         foreach ($this->all() as $generator) {
  75.             if ($generator->handles($route)) {
  76.                 return true;
  77.             }
  78.         }
  79.         return false;
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function isSitemapRoute($route$locale)
  85.     {
  86.         foreach ($this->all() as $generator) {
  87.             if ($generator->handles($route)) {
  88.                 return $generator->isSitemapRoute($route$locale);
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function getMainRouteUrl($route$locale)
  97.     {
  98.         foreach ($this->all() as $generator) {
  99.             if ($generator->handles($route)) {
  100.                 return $generator->getMainRouteUrl($route$locale);
  101.             }
  102.         }
  103.         return '';
  104.     }
  105. }