WorkspaceManager.php in Multiversion 8
File
src/Workspace/WorkspaceManager.php
View source
<?php
namespace Drupal\multiversion\Workspace;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\InvalidParameterException;
class WorkspaceManager implements WorkspaceManagerInterface, ContainerAwareInterface {
use StringTranslationTrait;
use ContainerAwareTrait;
protected $requestStack;
protected $entityTypeManager;
protected $currentUser;
protected $negotiators = [];
protected $sortedNegotiators;
protected $logger;
public function __construct(RequestStack $request_stack, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, LoggerInterface $logger = NULL) {
$this->requestStack = $request_stack;
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->logger = $logger ?: new NullLogger();
}
public function addNegotiator(WorkspaceNegotiatorInterface $negotiator, $priority) {
$this->negotiators[$priority][] = $negotiator;
$this->sortedNegotiators = NULL;
}
public function load($workspace_id) {
return $this->entityTypeManager
->getStorage('workspace')
->load($workspace_id);
}
public function loadMultiple(array $workspace_ids = NULL) {
return $this->entityTypeManager
->getStorage('workspace')
->loadMultiple($workspace_ids);
}
public function loadByMachineName($machine_name) {
$workspaces = $this->entityTypeManager
->getStorage('workspace')
->loadByProperties([
'machine_name' => $machine_name,
]);
return current($workspaces);
}
public function getActiveWorkspace() {
$workspace_id = $this
->getActiveWorkspaceId();
if ($workspace = $this
->load($workspace_id)) {
return $workspace;
}
}
public function getActiveWorkspaceId() {
$request = $this->requestStack
->getCurrentRequest();
if (empty($request)) {
return $this->container
->getParameter('workspace.default');
}
foreach ($this
->getSortedNegotiators() as $negotiator) {
if ($negotiator
->applies($request)) {
if ($workspace_id = $negotiator
->getWorkspaceId($request)) {
return $workspace_id;
}
}
}
}
public function setActiveWorkspace(WorkspaceInterface $workspace) {
if (!$workspace
->isPublished()) {
$this->logger
->error('The workspace {workspace} has been archived.', [
'workspace' => $workspace
->label(),
]);
throw new InvalidParameterException('Archived workspaces cannot be set as the active workspace.');
}
if (!$workspace
->access('view') && !$workspace
->isDefaultWorkspace()) {
$this->logger
->error('Denied access to view workspace {workspace}', [
'workspace' => $workspace
->label(),
]);
throw new WorkspaceAccessException('The user does not have permission to view that workspace.');
}
$request = $this->requestStack
->getCurrentRequest();
foreach ($this
->getSortedNegotiators() as $negotiator) {
if ($negotiator
->applies($request)) {
$negotiator
->persist($workspace);
break;
}
}
$this->entityTypeManager
->clearCachedDefinitions();
return $this;
}
protected function getSortedNegotiators() {
if (!isset($this->sortedNegotiators)) {
krsort($this->negotiators);
$this->sortedNegotiators = [];
foreach ($this->negotiators as $builders) {
$this->sortedNegotiators = array_merge($this->sortedNegotiators, $builders);
}
}
return $this->sortedNegotiators;
}
}