"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php" (26 Nov 2020, 3813 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PluginDependencyTrait.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Plugin;
    4 
    5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
    6 use Drupal\Component\Plugin\DependentPluginInterface;
    7 use Drupal\Component\Plugin\PluginInspectionInterface;
    8 use Drupal\Component\Utility\NestedArray;
    9 use Drupal\Core\Entity\DependencyTrait;
   10 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
   11 
   12 /**
   13  * Provides a trait for calculating the dependencies of a plugin.
   14  */
   15 trait PluginDependencyTrait {
   16 
   17   use DependencyTrait;
   18 
   19   /**
   20    * Calculates and returns dependencies of a specific plugin instance.
   21    *
   22    * Dependencies are added for the module that provides the plugin, as well
   23    * as any dependencies declared by the instance's calculateDependencies()
   24    * method, if it implements
   25    * \Drupal\Component\Plugin\DependentPluginInterface.
   26    *
   27    * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
   28    *   The plugin instance.
   29    *
   30    * @return array
   31    *   An array of dependencies keyed by the type of dependency.
   32    */
   33   protected function getPluginDependencies(PluginInspectionInterface $instance) {
   34     $dependencies = [];
   35     $definition = $instance->getPluginDefinition();
   36 
   37     $provider = NULL;
   38     $config_dependencies = [];
   39     if ($definition instanceof PluginDefinitionInterface) {
   40       $provider = $definition->getProvider();
   41 
   42       if ($definition instanceof DependentPluginDefinitionInterface) {
   43         $config_dependencies = $definition->getConfigDependencies();
   44       }
   45     }
   46     elseif (is_array($definition)) {
   47       $provider = $definition['provider'];
   48 
   49       if (isset($definition['config_dependencies'])) {
   50         $config_dependencies = $definition['config_dependencies'];
   51       }
   52     }
   53 
   54     // Add the provider as a dependency, taking into account if it's a module or
   55     // a theme.
   56     if ($provider) {
   57       if ($provider === 'core' || $this->moduleHandler()->moduleExists($provider)) {
   58         $dependencies['module'][] = $provider;
   59       }
   60       elseif ($this->themeHandler()->themeExists($provider)) {
   61         $dependencies['theme'][] = $provider;
   62       }
   63       else {
   64         @trigger_error('Declaring a dependency on an uninstalled module is deprecated in Drupal 8.7.0 and will not be supported in Drupal 9.0.0.', E_USER_DEPRECATED);
   65         $dependencies['module'][] = $provider;
   66       }
   67     }
   68 
   69     // Add the config dependencies.
   70     if ($config_dependencies) {
   71       $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
   72     }
   73 
   74     // If a plugin is dependent, calculate its dependencies.
   75     if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
   76       $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
   77     }
   78     return $dependencies;
   79   }
   80 
   81   /**
   82    * Calculates and adds dependencies of a specific plugin instance.
   83    *
   84    * Dependencies are added for the module that provides the plugin, as well
   85    * as any dependencies declared by the instance's calculateDependencies()
   86    * method, if it implements
   87    * \Drupal\Component\Plugin\DependentPluginInterface.
   88    *
   89    * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
   90    *   The plugin instance.
   91    */
   92   protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
   93     $this->addDependencies($this->getPluginDependencies($instance));
   94   }
   95 
   96   /**
   97    * Wraps the module handler.
   98    *
   99    * @return \Drupal\Core\Extension\ModuleHandlerInterface
  100    *   The module handler.
  101    */
  102   protected function moduleHandler() {
  103     return \Drupal::moduleHandler();
  104   }
  105 
  106   /**
  107    * Wraps the theme handler.
  108    *
  109    * @return \Drupal\Core\Extension\ThemeHandlerInterface
  110    *   The theme handler.
  111    */
  112   protected function themeHandler() {
  113     return \Drupal::service('theme_handler');
  114   }
  115 
  116 }