AccessAwareRouter.php in Drupal 8
File
core/lib/Drupal/Core/Routing/AccessAwareRouter.php
View source
<?php
namespace Drupal\Core\Routing;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext as SymfonyRequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\RouterInterface;
class AccessAwareRouter implements AccessAwareRouterInterface {
protected $router;
protected $accessManager;
protected $account;
public function __construct(RequestMatcherInterface $router, AccessManagerInterface $access_manager, AccountInterface $account) {
$this->router = $router;
$this->accessManager = $access_manager;
$this->account = $account;
}
public function __call($name, $arguments) {
return call_user_func_array([
$this->router,
$name,
], $arguments);
}
public function setContext(SymfonyRequestContext $context) {
if ($this->router instanceof RequestContextAwareInterface) {
$this->router
->setContext($context);
}
}
public function getContext() {
if ($this->router instanceof RequestContextAwareInterface) {
return $this->router
->getContext();
}
}
public function matchRequest(Request $request) {
$parameters = $this->router
->matchRequest($request);
$request->attributes
->add($parameters);
$this
->checkAccess($request);
return $request->attributes
->all();
}
protected function checkAccess(Request $request) {
$access_result = $this->accessManager
->checkRequest($request, $this->account, TRUE);
if (!$request->attributes
->has(AccessAwareRouterInterface::ACCESS_RESULT)) {
$request->attributes
->set(AccessAwareRouterInterface::ACCESS_RESULT, $access_result);
}
if (!$access_result
->isAllowed()) {
if ($access_result instanceof CacheableDependencyInterface && $request
->isMethodCacheable()) {
throw new CacheableAccessDeniedHttpException($access_result, $access_result instanceof AccessResultReasonInterface ? $access_result
->getReason() : NULL);
}
else {
throw new AccessDeniedHttpException($access_result instanceof AccessResultReasonInterface ? $access_result
->getReason() : NULL);
}
}
}
public function getRouteCollection() {
if ($this->router instanceof RouterInterface) {
return $this->router
->getRouteCollection();
}
}
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH) {
if ($this->router instanceof UrlGeneratorInterface) {
return $this->router
->generate($name, $parameters, $referenceType);
}
}
public function match($pathinfo) {
return $this
->matchRequest(Request::create($pathinfo));
}
}