You are here

class WorkspaceManager in Multiversion 8

Hierarchy

Expanded class hierarchy of WorkspaceManager

1 file declares its use of WorkspaceManager
WorkspaceManagerTest.php in tests/src/Unit/WorkspaceManagerTest.php
1 string reference to 'WorkspaceManager'
multiversion.services.yml in ./multiversion.services.yml
multiversion.services.yml
1 service uses WorkspaceManager
workspace.manager in ./multiversion.services.yml
Drupal\multiversion\Workspace\WorkspaceManager

File

src/Workspace/WorkspaceManager.php, line 16

Namespace

Drupal\multiversion\Workspace
View source
class WorkspaceManager implements WorkspaceManagerInterface, ContainerAwareInterface {
  use StringTranslationTrait;
  use ContainerAwareTrait;

  /**
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * @var array
   */
  protected $negotiators = [];

  /**
   * @var array
   */
  protected $sortedNegotiators;

  /**
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   * @param \Psr\Log\LoggerInterface $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();
  }

  /**
   * {@inheritdoc}
   */
  public function addNegotiator(WorkspaceNegotiatorInterface $negotiator, $priority) {
    $this->negotiators[$priority][] = $negotiator;
    $this->sortedNegotiators = NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function load($workspace_id) {
    return $this->entityTypeManager
      ->getStorage('workspace')
      ->load($workspace_id);
  }

  /**
   * {@inheritdoc}
   */
  public function loadMultiple(array $workspace_ids = NULL) {
    return $this->entityTypeManager
      ->getStorage('workspace')
      ->loadMultiple($workspace_ids);
  }

  /**
   * {@inheritdoc}
   */
  public function loadByMachineName($machine_name) {
    $workspaces = $this->entityTypeManager
      ->getStorage('workspace')
      ->loadByProperties([
      'machine_name' => $machine_name,
    ]);
    return current($workspaces);
  }

  /**
   * {@inheritdoc}
   *
   * @todo {@link https://www.drupal.org/node/2600382 Access check.}
   */
  public function getActiveWorkspace() {
    $workspace_id = $this
      ->getActiveWorkspaceId();
    if ($workspace = $this
      ->load($workspace_id)) {
      return $workspace;
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveWorkspace(WorkspaceInterface $workspace) {

    // Unpublished workspaces should not be allowed to be active.
    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 the current user doesn't have access to view the workspace, they
    // shouldn't be allowed to switch to it.
    // @todo Could this be handled better?
    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.');
    }

    // Set the workspace on the proper negotiator.
    $request = $this->requestStack
      ->getCurrentRequest();
    foreach ($this
      ->getSortedNegotiators() as $negotiator) {
      if ($negotiator
        ->applies($request)) {
        $negotiator
          ->persist($workspace);
        break;
      }
    }

    // Clear cached entity storage handlers
    $this->entityTypeManager
      ->clearCachedDefinitions();
    return $this;
  }

  /**
   * @return \Drupal\multiversion\Workspace\WorkspaceNegotiatorInterface[]
   */
  protected function getSortedNegotiators() {
    if (!isset($this->sortedNegotiators)) {

      // Sort the negotiators according to priority.
      krsort($this->negotiators);

      // Merge nested negotiators from $this->negotiators into
      // $this->sortedNegotiators.
      $this->sortedNegotiators = [];
      foreach ($this->negotiators as $builders) {
        $this->sortedNegotiators = array_merge($this->sortedNegotiators, $builders);
      }
    }
    return $this->sortedNegotiators;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkspaceManager::$currentUser protected property
WorkspaceManager::$entityTypeManager protected property
WorkspaceManager::$logger protected property
WorkspaceManager::$negotiators protected property
WorkspaceManager::$requestStack protected property
WorkspaceManager::$sortedNegotiators protected property
WorkspaceManager::addNegotiator public function Overrides WorkspaceManagerInterface::addNegotiator
WorkspaceManager::getActiveWorkspace public function @todo {@link https://www.drupal.org/node/2600382 Access check.} Overrides WorkspaceManagerInterface::getActiveWorkspace
WorkspaceManager::getActiveWorkspaceId public function Fetches the currently active workspace ID. Overrides WorkspaceManagerInterface::getActiveWorkspaceId
WorkspaceManager::getSortedNegotiators protected function
WorkspaceManager::load public function Overrides WorkspaceManagerInterface::load
WorkspaceManager::loadByMachineName public function Overrides WorkspaceManagerInterface::loadByMachineName
WorkspaceManager::loadMultiple public function Overrides WorkspaceManagerInterface::loadMultiple
WorkspaceManager::setActiveWorkspace public function Sets the active workspace for the site/session. Overrides WorkspaceManagerInterface::setActiveWorkspace
WorkspaceManager::__construct public function