Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Path / PathMatcher.php
1 <?php
2
3 namespace Drupal\Core\Path;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Routing\RouteMatchInterface;
7 use Drupal\Core\Url;
8
9 /**
10  * Provides a path matcher.
11  */
12 class PathMatcher implements PathMatcherInterface {
13
14   /**
15    * Whether the current page is the front page.
16    *
17    * @var bool
18    */
19   protected $isCurrentFrontPage;
20
21   /**
22    * The default front page.
23    *
24    * @var string
25    */
26   protected $frontPage;
27
28   /**
29    * The cache of regular expressions.
30    *
31    * @var array
32    */
33   protected $regexes;
34
35   /**
36    * The config factory service.
37    *
38    * @var \Drupal\Core\Config\ConfigFactoryInterface
39    */
40   protected $configFactory;
41
42   /**
43    * The current route match.
44    *
45    * @var \Drupal\Core\Routing\RouteMatchInterface
46    */
47   protected $routeMatch;
48
49   /**
50    * Creates a new PathMatcher.
51    *
52    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
53    *   The config factory.
54    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
55    *   The current route match.
56    */
57   public function __construct(ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
58     $this->configFactory = $config_factory;
59     $this->routeMatch = $route_match;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function matchPath($path, $patterns) {
66
67     if (!isset($this->regexes[$patterns])) {
68       // Convert path settings to a regular expression.
69       $to_replace = [
70         // Replace newlines with a logical 'or'.
71         '/(\r\n?|\n)/',
72         // Quote asterisks.
73         '/\\\\\*/',
74         // Quote <front> keyword.
75         '/(^|\|)\\\\<front\\\\>($|\|)/',
76       ];
77       $replacements = [
78         '|',
79         '.*',
80         '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2',
81       ];
82       $patterns_quoted = preg_quote($patterns, '/');
83       $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
84     }
85     return (bool) preg_match($this->regexes[$patterns], $path);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function isFrontPage() {
92     // Cache the result as this is called often.
93     if (!isset($this->isCurrentFrontPage)) {
94       $this->isCurrentFrontPage = FALSE;
95       // Ensure that the code can also be executed when there is no active
96       // route match, like on exception responses.
97       if ($this->routeMatch->getRouteName()) {
98         $url = Url::fromRouteMatch($this->routeMatch);
99         $this->isCurrentFrontPage = ($url->getRouteName() && '/' . $url->getInternalPath() === $this->getFrontPagePath());
100       }
101     }
102     return $this->isCurrentFrontPage;
103   }
104
105   /**
106    * Gets the current front page path.
107    *
108    * @return string
109    *   The front page path.
110    */
111   protected function getFrontPagePath() {
112     // Lazy-load front page config.
113     if (!isset($this->frontPage)) {
114       $this->frontPage = $this->configFactory->get('system.site')
115         ->get('page.front');
116     }
117     return $this->frontPage;
118   }
119
120 }