You are here

class SkinPluginManager in Skinr 8.2

Manages plugins for configuration translation mappers.

Hierarchy

Expanded class hierarchy of SkinPluginManager

1 string reference to 'SkinPluginManager'
skinr.services.yml in ./skinr.services.yml
skinr.services.yml
1 service uses SkinPluginManager
plugin.manager.skin in ./skinr.services.yml
Drupal\skinr\SkinPluginManager

File

src/SkinPluginManager.php, line 20
Contains \Drupal\skinr\SkinPluginManager.

Namespace

Drupal\skinr
View source
class SkinPluginManager extends DefaultPluginManager {
  const PLUGIN_PATH = 'skins';

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

  /**
   * Constructs a new SkinPluginManager.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $theme_handler;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {

      // Look at all themes and modules.
      // @todo If the list of installed modules and themes is changed, new
      //   definitions are not picked up immediately and obsolete definitions
      //   are not removed, because the list of search directories is only
      //   compiled once in this constructor. The current code only works due to
      //   coincidence: The request that installs (for instance, a new theme)
      //   does not instantiate this plugin manager at the beginning of the
      //   request; when routes are being rebuilt at the end of the request,
      //   this service only happens to get instantiated with the updated list
      //   of installed themes.
      $directories = array();
      foreach ($this->moduleHandler
        ->getModuleList() as $name => $module) {
        $directories[$name] = $module
          ->getPath() . '/' . self::PLUGIN_PATH;
      }
      foreach ($this->themeHandler
        ->listInfo() as $theme) {
        $directories[$theme
          ->getName()] = $theme
          ->getPath() . '/' . self::PLUGIN_PATH;
      }

      // Check skins directories for *.yml files in module/theme roots.
      $this->discovery = new SkinYamlDirectoryDiscovery($directories, 'skin_plugins', 'id');
      $this->discovery
        ->addTranslatableProperty('title');

      // $this->discovery = new InfoHookDecorator($this->discovery, 'skinr_info');
      // $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
    }
    return $this->discovery;
  }

  /**
   * Prepare the default status for a skin.
   *
   * @param $skin
   *   Information about a registered skin.
   *
   * @return array
   *   An array of default statuses for each enabled theme.
   */
  protected function addStatusDefaults($skin) {
    $status = array();

    // Retrieve the explicit default status of the registering theme for itself.
    $base_theme_status = NULL;
    if (isset($skin['status'][$skin['source']['name']])) {
      $base_theme_status = $skin['status'][$skin['source']['name']];
    }

    // Retrieve the sub themes of the base theme that registered the skin.
    $sub_themes = array();
    if (isset($skin['source']['sub themes'])) {
      $sub_themes = $skin['source']['sub themes'];
    }
    $theme_handler = \Drupal::service('theme_handler');
    $themes = $theme_handler
      ->listInfo();
    foreach ($themes as $name => $theme) {
      if (!$theme->status) {
        continue;
      }

      // If this theme is a sub theme of the theme that registered the skin, check
      // whether we need to inherit the status of the base theme to the sub theme.
      // This is the case when a skin of a base theme enables itself for the base
      // theme (not knowing about potential sub themes).
      if (isset($base_theme_status) && isset($sub_themes[$name])) {
        $status[$name] = $base_theme_status;
      }

      // Apply global default.
      $status += array(
        $name => $skin['default status'],
      );
    }

    // Lastly, apply all explicit defaults.
    $status = array_merge($status, $skin['status']);
    return $status;
  }

  /**
   * {@inheritdoc}
   */
  public function processDefinition(&$definition, $plugin_id) {
    parent::processDefinition($definition, $plugin_id);
    if (!isset($definition['skins']) && !isset($definition['groups'])) {
      throw new InvalidPluginDefinitionException($plugin_id, "The plugin definition of the mapper '{$plugin_id}' does not contain any groups or skins.");
    }

    // Set defaults for groups.
    if (!empty($definition['groups'])) {
      foreach ($definition['groups'] as &$group) {
        $group += [
          'title' => t('Untitled'),
          'description' => '',
          'weight' => 0,
        ];
      }
    }

    // Set defaults for skins.
    if (!empty($definition['skins'])) {

      /** @var \Drupal\Core\Extension\Extension $extension */
      $extension = $this->moduleHandler
        ->getModule($definition['provider']);
      $extension_info = system_get_info($extension
        ->getType(), $extension
        ->getName());
      foreach ($definition['skins'] as $skin_name => &$skin) {
        $skin += [
          'name' => $skin_name,
          'type' => 'checkboxes',
          'group' => 'general',
          'title' => $skin_name,
          'description' => '',
          'theme hooks' => [],
          'attached' => [],
          'options' => [],
          'default status' => 0,
          'status' => [],
          'weight' => 0,
          'source' => [],
        ];

        // Add source information.
        $skin['source'] = [
          'type' => 'module',
          'name' => $extension
            ->getName(),
          'path' => dirname($definition['path']),
          'pathname' => $definition['path'],
          'version' => $extension_info['version'],
        ];

        /* @todo
           if ($extension->getType() == 'theme') {
             $skin['source'] += [
               'base themes' => $this->themeHandler->getBaseThemes($this->themeHandler->listInfo(), $extension->getName()),
               'sub themes' => isset($sub_themes[$name]) ? $sub_themes[$name] : array(),
             ];
           }
           */

        // Merge in default status for all themes.
        $skin['status'] = $this
          ->addStatusDefaults($skin);

        // Validate skin options.
        foreach ($skin['options'] as $option_name => $option) {

          // Validate class by running it through Html::getClass().
          if (!is_array($skin['options'][$option_name]['class'])) {

            // Raise an error.
            \Drupal::logger('skinr')
              ->warning('The class for option %option in skin %skin needs to be an array.', array(
              '%option' => $option_name,
              '%skin' => $skin_name,
            ));

            // Reset to array to prevent errors.
            $skin['options'][$option_name]['class'] = array();
          }
          foreach ($skin['options'][$option_name]['class'] as $key => $class) {
            $skin['options'][$option_name]['class'][$key] = Html::getClass($class);
          }
        }
      }
    }
  }

}

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::$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 5
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::findDefinitions protected function Finds plugin definitions. 7
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::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 7
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
SkinPluginManager::$themeHandler protected property The theme handler.
SkinPluginManager::addStatusDefaults protected function Prepare the default status for a skin.
SkinPluginManager::getDiscovery protected function Gets the plugin discovery. Overrides DefaultPluginManager::getDiscovery
SkinPluginManager::PLUGIN_PATH constant
SkinPluginManager::processDefinition public function Performs extra processing on plugin definitions. Overrides DefaultPluginManager::processDefinition
SkinPluginManager::__construct public function Constructs a new SkinPluginManager. Overrides DefaultPluginManager::__construct
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.