You are here

class GutenbergLibraryManager in Gutenberg 8.2

Provides the default .gutenberg.yml library plugin manager.

Hierarchy

Expanded class hierarchy of GutenbergLibraryManager

1 string reference to 'GutenbergLibraryManager'
gutenberg.services.yml in ./gutenberg.services.yml
gutenberg.services.yml
1 service uses GutenbergLibraryManager
plugin.manager.gutenberg.library in ./gutenberg.services.yml
Drupal\gutenberg\GutenbergLibraryManager

File

src/GutenbergLibraryManager.php, line 19

Namespace

Drupal\gutenberg
View source
class GutenbergLibraryManager extends DefaultPluginManager implements GutenbergLibraryManagerInterface {

  /**
   * Provides default values for all gutenberg plugins.
   *
   * @var array
   */
  protected $defaults = [
    'libraries-edit' => [],
    'libraries-view' => [],
    'dynamic-blocks' => [],
  ];

  /**
   * The theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * The theme initialization.
   *
   * @var \Drupal\Core\Theme\ThemeInitializationInterface
   */
  protected $themeInitialization;

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

  /**
   * Static cache of theme Gutenberg plugin definitions.
   *
   * @var array
   */
  protected $activeThemeDefinitions;

  /**
   * Static cache of the merged active theme Gutenberg plugin definition.
   *
   * @var array
   */
  protected $activeThemeMergedDefinition;

  /**
   * Static cache of Gutenberg plugin definitions keyed by the extension type.
   *
   * @var array
   */
  protected $definitionsByExtension;

  /* @noinspection MagicMethodsValidityInspection */

  /* @noinspection PhpMissingParentConstructorInspection */

  /**
   * Constructs a new GutenbergManager object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   * @param \Drupal\Core\Theme\ThemeInitializationInterface $theme_initialization
   *   The theme initialization.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Psr\Log\LoggerInterface $logger
   *   Gutenberg logger interface.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeInitializationInterface $theme_initialization, CacheBackendInterface $cache_backend, LoggerInterface $logger) {
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $theme_handler;
    $this->themeInitialization = $theme_initialization;
    $this->logger = $logger;
    $this
      ->alterInfo('gutenberg_info');
    $this
      ->setCacheBackend($cache_backend, 'gutenberg', [
      'gutenberg',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {
      $this->discovery = new YamlDiscovery('gutenberg', $this->moduleHandler
        ->getModuleDirectories() + $this->themeHandler
        ->getThemeDirectories());
    }
    return $this->discovery;
  }

  /**
   * {@inheritdoc}
   */
  protected function findDefinitions() {
    $definitions = $this
      ->getDiscovery()
      ->findAll();
    foreach ($definitions as $plugin_id => &$definition) {
      $definitions[$plugin_id] = $definition + [
        'id' => $plugin_id,
        'provider' => $plugin_id,
      ];
      $this
        ->processDefinition($definition, $plugin_id);
    }
    unset($definition);
    $this
      ->alterDefinitions($definitions);

    // If this plugin was provided by a module/theme that does not exist,
    // remove the plugin definition.
    foreach ($definitions as $plugin_id => $definition) {
      $plugin_id = $this
        ->extractProviderFromDefinition($definition);
      if ($plugin_id && !in_array($plugin_id, [
        'core',
        'component',
      ]) && !$this
        ->providerExists($plugin_id)) {
        unset($definitions[$plugin_id]);
      }
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);
    if (empty($definition['provider'])) {
      throw new InvalidPluginDefinitionException(sprintf('Gutenberg plugin property (%s) definition "provider" is required.', $plugin_id));
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function providerExists($provider) {
    return $this->moduleHandler
      ->moduleExists($provider) || $this->themeHandler
      ->themeExists($provider);
  }

  /**
   * {@inheritdoc}
   */
  public function clearCachedDefinitions() {
    parent::clearCachedDefinitions();
    $this->definitionsByExtension = NULL;
    $this->activeThemeDefinitions = NULL;
    $this->activeThemeMergedDefinition = NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleDefinitions() {
    return $this
      ->getDefinitionsByExtension()['module'];
  }

  /**
   * {@inheritdoc}
   */
  public function getThemeDefinitions() {
    return $this
      ->getDefinitionsByExtension()['theme'];
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveThemeDefinitions() {
    if (isset($this->activeThemeDefinitions)) {
      return $this->activeThemeDefinitions;
    }
    $cid = $this->cacheKey . ':active_themes';
    if ($cache = $this->cacheBackend
      ->get($cid)) {
      return $this->activeThemeDefinitions = $cache->data;
    }
    $theme_name = $this->themeHandler
      ->getDefault();
    $theme_definitions = [];
    try {
      $active_theme = $this->themeInitialization
        ->getActiveThemeByName($theme_name);
      $definitions = $this
        ->getDefinitionsByExtension();

      // Note: Reversing the order so that base themes are first.
      $themes = array_reverse(array_merge([
        $active_theme
          ->getName(),
      ], array_keys($active_theme
        ->getBaseThemeExtensions())));
      foreach ($themes as $theme) {
        if (isset($definitions['theme'][$theme])) {
          $theme_definitions[$theme] = $definitions['theme'][$theme];
        }
      }
    } catch (MissingThemeDependencyException $e) {
      $this->logger
        ->error($e
        ->getMessage());
    }
    $this->cacheBackend
      ->set($cid, $theme_definitions, Cache::PERMANENT, [
      'gutenberg',
    ]);
    $this->activeThemeDefinitions = $theme_definitions;
    return $this->activeThemeDefinitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveThemeMergedDefinition() {
    if (isset($this->activeThemeMergedDefinition)) {
      return $this->activeThemeMergedDefinition;
    }
    $cid = $this->cacheKey . ':active_theme';
    if ($cache = $this->cacheBackend
      ->get($cid)) {
      return $this->activeThemeMergedDefinition = $cache->data;
    }

    // Specify the default definition.
    $definition = $this->defaults;
    foreach ($this
      ->getActiveThemeDefinitions() as $array) {
      foreach ($array as $key => $value) {
        if ($key === 'id' || $key === 'provider') {

          // Ignore irrelevant properties.
          continue;
        }
        if (isset($definition[$key]) && is_array($definition[$key]) && is_array($value)) {

          // Merge arrays.
          $definition[$key] = array_merge($definition[$key], $value);
        }
        else {
          $definition[$key] = $value;
        }
      }
    }
    $this->activeThemeMergedDefinition = $definition;
    $this->cacheBackend
      ->set($cid, $definition, Cache::PERMANENT, [
      'gutenberg',
    ]);
    return $this->activeThemeMergedDefinition;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinitionsByExtension() {
    if (isset($this->definitionsByExtension)) {
      return $this->definitionsByExtension;
    }
    $cid = $this->cacheKey . ':by_extension';
    if ($cache = $this->cacheBackend
      ->get($cid)) {
      return $this->definitionsByExtension = $cache->data;
    }
    $definitions_by_extension = [
      'theme' => [],
      'module' => [],
    ];
    foreach ($this
      ->getDefinitions() as $plugin_id => $plugin_definition) {
      if ($this->themeHandler
        ->themeExists($plugin_id)) {
        $definitions_by_extension['theme'][$plugin_id] = $plugin_definition;
      }
      elseif ($this->moduleHandler
        ->moduleExists($plugin_id)) {
        $definitions_by_extension['module'][$plugin_id] = $plugin_definition;
      }
    }
    $this->cacheBackend
      ->set($cid, $definitions_by_extension, Cache::PERMANENT, [
      'gutenberg',
    ]);
    $this->definitionsByExtension = $definitions_by_extension;
    return $this->definitionsByExtension;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultPluginManager::$additionalAnnotationNamespaces protected property Additional namespaces the annotation discovery mechanism should scan for annotation definitions.
DefaultPluginManager::$alterHook protected property Name of the alter hook if one should be invoked.
DefaultPluginManager::$cacheKey protected property The cache key.
DefaultPluginManager::$cacheTags protected property An array of cache tags to use for the cached definitions.
DefaultPluginManager::$moduleHandler protected property The module handler to invoke the alter hook. 1
DefaultPluginManager::$namespaces protected property An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.
DefaultPluginManager::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
DefaultPluginManager::$pluginInterface protected property The interface each plugin should implement. 1
DefaultPluginManager::$subdir protected property The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.
DefaultPluginManager::alterDefinitions protected function Invokes the hook to alter the definitions if the alter hook is set. 1
DefaultPluginManager::alterInfo protected function Sets the alter hook name.
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::fixContextAwareDefinitions private function Fix the definitions of context-aware plugins.
DefaultPluginManager::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
DefaultPluginManager::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
DefaultPluginManager::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
DefaultPluginManager::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
DefaultPluginManager::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions 2
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::setCacheBackend public function Initialize the cache backend.
DefaultPluginManager::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
DefaultPluginManager::useCaches public function Disable the use of caches. Overrides CachedDiscoveryInterface::useCaches 1
DiscoveryCachedTrait::$definitions protected property Cached definitions array. 1
DiscoveryCachedTrait::getDefinition public function Overrides DiscoveryTrait::getDefinition 3
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
GutenbergLibraryManager::$activeThemeDefinitions protected property Static cache of theme Gutenberg plugin definitions.
GutenbergLibraryManager::$activeThemeMergedDefinition protected property Static cache of the merged active theme Gutenberg plugin definition.
GutenbergLibraryManager::$defaults protected property Provides default values for all gutenberg plugins. Overrides DefaultPluginManager::$defaults
GutenbergLibraryManager::$definitionsByExtension protected property Static cache of Gutenberg plugin definitions keyed by the extension type.
GutenbergLibraryManager::$logger protected property The Gutenberg logger.
GutenbergLibraryManager::$themeHandler protected property The theme handler.
GutenbergLibraryManager::$themeInitialization protected property The theme initialization.
GutenbergLibraryManager::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides DefaultPluginManager::clearCachedDefinitions
GutenbergLibraryManager::findDefinitions protected function Finds plugin definitions. Overrides DefaultPluginManager::findDefinitions
GutenbergLibraryManager::getActiveThemeDefinitions public function Gets a list of the active theme's .gutenberg.yml definitions. Overrides GutenbergLibraryManagerInterface::getActiveThemeDefinitions
GutenbergLibraryManager::getActiveThemeMergedDefinition public function Get the active theme merged definition. Overrides GutenbergLibraryManagerInterface::getActiveThemeMergedDefinition
GutenbergLibraryManager::getDefinitionsByExtension public function Get the list of gutenberg.yml definitions and group them by their type. Overrides GutenbergLibraryManagerInterface::getDefinitionsByExtension
GutenbergLibraryManager::getDiscovery protected function Gets the plugin discovery. Overrides DefaultPluginManager::getDiscovery
GutenbergLibraryManager::getModuleDefinitions public function Gets a list of modules with .gutenberg.yml definitions. Overrides GutenbergLibraryManagerInterface::getModuleDefinitions
GutenbergLibraryManager::getThemeDefinitions public function Gets a list of themes with .gutenberg.yml definitions. Overrides GutenbergLibraryManagerInterface::getThemeDefinitions
GutenbergLibraryManager::processDefinition public function Overrides DefaultPluginManager::processDefinition
GutenbergLibraryManager::providerExists protected function Determines if the provider of a definition exists. Overrides DefaultPluginManager::providerExists
GutenbergLibraryManager::__construct public function Constructs a new GutenbergManager object. Overrides DefaultPluginManager::__construct
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance 12
PluginManagerBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance 7
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.