Skip to content
Snippets Groups Projects
Commit 09769ab5 authored by Alex Bronstein's avatar Alex Bronstein
Browse files

Issue #2822752 by tim.plunkett, jibran: Allow object-based plugin definitions...

Issue #2822752 by tim.plunkett, jibran: Allow object-based plugin definitions to be created by non-annotated discovery
parent a90e6866
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\Core\Layout;
namespace Drupal\Component\Annotation\Plugin\Discovery;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
/**
* Ensures that all array-based definitions are converted to objects.
*
* @internal
* The layout system is currently experimental and should only be leveraged by
* experimental modules and development releases of contributed modules.
* See https://www.drupal.org/core/experimental for more information.
*
* @todo Move into \Drupal\Component\Plugin\Discovery in
* https://www.drupal.org/node/2822752.
* Ensures that all definitions are run through the annotation process.
*/
class ObjectDefinitionDiscoveryDecorator implements DiscoveryInterface {
class AnnotationBridgeDecorator implements DiscoveryInterface {
use DiscoveryTrait;
......@@ -30,9 +22,6 @@ class ObjectDefinitionDiscoveryDecorator implements DiscoveryInterface {
/**
* The name of the annotation that contains the plugin definition.
*
* The class corresponding to this name must implement
* \Drupal\Component\Annotation\AnnotationInterface.
*
* @var string|null
*/
protected $pluginDefinitionAnnotationName;
......@@ -43,7 +32,9 @@ class ObjectDefinitionDiscoveryDecorator implements DiscoveryInterface {
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
* The discovery object that is being decorated.
* @param string $plugin_definition_annotation_name
* The name of the annotation that contains the plugin definition.
* The name of the annotation that contains the plugin definition. The class
* corresponding to this name must implement
* \Drupal\Component\Annotation\AnnotationInterface.
*/
public function __construct(DiscoveryInterface $decorated, $plugin_definition_annotation_name) {
$this->decorated = $decorated;
......@@ -56,6 +47,9 @@ public function __construct(DiscoveryInterface $decorated, $plugin_definition_an
public function getDefinitions() {
$definitions = $this->decorated->getDefinitions();
foreach ($definitions as $id => $definition) {
// Annotation constructors expect an array of values. If the definition is
// not an array, it usually means it has been processed already and can be
// ignored.
if (is_array($definition)) {
$definitions[$id] = (new $this->pluginDefinitionAnnotationName($definition))->get();
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Core\Layout;
use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
......@@ -63,7 +64,7 @@ protected function getDiscovery() {
if (!$this->discovery) {
$discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
$discovery = new YamlDiscoveryDecorator($discovery, 'layouts', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories());
$discovery = new ObjectDefinitionDiscoveryDecorator($discovery, $this->pluginDefinitionAnnotationName);
$discovery = new AnnotationBridgeDecorator($discovery, $this->pluginDefinitionAnnotationName);
$discovery = new ObjectDefinitionContainerDerivativeDiscoveryDecorator($discovery);
$this->discovery = $discovery;
}
......
<?php
namespace Drupal\Tests\Component\Annotation\Plugin\Discovery;
use Drupal\Component\Annotation\Plugin;
use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator
* @group Plugin
*/
class AnnotationBridgeDecoratorTest extends UnitTestCase {
/**
* @covers ::getDefinitions
*/
public function testGetDefinitions() {
$definitions = [];
$definitions['object'] = new ObjectDefinition(['id' => 'foo']);
$definitions['array'] = ['id' => 'bar'];
$discovery = $this->prophesize(DiscoveryInterface::class);
$discovery->getDefinitions()->willReturn($definitions);
$decorator = new AnnotationBridgeDecorator($discovery->reveal(), TestAnnotation::class);
$expected = [
'object' => new ObjectDefinition(['id' => 'foo']),
'array' => new ObjectDefinition(['id' => 'bar']),
];
$this->assertEquals($expected, $decorator->getDefinitions());
}
}
class TestAnnotation extends Plugin {
/**
* {@inheritdoc}
*/
public function get() {
return new ObjectDefinition($this->definition);
}
}
class ObjectDefinition extends PluginDefinition {
/**
* ObjectDefinition constructor.
*
* @param array $definition
*/
public function __construct(array $definition) {
foreach ($definition as $property => $value) {
$this->{$property} = $value;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment