Commit 39881968 authored by alexpott's avatar alexpott

Issue #2939925 by jibran, tim.plunkett, yo30: Add a getPluginDependencies()...

Issue #2939925 by jibran, tim.plunkett, yo30: Add a getPluginDependencies() method to \Drupal\Core\Plugin\PluginDependencyTrait
parent 2f5a36cb
......@@ -5,6 +5,7 @@
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
......@@ -16,7 +17,7 @@ trait PluginDependencyTrait {
use DependencyTrait;
/**
* Calculates and adds dependencies of a specific plugin instance.
* Calculates and returns dependencies of a specific plugin instance.
*
* Dependencies are added for the module that provides the plugin, as well
* as any dependencies declared by the instance's calculateDependencies()
......@@ -25,28 +26,47 @@ trait PluginDependencyTrait {
*
* @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
* The plugin instance.
*
* @return array
* An array of dependencies keyed by the type of dependency.
*/
protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
protected function getPluginDependencies(PluginInspectionInterface $instance) {
$dependencies = [];
$definition = $instance->getPluginDefinition();
if ($definition instanceof PluginDefinitionInterface) {
$this->addDependency('module', $definition->getProvider());
$dependencies['module'][] = $definition->getProvider();
if ($definition instanceof DependentPluginDefinitionInterface && $config_dependencies = $definition->getConfigDependencies()) {
$this->addDependencies($config_dependencies);
$dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
}
}
elseif (is_array($definition)) {
$this->addDependency('module', $definition['provider']);
$dependencies['module'][] = $definition['provider'];
// Plugins can declare additional dependencies in their definition.
if (isset($definition['config_dependencies'])) {
$this->addDependencies($definition['config_dependencies']);
$dependencies = NestedArray::mergeDeep($dependencies, $definition['config_dependencies']);
}
}
// If a plugin is dependent, calculate its dependencies.
if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
$this->addDependencies($plugin_dependencies);
$dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
}
return $dependencies;
}
/**
* Calculates and adds dependencies of a specific plugin instance.
*
* Dependencies are added for the module that provides the plugin, as well
* as any dependencies declared by the instance's calculateDependencies()
* method, if it implements
* \Drupal\Component\Plugin\DependentPluginInterface.
*
* @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
* The plugin instance.
*/
protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
$this->addDependencies($this->getPluginDependencies($instance));
}
}
......@@ -2,9 +2,6 @@
namespace Drupal\layout_builder\Entity;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Entity\EntityViewDisplay as BaseEntityViewDisplay;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
......@@ -232,32 +229,6 @@ public function onDependencyRemoval(array $dependencies) {
return $changed;
}
/**
* Calculates and returns dependencies of a specific plugin instance.
*
* @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
* The plugin instance.
*
* @return array
* An array of dependencies keyed by the type of dependency.
*
* @todo Replace this in https://www.drupal.org/project/drupal/issues/2939925.
*/
protected function getPluginDependencies(PluginInspectionInterface $instance) {
$definition = $instance->getPluginDefinition();
$dependencies['module'][] = $definition['provider'];
// Plugins can declare additional dependencies in their definition.
if (isset($definition['config_dependencies'])) {
$dependencies = NestedArray::mergeDeep($dependencies, $definition['config_dependencies']);
}
// If a plugin is dependent, calculate its dependencies.
if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
$dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
}
return $dependencies;
}
/**
* {@inheritdoc}
*/
......
......@@ -16,10 +16,25 @@
*/
class PluginDependencyTraitTest extends UnitTestCase {
/**
* @covers ::getPluginDependencies
*
* @dataProvider providerTestPluginDependencies
*/
public function testGetPluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
$test_class = new TestPluginDependency();
$plugin->getPluginDefinition()->willReturn($definition);
$actual = $test_class->getPluginDependencies($plugin->reveal());
$this->assertEquals($expected, $actual);
$this->assertEmpty($test_class->getDependencies());
}
/**
* @covers ::calculatePluginDependencies
*
* @dataProvider providerTestCalculatePluginDependencies
* @dataProvider providerTestPluginDependencies
*
* @param \Prophecy\Prophecy\ProphecyInterface $plugin
* A prophecy of a plugin instance.
......@@ -38,9 +53,9 @@ public function testCalculatePluginDependencies(ProphecyInterface $plugin, $defi
}
/**
* Provides test data for ::testCalculatePluginDependencies().
* Provides test data for plugin dependencies.
*/
public function providerTestCalculatePluginDependencies() {
public function providerTestPluginDependencies() {
$data = [];
$plugin = $this->prophesize(PluginInspectionInterface::class);
......@@ -111,6 +126,7 @@ class TestPluginDependency {
use PluginDependencyTrait {
calculatePluginDependencies as public;
getPluginDependencies as public;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment