annotate core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php @ 19:fa3358dc1485 tip

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