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