You are here

class CKEditor4To5UpgradePluginManager in Drupal 10

Provides a CKEditor 4 to 5 upgrade plugin manager.

@internal

Hierarchy

Expanded class hierarchy of CKEditor4To5UpgradePluginManager

See also

\Drupal\ckeditor5\Plugin\CKEditor4To5UpgradePluginInterface

\Drupal\ckeditor5\Annotation\CKEditor4To5Upgrade

Plugin API

1 string reference to 'CKEditor4To5UpgradePluginManager'
ckeditor5.services.yml in core/modules/ckeditor5/ckeditor5.services.yml
core/modules/ckeditor5/ckeditor5.services.yml
1 service uses CKEditor4To5UpgradePluginManager
plugin.manager.ckeditor4to5upgrade.plugin in core/modules/ckeditor5/ckeditor5.services.yml
Drupal\ckeditor5\Plugin\CKEditor4To5UpgradePluginManager

File

core/modules/ckeditor5/src/Plugin/CKEditor4To5UpgradePluginManager.php, line 24

Namespace

Drupal\ckeditor5\Plugin
View source
class CKEditor4To5UpgradePluginManager extends DefaultPluginManager {

  /**
   * A map of CKEditor 4 buttons to an upgrade plugin ID.
   *
   * @var array
   */
  protected $cke4ButtonsMap;

  /**
   * A map of CKEditor 4 plugins with settings to an upgrade plugin ID.
   *
   * @var array
   */
  protected $cke4PluginSettingsMap;

  /**
   * A map of CKEditor 5 plugins with configurable subset to upgrade plugin ID.
   *
   * @var array
   */
  protected $cke5SubsetConfigurationMap;

  /**
   * Constructs a CKEditor4To5UpgradePluginManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/CKEditor4To5Upgrade', $namespaces, $module_handler, CKEditor4To5UpgradePluginInterface::class, CKEditor4To5Upgrade::class);
    $this
      ->alterInfo('ckeditor4to5upgrade_plugin_info');
    $this
      ->setCacheBackend($cache_backend, 'ckeditor4to5upgrade_plugins');
  }

  /**
   * Validates plugin definitions: avoids conflicts. Builds maps for later use.
   */
  protected function validateAndBuildMaps() : void {
    if ($this->cke4ButtonsMap !== NULL) {
      return;
    }
    foreach ($this
      ->getDefinitions() as $upgrade_plugin_id => $definition) {

      // Only one CKEditor4To5Upgrade plugin can define the upgrade path for a
      // CKEditor 4 button.
      if (isset($definition['cke4_buttons'])) {
        assert(Inspector::assertAllStrings($definition['cke4_buttons']));
        foreach ($definition['cke4_buttons'] as $cke4_button_id) {
          if (isset($this->cke4ButtonsMap[$cke4_button_id])) {
            throw new \OutOfBoundsException(sprintf('The "%s" CKEditor 4 button is already being upgraded by the "%s" CKEditor4To5Upgrade plugin, the "%s" plugin is as well. This conflict needs to be resolved.', $cke4_button_id, $this->cke4ButtonsMap[$cke4_button_id], $upgrade_plugin_id));
          }
          $this->cke4ButtonsMap[$cke4_button_id] = $upgrade_plugin_id;
        }
      }

      // Only one CKEditor4To5Upgrade plugin can define the upgrade path for a
      // CKEditor 4 plugin's settings.
      if (isset($definition['cke4_plugin_settings'])) {
        assert(Inspector::assertAllStrings($definition['cke4_plugin_settings']));
        foreach ($definition['cke4_plugin_settings'] as $cke4_plugin_id) {
          if (isset($this->cke4PluginSettingsMap[$cke4_plugin_id])) {
            throw new \OutOfBoundsException(sprintf('The "%s" CKEditor 4 plugin\'s settings are already being upgraded by the "%s" CKEditor4To5Upgrade plugin, the "%s" plugin is as well. This conflict needs to be resolved.', $cke4_plugin_id, $this->cke4PluginSettingsMap[$cke4_plugin_id], $upgrade_plugin_id));
          }
          $this->cke4PluginSettingsMap[$cke4_plugin_id] = $upgrade_plugin_id;
        }
      }

      // Only one CKEditor4To5Upgrade plugin can define the upgrade path for a
      // CKEditor 5 plugin's elements subset configuration.
      if (isset($definition['cke5_plugin_elements_subset_configuration'])) {
        assert(Inspector::assertAllStrings($definition['cke5_plugin_elements_subset_configuration']));
        foreach ($definition['cke5_plugin_elements_subset_configuration'] as $cke5_plugin_id) {
          if (isset($this->cke5SubsetConfigurationMap[$cke5_plugin_id])) {
            throw new \OutOfBoundsException(sprintf('The "%s" CKEditor 5 plugin\'s elements subset configuration is already being computed by the "%s" CKEditor4To5Upgrade plugin, the "%s" plugin is as well. This conflict needs to be resolved.', $cke5_plugin_id, $this->cke5SubsetConfigurationMap[$cke5_plugin_id], $upgrade_plugin_id));
          }
          $this->cke5SubsetConfigurationMap[$cke5_plugin_id] = $upgrade_plugin_id;
        }
      }
    }
  }

  /**
   * Maps a CKEditor 4 button to the CKEditor 5 equivalent, if it exists.
   *
   * Generated by inspecting all \Drupal\ckeditor\CKEditorPluginButtonsInterface
   * implementations.
   *
   * @param string $cke4_button
   *   A valid CKEditor 4 button name.
   * @param \Drupal\ckeditor5\HTMLRestrictions $text_format_html_restrictions
   *   The restrictions of the text format, to allow an upgrade plugin to
   *   inspect the text format's HTML restrictions to make a decision.
   *
   * @return string[]|null
   *   The equivalent CKEditor 5 toolbar items, or NULL if no equivalent exists.
   *
   * @throws \OutOfBoundsException
   *   Thrown when no upgrade path exists.
   * @throws \LogicException
   *   Thrown when a plugin claims to provide an upgrade path but does not.
   *
   * @see \Drupal\ckeditor\CKEditorPluginButtonsInterface
   */
  public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_button, HTMLRestrictions $text_format_html_restrictions) : ?array {
    $this
      ->validateAndBuildMaps();
    if (!isset($this->cke4ButtonsMap[$cke4_button])) {
      throw new \OutOfBoundsException(sprintf('No upgrade path found for the "%s" button.', $cke4_button));
    }
    $plugin_id = $this->cke4ButtonsMap[$cke4_button];
    try {
      return $this
        ->createInstance($plugin_id)
        ->mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem($cke4_button, $text_format_html_restrictions);
    } catch (\OutOfBoundsException $e) {
      throw new \LogicException(sprintf('The "%s" CKEditor4To5Upgrade plugin claims to provide an upgrade path for the "%s" CKEditor 4 button but does not.', $plugin_id, $cke4_button));
    }
  }

  /**
   * Maps CKEditor 4 settings to the CKEditor 5 equivalent, if needed.
   *
   * Not every CKEditor 5 plugin has settings; some CKEditor 5 plugins may have
   * settings that the CKEditor 4 equivalent did not and vice versa. Therefore
   * the complete CKEditor 4 settings are provided, and any CKEditor 5 setting
   * can be set.
   *
   * @param string $cke4_plugin_id
   *   The CKEditor 4 plugin whose settings need to be mapped.
   * @param array $cke4_plugin_settings
   *   The settings for this CKEditor 4 plugin.
   *
   * @return array|null
   *   NULL if not needed, otherwise an array with a single key-value pair:
   *   - key: the plugin ID of the equivalent CKEditor 5 plugin
   *   - value: the equivalent settings
   *
   * @throws \OutOfBoundsException
   *   Thrown when no upgrade path exists.
   * @throws \LogicException
   *   Thrown when a plugin claims to provide an upgrade path but does not.
   *
   * @see \Drupal\ckeditor\CKEditorPluginConfigurableInterface
   */
  public function mapCKEditor4SettingsToCKEditor5Configuration(string $cke4_plugin_id, array $cke4_plugin_settings) : ?array {
    $this
      ->validateAndBuildMaps();
    if (!isset($this->cke4PluginSettingsMap[$cke4_plugin_id])) {
      throw new \OutOfBoundsException(sprintf('No upgrade path found for the "%s" plugin settings.', $cke4_plugin_id));
    }
    $plugin_id = $this->cke4PluginSettingsMap[$cke4_plugin_id];
    try {
      return $this
        ->createInstance($plugin_id)
        ->mapCKEditor4SettingsToCKEditor5Configuration($cke4_plugin_id, $cke4_plugin_settings);
    } catch (\OutOfBoundsException $e) {
      throw new \LogicException(sprintf('The "%s" CKEditor4To5Upgrade plugin claims to provide an upgrade path for the "%s" CKEditor 4 plugin settings but does not.', $plugin_id, $cke4_plugin_id));
    }
  }

  /**
   * Computes elements subset configuration for CKEditor 5 plugin.
   *
   * Every CKEditor 5 plugin that implements the elements subset interface must
   * implement this as well, to ensure a smooth upgrade path.
   *
   * @param string $cke5_plugin_id
   *   The CKEditor 5 plugin whose subset configuration needs to be computed.
   * @param \Drupal\filter\FilterFormatInterface $text_format
   *   The text format based on whose restrictions this should be computed.
   *
   * @return array|null
   *   NULL if not needed, otherwise a configuration array (which can itself be
   *   a subset of the default configuration of this CKEditor 5 plugin: perhaps
   *   only some of the configuration values determine the subset).
   *
   * @throws \OutOfBoundsException
   *   Thrown when no upgrade path exists.
   * @throws \LogicException
   *   Thrown when a plugin claims to provide an upgrade path but does not.
   *
   * @see \Drupal\ckeditor5\Plugin\CKEditor5PluginElementsSubsetInterface
   */
  public function computeCKEditor5PluginSubsetConfiguration(string $cke5_plugin_id, FilterFormatInterface $text_format) : ?array {
    $this
      ->validateAndBuildMaps();
    if (!isset($this->cke5SubsetConfigurationMap[$cke5_plugin_id])) {
      throw new \OutOfBoundsException(sprintf('No upgrade path found for the "%s" elements subset configuration.', $cke5_plugin_id));
    }
    $plugin_id = $this->cke5SubsetConfigurationMap[$cke5_plugin_id];
    try {
      return $this
        ->createInstance($plugin_id)
        ->computeCKEditor5PluginSubsetConfiguration($cke5_plugin_id, $text_format);
    } catch (\OutOfBoundsException $e) {
      throw new \LogicException(sprintf('The "%s" CKEditor4To5Upgrade plugin claims to provide an upgrade path for the "%s" CKEditor 4 plugin settings but does not.', $plugin_id, $cke5_plugin_id));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditor4To5UpgradePluginManager::$cke4ButtonsMap protected property A map of CKEditor 4 buttons to an upgrade plugin ID.
CKEditor4To5UpgradePluginManager::$cke4PluginSettingsMap protected property A map of CKEditor 4 plugins with settings to an upgrade plugin ID.
CKEditor4To5UpgradePluginManager::$cke5SubsetConfigurationMap protected property A map of CKEditor 5 plugins with configurable subset to upgrade plugin ID.
CKEditor4To5UpgradePluginManager::computeCKEditor5PluginSubsetConfiguration public function Computes elements subset configuration for CKEditor 5 plugin.
CKEditor4To5UpgradePluginManager::mapCKEditor4SettingsToCKEditor5Configuration public function Maps CKEditor 4 settings to the CKEditor 5 equivalent, if needed.
CKEditor4To5UpgradePluginManager::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem public function Maps a CKEditor 4 button to the CKEditor 5 equivalent, if it exists.
CKEditor4To5UpgradePluginManager::validateAndBuildMaps protected function Validates plugin definitions: avoids conflicts. Builds maps for later use.
CKEditor4To5UpgradePluginManager::__construct public function Constructs a CKEditor4To5UpgradePluginManager object. Overrides DefaultPluginManager::__construct
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::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 9
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::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions 6
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::findDefinitions protected function Finds plugin definitions. 7
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::getDiscovery protected function Gets the plugin discovery. Overrides PluginManagerBase::getDiscovery 13
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::processDefinition public function Performs extra processing on plugin definitions. 13
DefaultPluginManager::providerExists protected function Determines if the provider of a definition exists. 3
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
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 6
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.
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.