context_suite-8.x-1.0-beta1/src/Plugin/Condition/RequestPathRegex.php
src/Plugin/Condition/RequestPathRegex.php
<?php namespace Drupal\context_suite\Plugin\Condition; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Path\AliasManagerInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RequestStack; /** * Provides a 'Request Path Regex' condition. * * @Condition( * id = "request_path_regex", * label = @Translation("Request Path Regex"), * ) */ class RequestPathRegex extends ConditionPluginBase implements ContainerFactoryPluginInterface { /** * The default front page. * * @var string */ protected $frontPage; /** * An alias manager to find the alias for the current system path. * * @var \Drupal\Core\Path\AliasManagerInterface */ protected $aliasManager; /** * The path matcher. * * @var \Drupal\Core\Path\PathMatcherInterface */ protected $pathMatcher; /** * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack */ protected $requestStack; /** * The current path. * * @var \Drupal\Core\Path\CurrentPathStack */ protected $currentPath; /** * The cache of regular expressions. * * @var array */ protected $regexes; /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * Constructs a RequestPath condition plugin. * * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager * An alias manager to find the alias for the current system path. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher * The path matcher service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Path\CurrentPathStack $current_path * The current path. * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ public function __construct(AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, RequestStack $request_stack, CurrentPathStack $current_path, array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->aliasManager = $alias_manager; $this->pathMatcher = $path_matcher; $this->requestStack = $request_stack; $this->currentPath = $current_path; $this->configFactory = $config_factory; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $container->get('path.alias_manager'), $container->get('path.matcher'), $container->get('request_stack'), $container->get('path.current'), $configuration, $plugin_id, $plugin_definition, $container->get('config.factory') ); } /** * {@inheritdoc} */ public function defaultConfiguration() { return ['pages' => ''] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['pages'] = [ '#type' => 'textarea', '#title' => $this->t('Pages'), '#default_value' => $this->configuration['pages'], '#description' => $this->t("Specify pages by using their paths regular expression. Enter one path per line.<br>An example path is <strong>@user-wildcard</strong> for every user page.<br> %front is the front page.", [ '@user-wildcard' => '\/user\/([0-9]+)', '%front' => '<front>', ]), ]; return parent::buildConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $this->configuration['pages'] = $form_state->getValue('pages'); parent::submitConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function summary() { $pages = array_map('trim', explode("\n", $this->configuration['pages'])); $pages = implode(', ', $pages); if (!empty($this->configuration['negate'])) { return $this->t('Do not return true on the following pages: @pages', ['@pages' => $pages]); } return $this->t('Return true on the following pages: @pages', ['@pages' => $pages]); } /** * {@inheritdoc} */ public function evaluate() { // Convert path to lowercase. This allows comparison of the same path // with different case. Ex: /Page, /page, /PAGE. $pages = mb_strtolower($this->configuration['pages']); if (!$pages) { return TRUE; } $request = $this->requestStack->getCurrentRequest(); // Compare the lowercase path alias (if any) and internal path. $path = $this->currentPath->getPath($request); // Do not trim a trailing slash if that is the complete path. $path = $path === '/' ? $path : rtrim($path, '/'); $path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path)); return $this->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->matchPath($path, $pages)); } /** * Checks if a path matches any pattern in a set of patterns. * * @param string $path * The path to match. * @param string $patterns * A set of patterns separated by a newline. * * @return bool * TRUE if the path matches a pattern, FALSE otherwise. */ public function matchPath($path, $patterns) { if (!isset($this->regexes[$patterns])) { // Convert path settings to a regular expression. $to_replace = [ // Replace newlines with a logical 'or'. '/(\r\n?|\n)/', // Quote <front> keyword. '/(^|\|)\\\\<front\\\\>($|\|)/', ]; $replacements = [ '|', '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2', ]; $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns) . ')$/'; } return (bool) preg_match($this->regexes[$patterns], $path); } /** * Gets the current front page path. * * @return string * The front page path. */ protected function getFrontPagePath() { // Lazy-load front page config. if (!isset($this->frontPage)) { $this->frontPage = $this->configFactory->get('system.site') ->get('page.front'); } return $this->frontPage; } /** * {@inheritdoc} */ public function getCacheContexts() { $contexts = parent::getCacheContexts(); $contexts[] = 'url.path'; return $contexts; } }