You are here

public function CheckProvider::loadCheck in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Access/CheckProvider.php \Drupal\Core\Access\CheckProvider::loadCheck()

Lazy-loads access check services.

Parameters

string $service_id: The service id of the access check service to load.

Return value

callable A callable access check.

Throws

\InvalidArgumentException Thrown when the service hasn't been registered in addCheckService().

\Drupal\Core\Access\AccessException Thrown when the service doesn't implement the required interface.

Overrides CheckProviderInterface::loadCheck

1 call to CheckProvider::loadCheck()
CheckProvider::applies in core/lib/Drupal/Core/Access/CheckProvider.php
Determine which registered access checks apply to a route.

File

core/lib/Drupal/Core/Access/CheckProvider.php, line 94

Class

CheckProvider
Loads access checkers from the container.

Namespace

Drupal\Core\Access

Code

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],
  ];
}