annotate core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
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@4 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@4 20 * Calculates and returns dependencies of a specific plugin instance.
Chris@4 21 *
Chris@4 22 * Dependencies are added for the module that provides the plugin, as well
Chris@4 23 * as any dependencies declared by the instance's calculateDependencies()
Chris@4 24 * method, if it implements
Chris@4 25 * \Drupal\Component\Plugin\DependentPluginInterface.
Chris@4 26 *
Chris@4 27 * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
Chris@4 28 * The plugin instance.
Chris@4 29 *
Chris@4 30 * @return array
Chris@4 31 * An array of dependencies keyed by the type of dependency.
Chris@4 32 */
Chris@4 33 protected function getPluginDependencies(PluginInspectionInterface $instance) {
Chris@4 34 $dependencies = [];
Chris@4 35 $definition = $instance->getPluginDefinition();
Chris@5 36
Chris@5 37 $provider = NULL;
Chris@5 38 $config_dependencies = [];
Chris@4 39 if ($definition instanceof PluginDefinitionInterface) {
Chris@5 40 $provider = $definition->getProvider();
Chris@5 41
Chris@5 42 if ($definition instanceof DependentPluginDefinitionInterface) {
Chris@5 43 $config_dependencies = $definition->getConfigDependencies();
Chris@4 44 }
Chris@4 45 }
Chris@4 46 elseif (is_array($definition)) {
Chris@5 47 $provider = $definition['provider'];
Chris@5 48
Chris@4 49 if (isset($definition['config_dependencies'])) {
Chris@5 50 $config_dependencies = $definition['config_dependencies'];
Chris@4 51 }
Chris@4 52 }
Chris@4 53
Chris@5 54 // Add the provider as a dependency, taking into account if it's a module or
Chris@5 55 // a theme.
Chris@5 56 if ($provider) {
Chris@5 57 if ($provider === 'core' || $this->moduleHandler()->moduleExists($provider)) {
Chris@5 58 $dependencies['module'][] = $provider;
Chris@5 59 }
Chris@5 60 elseif ($this->themeHandler()->themeExists($provider)) {
Chris@5 61 $dependencies['theme'][] = $provider;
Chris@5 62 }
Chris@5 63 else {
Chris@5 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@5 65 $dependencies['module'][] = $provider;
Chris@5 66 }
Chris@5 67 }
Chris@5 68
Chris@5 69 // Add the config dependencies.
Chris@5 70 if ($config_dependencies) {
Chris@5 71 $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
Chris@5 72 }
Chris@5 73
Chris@4 74 // If a plugin is dependent, calculate its dependencies.
Chris@4 75 if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
Chris@4 76 $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
Chris@4 77 }
Chris@4 78 return $dependencies;
Chris@4 79 }
Chris@4 80
Chris@4 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@4 93 $this->addDependencies($this->getPluginDependencies($instance));
Chris@0 94 }
Chris@0 95
Chris@5 96 /**
Chris@5 97 * Wraps the module handler.
Chris@5 98 *
Chris@5 99 * @return \Drupal\Core\Extension\ModuleHandlerInterface
Chris@5 100 * The module handler.
Chris@5 101 */
Chris@5 102 protected function moduleHandler() {
Chris@5 103 return \Drupal::moduleHandler();
Chris@5 104 }
Chris@5 105
Chris@5 106 /**
Chris@5 107 * Wraps the theme handler.
Chris@5 108 *
Chris@5 109 * @return \Drupal\Core\Extension\ThemeHandlerInterface
Chris@5 110 * The theme handler.
Chris@5 111 */
Chris@5 112 protected function themeHandler() {
Chris@5 113 return \Drupal::service('theme_handler');
Chris@5 114 }
Chris@5 115
Chris@0 116 }