Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Routing;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface;
|
Chris@0
|
6 use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface as BaseRouteEnhancerInterface;
|
Chris@0
|
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
Chris@0
|
8 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
Chris@0
|
9 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
Chris@0
|
10 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
11 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * A route enhancer which lazily loads route enhancers, depending on the route.
|
Chris@0
|
15 *
|
Chris@0
|
16 * We lazy initialize route enhancers, because otherwise all dependencies of
|
Chris@0
|
17 * all route enhancers are initialized on every request, which is slow. However,
|
Chris@0
|
18 * with the use of lazy loading, dependencies are instantiated only when used.
|
Chris@0
|
19 */
|
Chris@0
|
20 class LazyRouteEnhancer implements BaseRouteEnhancerInterface, ContainerAwareInterface {
|
Chris@0
|
21
|
Chris@0
|
22 use ContainerAwareTrait;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Array of enhancers service IDs.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $serviceIds = [];
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The initialized route enhancers.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $enhancers = NULL;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Constructs the LazyRouteEnhancer object.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param $service_ids
|
Chris@0
|
42 * Array of enhancers service IDs.
|
Chris@0
|
43 */
|
Chris@0
|
44 public function __construct($service_ids) {
|
Chris@0
|
45 $this->serviceIds = $service_ids;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * For each route, saves a list of applicable enhancers to the route.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param \Symfony\Component\Routing\RouteCollection $route_collection
|
Chris@0
|
52 * A collection of routes to apply enhancer checks to.
|
Chris@0
|
53 */
|
Chris@0
|
54 public function setEnhancers(RouteCollection $route_collection) {
|
Chris@0
|
55 /** @var \Symfony\Component\Routing\Route $route **/
|
Chris@0
|
56 foreach ($route_collection as $route_name => $route) {
|
Chris@0
|
57 $service_ids = [];
|
Chris@0
|
58 foreach ($this->getEnhancers() as $service_id => $enhancer) {
|
Chris@0
|
59 if ((!$enhancer instanceof RouteEnhancerInterface) || $enhancer->applies($route)) {
|
Chris@0
|
60 $service_ids[] = $service_id;
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63 if ($service_ids) {
|
Chris@0
|
64 $route->setOption('_route_enhancers', array_unique($service_ids));
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * For each route, gets a list of applicable enhancer to the route.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
|
Chris@0
|
73 */
|
Chris@0
|
74 protected function getEnhancers() {
|
Chris@0
|
75 if (!isset($this->enhancers)) {
|
Chris@0
|
76 foreach ($this->serviceIds as $service_id) {
|
Chris@0
|
77 $this->enhancers[$service_id] = $this->container->get($service_id);
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80 return $this->enhancers;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function enhance(array $defaults, Request $request) {
|
Chris@0
|
87 /** @var \Symfony\Component\Routing\Route $route */
|
Chris@0
|
88 $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
|
Chris@0
|
89
|
Chris@0
|
90 $enhancer_ids = $route->getOption('_route_enhancers');
|
Chris@0
|
91
|
Chris@0
|
92 if (isset($enhancer_ids)) {
|
Chris@0
|
93 foreach ($enhancer_ids as $enhancer_id) {
|
Chris@0
|
94 $defaults = $this->container->get($enhancer_id)->enhance($defaults, $request);
|
Chris@0
|
95 }
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return $defaults;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|