View source
<?php
namespace Drupal\multiversion;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteProvider as CoreRouteProvider;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Database\Connection;
class RouteProvider extends CoreRouteProvider {
private $workspaceManager;
public function __construct(Connection $connection, StateInterface $state, CurrentPathStack $current_path, CacheBackendInterface $cache_backend, InboundPathProcessorInterface $path_processor, CacheTagsInvalidatorInterface $cache_tag_invalidator, $table = 'router', LanguageManagerInterface $language_manager = NULL, WorkspaceManagerInterface $workspace_manager) {
if (floatval(\Drupal::VERSION) < 8.5) {
parent::__construct($connection, $state, $current_path, $cache_backend, $path_processor, $cache_tag_invalidator, $table);
}
else {
parent::__construct($connection, $state, $current_path, $cache_backend, $path_processor, $cache_tag_invalidator, $table, $language_manager);
}
$this->workspaceManager = $workspace_manager;
}
public function getRouteCollectionForRequest(Request $request) {
$workspace_id = $this->workspaceManager
->getActiveWorkspaceId();
if (!method_exists($this, 'getCurrentLanguageCacheIdPart')) {
$cid = 'route:' . "workspace{$workspace_id}:" . $request
->getPathInfo() . ':' . $request
->getQueryString();
}
else {
$language_part = $this
->getCurrentLanguageCacheIdPart();
$cid = 'route:' . "workspace{$workspace_id}:" . "{$language_part}:" . $request
->getPathInfo() . ':' . $request
->getQueryString();
}
if ($cached = $this->cache
->get($cid)) {
$this->currentPath
->setPath($cached->data['path'], $request);
$request->query
->replace($cached->data['query']);
return $cached->data['routes'];
}
else {
$path = $request
->getPathInfo();
$path = $path === '/' ? $path : rtrim($request
->getPathInfo(), '/');
$path = $this->pathProcessor
->processInbound($path, $request);
$this->currentPath
->setPath($path, $request);
$query_parameters = $request->query
->all();
$routes = $this
->getRoutesByPath(rtrim($path, '/'));
$cache_value = [
'path' => $path,
'query' => $query_parameters,
'routes' => $routes,
];
$this->cache
->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, [
'route_match',
]);
return $routes;
}
}
}