vendor/symfony-cmf/routing/src/Enhancer/RouteEnhancerTrait.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony CMF package.
  4.  *
  5.  * (c) Symfony CMF
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Cmf\Component\Routing\Enhancer;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13.  * Functionality to collect and apply route enhancers to a request.
  14.  *
  15.  * @author Tim Plunkett
  16.  * @author Larry Garfield
  17.  * @author David Buchmann
  18.  */
  19. trait RouteEnhancerTrait
  20. {
  21.     /**
  22.      * @var RouteEnhancerInterface[][]
  23.      */
  24.     private $enhancers = [];
  25.     /**
  26.      * Cached sorted list of enhancers.
  27.      *
  28.      * @var RouteEnhancerInterface[]
  29.      */
  30.     private $sortedEnhancers = [];
  31.     /**
  32.      * Apply the route enhancers to the defaults, according to priorities.
  33.      *
  34.      * @param array $defaults
  35.      *
  36.      * @return array
  37.      */
  38.     protected function applyRouteEnhancers($defaultsRequest $request)
  39.     {
  40.         foreach ($this->getRouteEnhancers() as $enhancer) {
  41.             $defaults $enhancer->enhance($defaults$request);
  42.         }
  43.         return $defaults;
  44.     }
  45.     /**
  46.      * Add route enhancers to the router to let them generate information on
  47.      * matched routes.
  48.      *
  49.      * The order of the enhancers is determined by the priority, the higher the
  50.      * value, the earlier the enhancer is run.
  51.      *
  52.      * @param int $priority
  53.      *
  54.      * @return self
  55.      */
  56.     public function addRouteEnhancer(RouteEnhancerInterface $enhancer$priority 0)
  57.     {
  58.         if (empty($this->enhancers[$priority])) {
  59.             $this->enhancers[$priority] = [];
  60.         }
  61.         $this->enhancers[$priority][] = $enhancer;
  62.         $this->sortedEnhancers = [];
  63.         return $this;
  64.     }
  65.     /**
  66.      * Sorts the enhancers and flattens them.
  67.      *
  68.      * @return RouteEnhancerInterface[] the enhancers ordered by priority
  69.      */
  70.     public function getRouteEnhancers()
  71.     {
  72.         if (=== count($this->sortedEnhancers)) {
  73.             $this->sortedEnhancers $this->sortRouteEnhancers();
  74.         }
  75.         return $this->sortedEnhancers;
  76.     }
  77.     /**
  78.      * Sort enhancers by priority.
  79.      *
  80.      * The highest priority number is the highest priority (reverse sorting).
  81.      *
  82.      * @return RouteEnhancerInterface[] the sorted enhancers
  83.      */
  84.     private function sortRouteEnhancers()
  85.     {
  86.         if (=== count($this->enhancers)) {
  87.             return [];
  88.         }
  89.         krsort($this->enhancers);
  90.         return call_user_func_array('array_merge'$this->enhancers);
  91.     }
  92. }