CheckProvider.php in Drupal 8
File
core/lib/Drupal/Core/Access/CheckProvider.php
View source
<?php
namespace Drupal\Core\Access;
use Drupal\Core\Routing\Access\AccessInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class CheckProvider implements CheckProviderInterface, ContainerAwareInterface {
use ContainerAwareTrait;
protected $checkIds = [];
protected $checks;
protected $checkMethods = [];
protected $checksNeedsRequest = [];
protected $staticRequirementMap;
protected $dynamicRequirementMap;
public function addCheckService($service_id, $service_method, array $applies_checks = [], $needs_incoming_request = FALSE) {
$this->checkIds[] = $service_id;
$this->checkMethods[$service_id] = $service_method;
if ($needs_incoming_request) {
$this->checksNeedsRequest[$service_id] = $service_id;
}
foreach ($applies_checks as $applies_check) {
$this->staticRequirementMap[$applies_check][] = $service_id;
}
}
public function getChecksNeedRequest() {
return $this->checksNeedsRequest;
}
public function setChecks(RouteCollection $routes) {
$this
->loadDynamicRequirementMap();
foreach ($routes as $route) {
if ($checks = $this
->applies($route)) {
$route
->setOption('_access_checks', $checks);
}
}
}
public function loadCheck($service_id) {
if (empty($this->checks[$service_id])) {
if (!in_array($service_id, $this->checkIds)) {
throw new \InvalidArgumentException(sprintf('No check has been registered for %s', $service_id));
}
$check = $this->container
->get($service_id);
if (!$check instanceof AccessInterface) {
throw new AccessException('All access checks must implement AccessInterface.');
}
if (!is_callable([
$check,
$this->checkMethods[$service_id],
])) {
throw new AccessException(sprintf('Access check method %s in service %s must be callable.', $this->checkMethods[$service_id], $service_id));
}
$this->checks[$service_id] = $check;
}
return [
$this->checks[$service_id],
$this->checkMethods[$service_id],
];
}
protected function applies(Route $route) {
$checks = [];
foreach ($route
->getRequirements() as $key => $value) {
if (isset($this->staticRequirementMap[$key])) {
foreach ($this->staticRequirementMap[$key] as $service_id) {
$checks[] = $service_id;
}
}
}
foreach ($this->dynamicRequirementMap as $service_id) {
if ($this->checks[$service_id]
->applies($route)) {
$checks[] = $service_id;
}
}
return $checks;
}
protected function loadDynamicRequirementMap() {
if (isset($this->dynamicRequirementMap)) {
return;
}
$this->dynamicRequirementMap = [];
foreach ($this->checkIds as $service_id) {
if (empty($this->checks[$service_id])) {
$this
->loadCheck($service_id);
}
if ($this->checks[$service_id] instanceof AccessCheckInterface) {
$this->dynamicRequirementMap[] = $service_id;
}
}
}
}