-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from chesn0k/plugin-discovery-attribute
Plugin manager using attributes for plugins
- Loading branch information
Showing
14 changed files
with
285 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
plugin.manager.{{ plugin_type }}: | ||
class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager | ||
parent: default_plugin_manager |
23 changes: 23 additions & 0 deletions
23
templates/_plugin-manager/attribute/src/Attribute/Example.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\{{ machine_name }}\Attribute; | ||
|
||
use Drupal\Component\Plugin\Attribute\AttributeBase; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
|
||
/** | ||
* The {{ plugin_type }} attribute. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class {{ class_prefix }} extends AttributeBase { | ||
|
||
/** | ||
* Constructs a new {{ class_prefix }} instance. | ||
*/ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly ?TranslatableMarkup $label, | ||
public readonly ?TranslatableMarkup $description = NULL, | ||
) {} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
templates/_plugin-manager/attribute/src/ExampleInterface.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\{{ machine_name }}; | ||
|
||
/** | ||
* Interface for {{ plugin_type }} plugins. | ||
*/ | ||
interface {{ class_prefix }}Interface { | ||
|
||
/** | ||
* Returns the translated plugin label. | ||
*/ | ||
public function label(): string; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
templates/_plugin-manager/attribute/src/ExamplePluginBase.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\{{ machine_name }}; | ||
|
||
use Drupal\Component\Plugin\PluginBase; | ||
|
||
/** | ||
* Base class for {{ plugin_type }} plugins. | ||
*/ | ||
abstract class {{ class_prefix }}PluginBase extends PluginBase implements {{ class_prefix }}Interface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function label(): string { | ||
// Cast the label to a string since it is a TranslatableMarkup object. | ||
return (string) $this->pluginDefinition['label']; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
templates/_plugin-manager/attribute/src/ExamplePluginManager.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\{{ machine_name }}; | ||
|
||
{% sort %} | ||
use Drupal\Core\Cache\CacheBackendInterface; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
use Drupal\Core\Plugin\DefaultPluginManager; | ||
use Drupal\{{ machine_name }}\Attribute\{{ class_prefix }}; | ||
{% endsort %} | ||
|
||
/** | ||
* {{ class_prefix }} plugin manager. | ||
*/ | ||
final class {{ class_prefix }}PluginManager extends DefaultPluginManager { | ||
|
||
/** | ||
* Constructs the object. | ||
*/ | ||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { | ||
parent::__construct('Plugin/{{ class_prefix }}', $namespaces, $module_handler, {{ class_prefix }}Interface::class, {{ class_prefix }}::class); | ||
$this->alterInfo('{{ plugin_type }}_info'); | ||
$this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins'); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
templates/_plugin-manager/attribute/src/Plugin/Example/Foo.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\{{ machine_name }}\Plugin\{{ class_prefix }}; | ||
|
||
{% sort %} | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Drupal\{{ machine_name }}\Attribute\{{ class_prefix }}; | ||
use Drupal\{{ machine_name }}\{{ class_prefix }}PluginBase; | ||
{% endsort %} | ||
|
||
/** | ||
* Plugin implementation of the {{ plugin_type }}. | ||
*/ | ||
#[{{ class_prefix }}( | ||
id: "foo", | ||
label: new TranslatableMarkup("Foo"), | ||
description: new TranslatableMarkup("Foo description."), | ||
)] | ||
final class Foo extends {{ class_prefix }}PluginBase { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/functional/Generator/_plugin_manager/_attribute/foo.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
plugin.manager.bar: | ||
class: Drupal\foo\BarPluginManager | ||
parent: default_plugin_manager |
23 changes: 23 additions & 0 deletions
23
tests/functional/Generator/_plugin_manager/_attribute/src/Attribute/Bar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\foo\Attribute; | ||
|
||
use Drupal\Component\Plugin\Attribute\AttributeBase; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
|
||
/** | ||
* The bar attribute. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class Bar extends AttributeBase { | ||
|
||
/** | ||
* Constructs a new Bar instance. | ||
*/ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly ?TranslatableMarkup $label, | ||
public readonly ?TranslatableMarkup $description = NULL, | ||
) {} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
tests/functional/Generator/_plugin_manager/_attribute/src/BarInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\foo; | ||
|
||
/** | ||
* Interface for bar plugins. | ||
*/ | ||
interface BarInterface { | ||
|
||
/** | ||
* Returns the translated plugin label. | ||
*/ | ||
public function label(): string; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
tests/functional/Generator/_plugin_manager/_attribute/src/BarPluginBase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\foo; | ||
|
||
use Drupal\Component\Plugin\PluginBase; | ||
|
||
/** | ||
* Base class for bar plugins. | ||
*/ | ||
abstract class BarPluginBase extends PluginBase implements BarInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function label(): string { | ||
// Cast the label to a string since it is a TranslatableMarkup object. | ||
return (string) $this->pluginDefinition['label']; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tests/functional/Generator/_plugin_manager/_attribute/src/BarPluginManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\foo; | ||
|
||
use Drupal\Core\Cache\CacheBackendInterface; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
use Drupal\Core\Plugin\DefaultPluginManager; | ||
use Drupal\foo\Attribute\Bar; | ||
|
||
/** | ||
* Bar plugin manager. | ||
*/ | ||
final class BarPluginManager extends DefaultPluginManager { | ||
|
||
/** | ||
* Constructs the object. | ||
*/ | ||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { | ||
parent::__construct('Plugin/Bar', $namespaces, $module_handler, BarInterface::class, Bar::class); | ||
$this->alterInfo('bar_info'); | ||
$this->setCacheBackend($cache_backend, 'bar_plugins'); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
tests/functional/Generator/_plugin_manager/_attribute/src/Plugin/Bar/Foo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Drupal\foo\Plugin\Bar; | ||
|
||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Drupal\foo\Attribute\Bar; | ||
use Drupal\foo\BarPluginBase; | ||
|
||
/** | ||
* Plugin implementation of the bar. | ||
*/ | ||
#[Bar( | ||
id: "foo", | ||
label: new TranslatableMarkup("Foo"), | ||
description: new TranslatableMarkup("Foo description."), | ||
)] | ||
final class Foo extends BarPluginBase { | ||
|
||
} |