8a96b08df1ad1c684eeef02202cc191d879df2e2
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Theme / ThemeNegotiatorWrapper.php
1 <?php
2
3 namespace Drupal\webprofiler\Theme;
4
5 use Drupal\Core\Routing\RouteMatchInterface;
6 use Drupal\Core\Theme\ThemeNegotiator;
7
8 /**
9  * Class ThemeNegotiatorWrapper
10  */
11 class ThemeNegotiatorWrapper extends ThemeNegotiator {
12
13   /**
14    * @var \Drupal\Core\Theme\ThemeNegotiatorInterface
15    */
16   private $negotiator;
17
18   /**
19    * {@inheritdoc}
20    */
21   public function determineActiveTheme(RouteMatchInterface $route_match) {
22     // This method has changed in Drupal 8.4.x, to maintain compatibility with
23     // Drupal 8.3.x we check the existence or not of the classResolver
24     // property.
25     // TODO: remove this logic when we decide to drop Drupal 8.3.x support.
26     if (property_exists($this, 'classResolver')) {
27       $classResolver = $this->classResolver;
28       $negotiators = $this->negotiators;
29     } else {
30       $classResolver = \Drupal::classResolver();
31       $negotiators = $this->getSortedNegotiators();
32     }
33
34     foreach ($negotiators as $negotiator_id) {
35       if (property_exists($this, 'classResolver')) {
36         $negotiator = $classResolver->getInstanceFromDefinition($negotiator_id);
37       } else {
38         $negotiator = $negotiator_id;
39       }
40
41       if ($negotiator->applies($route_match)) {
42         $theme = $negotiator->determineActiveTheme($route_match);
43         if ($theme !== NULL && $this->themeAccess->checkAccess($theme)) {
44           $this->negotiator = $negotiator;
45           return $theme;
46         }
47       }
48     }
49   }
50
51   /**
52    * @return \Drupal\Core\Theme\ThemeNegotiatorInterface
53    */
54   public function getNegotiator() {
55     return $this->negotiator;
56   }
57
58   /**
59    * Returns the sorted array of theme negotiators.
60    *
61    * @return array|\Drupal\Core\Theme\ThemeNegotiatorInterface[]
62    *   An array of theme negotiator objects.
63    *
64    * TODO: remove this method when we decide to drop Drupal 8.3.x support.
65    */
66   protected function getSortedNegotiators() {
67     if (!isset($this->sortedNegotiators)) {
68       // Sort the negotiators according to priority.
69       krsort($this->negotiators);
70       // Merge nested negotiators from $this->negotiators into
71       // $this->sortedNegotiators.
72       $this->sortedNegotiators = [];
73       foreach ($this->negotiators as $builders) {
74         $this->sortedNegotiators = array_merge($this->sortedNegotiators, $builders);
75       }
76     }
77     return $this->sortedNegotiators;
78   }
79
80 }