From 9b0250e1c2351a34aae376331a52230c5a6d8a08 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 19 Jan 2024 16:49:11 -0400 Subject: [PATCH] Move away from compiler pass. --- .../DgiStandardDerivativeExaminerModel.php | 8 +++++ ...ndardDerivativeExaminerServiceProvider.php | 20 ------------- src/ModelTargetPluginManagerExtensionPass.php | 30 ------------------- .../ModelPluginBase.php | 5 +++- src/TargetPluginManager.php | 15 +++++++++- 5 files changed, 26 insertions(+), 52 deletions(-) delete mode 100644 src/DgiStandardDerivativeExaminerServiceProvider.php delete mode 100644 src/ModelTargetPluginManagerExtensionPass.php diff --git a/src/Annotation/DgiStandardDerivativeExaminerModel.php b/src/Annotation/DgiStandardDerivativeExaminerModel.php index a94697f..c13f3c6 100644 --- a/src/Annotation/DgiStandardDerivativeExaminerModel.php +++ b/src/Annotation/DgiStandardDerivativeExaminerModel.php @@ -3,6 +3,7 @@ namespace Drupal\dgi_standard_derivative_examiner\Annotation; use Drupal\Component\Annotation\Plugin; +use Drupal\dgi_standard_derivative_examiner\TargetPluginManager; /** * Model plugin info. @@ -25,4 +26,11 @@ class DgiStandardDerivativeExaminerModel extends Plugin { */ public string $uri; + /** + * The name of the class managing target plugins. + * + * @var string + */ + public string $targetManagerClass = TargetPluginManager::class; + } diff --git a/src/DgiStandardDerivativeExaminerServiceProvider.php b/src/DgiStandardDerivativeExaminerServiceProvider.php deleted file mode 100644 index cbd8033..0000000 --- a/src/DgiStandardDerivativeExaminerServiceProvider.php +++ /dev/null @@ -1,20 +0,0 @@ -addCompilerPass(new ModelTargetPluginManagerExtensionPass()); - } - -} diff --git a/src/ModelTargetPluginManagerExtensionPass.php b/src/ModelTargetPluginManagerExtensionPass.php deleted file mode 100644 index b68cca3..0000000 --- a/src/ModelTargetPluginManagerExtensionPass.php +++ /dev/null @@ -1,30 +0,0 @@ -get('plugin.manager.dgi_standard_derivative_examiner.model'); - - foreach ($model_plugin_manager->getDefinitions() as $id => $definition) { - $container->register("plugin.manager.dgi_standard_derivative_examiner.target.{$id}", TargetPluginManager::class) - ->setArguments([ - $id, - new Reference('container.namespaces'), - new Reference('module_handler'), - ]); - } - } - -} diff --git a/src/Plugin/dgi_standard_derivative_examiner/ModelPluginBase.php b/src/Plugin/dgi_standard_derivative_examiner/ModelPluginBase.php index 9c2f71a..cb82940 100644 --- a/src/Plugin/dgi_standard_derivative_examiner/ModelPluginBase.php +++ b/src/Plugin/dgi_standard_derivative_examiner/ModelPluginBase.php @@ -2,6 +2,7 @@ namespace Drupal\dgi_standard_derivative_examiner\Plugin\dgi_standard_derivative_examiner; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\dgi_standard_derivative_examiner\ModelInterface; @@ -35,7 +36,9 @@ abstract class ModelPluginBase extends PluginBase implements ContainerFactoryPlu public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = new static($configuration, $plugin_id, $plugin_definition); - $instance->targetPluginManager = $container->get("plugin.manager.dgi_standard_derivative_examiner.target.{$plugin_id}"); + $instance->targetPluginManager = is_a($plugin_definition['targetManagerClass'], ContainerInjectionInterface::class, TRUE) ? + $plugin_definition['targetManagerClass']::create($container, $plugin_id) : + new $plugin_definition['targetManagerClass']($plugin_id); return $instance; } diff --git a/src/TargetPluginManager.php b/src/TargetPluginManager.php index 6837455..4842669 100644 --- a/src/TargetPluginManager.php +++ b/src/TargetPluginManager.php @@ -2,14 +2,16 @@ namespace Drupal\dgi_standard_derivative_examiner; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\dgi_standard_derivative_examiner\Annotation\DgiStandardDerivativeExaminerTarget; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Target plugin manager service. */ -class TargetPluginManager extends DefaultPluginManager implements TargetPluginManagerInterface { +class TargetPluginManager extends DefaultPluginManager implements TargetPluginManagerInterface, ContainerInjectionInterface { /** * Constructor. @@ -31,4 +33,15 @@ public function __construct( $this->alterInfo("dgi_standard_derivative_examiner_{$type}_target_plugin_info"); } + /** + * {@inheritDoc} + */ + public static function create(ContainerInterface $container, string $type = '') : self { + return new static( + $type, + $container->get('container.namespaces'), + $container->get('module_handler'), + ); + } + }