PathCondition.php in Access Filter 8
File
src/Plugin/AccessFilter/Condition/PathCondition.php
View source
<?php
namespace Drupal\access_filter\Plugin\AccessFilter\Condition;
use Drupal\Core\Path\PathMatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PathCondition extends ConditionBase {
protected $pathMatcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PathMatcherInterface $path_matcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->pathMatcher = $path_matcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('path.matcher'));
}
public function summary() {
$regex = !empty($this->configuration['regex']) ? '<span class="regex">[Regex]</span>' : '';
return $this->configuration['path'] . $regex;
}
public function validateConfiguration(array $configuration) {
$errors = [];
if (!isset($configuration['path']) || !strlen($configuration['path'])) {
$errors[] = $this
->t("'@property' is required.", [
'@property' => 'path',
]);
}
return $errors;
}
public function isMatched(Request $request) {
$path = $request
->getPathInfo();
if (empty($this->configuration['regex'])) {
return $this->pathMatcher
->matchPath($path, $this->configuration['path']);
}
else {
return (bool) preg_match($this->configuration['path'], $path);
}
}
}