diff --git a/inmail.install b/inmail.install index fd62246..61a4396 100644 --- a/inmail.install +++ b/inmail.install @@ -3,7 +3,6 @@ * @file * Implementations of install hooks for the Inmail module. */ -use Drupal\inmail\PluginRequirementsInterface; /** * Implements hook_requirements(). @@ -27,21 +26,22 @@ function inmail_get_plugin_requirements() { /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = \Drupal::entityTypeManager(); $plugin_types = ['analyzer', 'deliverer', 'handler']; - $plugins = []; + $plugin_requirements = []; foreach ($plugin_types as $plugin_type) { $enabled_instances = $entity_type_manager->getStorage("inmail_$plugin_type")->loadByProperties(['status' => TRUE]); /** @var \Drupal\inmail\Entity\PluginConfigEntity $enabled_instance */ foreach ($enabled_instances as $enabled_instance) { - $key = $plugin_type . '_' . $enabled_instance->id(); - $plugins[$key] = $enabled_instance->checkRequirements(); + if (!$enabled_instance->isAvailable()) { + $key = $plugin_type . '_' . $enabled_instance->getPluginId(); + if (!isset($plugin_requirements[$key])) { + $plugin_requirements[$key] = $enabled_instance->checkRequirements(); + } + } } } - // Filter plugins with no requirements. - $plugins = array_filter($plugins); - - return $plugins; + return $plugin_requirements; } /** diff --git a/src/Entity/AnalyzerConfig.php b/src/Entity/AnalyzerConfig.php index b415afb..90e4873 100644 --- a/src/Entity/AnalyzerConfig.php +++ b/src/Entity/AnalyzerConfig.php @@ -43,6 +43,20 @@ namespace Drupal\inmail\Entity; class AnalyzerConfig extends PluginConfigEntity { /** + * Constructs a new AnalyzerConfig. + * + * @param array $values + * An array of values to set, keyed by property name. If the entity type + * has bundles, the bundle key has to be specified. + * @param string $entity_type + * The type of the entity to create. + */ + public function __construct(array $values, $entity_type) { + parent::__construct($values, $entity_type); + $this->pluginType = 'analyzer'; + } + + /** * The weight of the analyzer configuration. * * Analyzers with lower weights are invoked before those with higher weights. diff --git a/src/Entity/DelivererConfig.php b/src/Entity/DelivererConfig.php index 39893d0..0cbed0a 100644 --- a/src/Entity/DelivererConfig.php +++ b/src/Entity/DelivererConfig.php @@ -40,4 +40,18 @@ namespace Drupal\inmail\Entity; * ) */ class DelivererConfig extends PluginConfigEntity { + + /** + * Constructs a new DelivererConfig. + * + * @param array $values + * An array of values to set, keyed by property name. If the entity type + * has bundles, the bundle key has to be specified. + * @param string $entity_type + * The type of the entity to create. + */ + public function __construct(array $values, $entity_type) { + parent::__construct($values, $entity_type); + $this->pluginType = 'deliverer'; + } } diff --git a/src/Entity/HandlerConfig.php b/src/Entity/HandlerConfig.php index 552f146..0344acb 100644 --- a/src/Entity/HandlerConfig.php +++ b/src/Entity/HandlerConfig.php @@ -40,4 +40,19 @@ namespace Drupal\inmail\Entity; */ class HandlerConfig extends PluginConfigEntity { // @todo Implement HandlerConfig::calculateDependencies() https://www.drupal.org/node/2379929 + + /** + * Constructs a new HandlerConfig. + * + * @param array $values + * An array of values to set, keyed by property name. If the entity type + * has bundles, the bundle key has to be specified. + * @param string $entity_type + * The type of the entity to create. + */ + public function __construct(array $values, $entity_type) { + parent::__construct($values, $entity_type); + $this->pluginType = 'handler'; + } + } diff --git a/src/Entity/PluginConfigEntity.php b/src/Entity/PluginConfigEntity.php index 4f4ba18..f704a3d 100644 --- a/src/Entity/PluginConfigEntity.php +++ b/src/Entity/PluginConfigEntity.php @@ -39,6 +39,20 @@ abstract class PluginConfigEntity extends ConfigEntityBase implements PluginRequ protected $configuration = array(); /** + * The plugin instance. + * + * @var \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface|\Drupal\inmail\Plugin\inmail\Deliverer\DelivererInterface|\Drupal\inmail\Plugin\inmail\Handler\HandlerInterface + */ + protected $pluginInstance; + + /** + * The Inmail plugin type - deliverer, analyzer, handler. + * + * @var string + */ + protected $pluginType; + + /** * Returns the plugin ID. * * @return string @@ -74,23 +88,52 @@ abstract class PluginConfigEntity extends ConfigEntityBase implements PluginRequ } /** + * Returns the plugin instance. + * + * @return \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface|\Drupal\inmail\Plugin\inmail\Deliverer\DelivererInterface|\Drupal\inmail\Plugin\inmail\Handler\HandlerInterface + * The instantiated plugin. + */ + public function getPluginInstance() { + if (empty($this->pluginInstance)) { + return \Drupal::service('plugin.manager.inmail.' . $this->pluginType)->createInstance($this->plugin, $this->configuration); + } + + return $this->pluginInstance; + } + + /** + * Returns the plugin type. + * + * @return string + * The plugin type. + */ + public function getPluginType() { + return $this->pluginType; + } + + /** * {@inheritdoc} */ public function checkRequirements() { $requirements = []; - // @todo: Move to the entity level. - $entity_type_parameters = explode('_', $this->getEntityTypeId()); - $plugin_type = end($entity_type_parameters); - - /** @var \Drupal\Core\Plugin\DefaultPluginManager $plugin_manager */ - $plugin_manager = \Drupal::service("plugin.manager.inmail.$plugin_type"); - // Create plugin instance. - // @todo: Implement getPlugin() method. - if ($plugin = $plugin_manager->createInstance($this->getPluginId(), $this->getConfiguration())) { + + if ($plugin = $this->getPluginInstance()) { return $plugin->checkRequirements(); } return $requirements; } + /** + * {@inheritdoc} + */ + public function isAvailable() { + $is_available = FALSE; + if ($plugin = $this->getPluginInstance()) { + $is_available = $plugin->isAvailable(); + } + + return $is_available; + } + } diff --git a/src/MessageProcessor.php b/src/MessageProcessor.php index 2333b94..ef7abd3 100644 --- a/src/MessageProcessor.php +++ b/src/MessageProcessor.php @@ -115,7 +115,7 @@ class MessageProcessor implements MessageProcessorInterface { uasort($analyzer_configs, array($this->analyzerStorage->getEntityType()->getClass(), 'sort')); foreach ($analyzer_configs as $analyzer_config) { /** @var \Drupal\inmail\Entity\AnalyzerConfig $analyzer_config */ - if ($analyzer_config->status() && empty($analyzer_config->checkRequirements())) { + if ($analyzer_config->status() && $analyzer_config->isAvailable()) { /** @var \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerInterface $analyzer */ $analyzer = $this->analyzerManager->createInstance($analyzer_config->getPluginId(), $analyzer_config->getConfiguration()); $analyzer->analyze($message, $result); @@ -136,7 +136,7 @@ class MessageProcessor implements MessageProcessorInterface { // Handle message. foreach ($this->handlerStorage->loadMultiple() as $handler_config) { /** @var \Drupal\inmail\Entity\HandlerConfig $handler_config */ - if ($handler_config->status() && empty($handler_config->checkRequirements())) { + if ($handler_config->status() && $handler_config->isAvailable()) { /** @var \Drupal\inmail\Plugin\inmail\handler\HandlerInterface $handler */ $handler = $this->handlerManager->createInstance($handler_config->getPluginId(), $handler_config->getConfiguration()); $handler->invoke($message, $result); diff --git a/src/Plugin/inmail/Analyzer/AnalyzerBase.php b/src/Plugin/inmail/Analyzer/AnalyzerBase.php index d18a68d..f83e830 100644 --- a/src/Plugin/inmail/Analyzer/AnalyzerBase.php +++ b/src/Plugin/inmail/Analyzer/AnalyzerBase.php @@ -3,7 +3,7 @@ namespace Drupal\inmail\Plugin\inmail\Analyzer; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\PluginBase; +use Drupal\inmail\PluginBase; /** * Base class for message analyzer plugins. @@ -69,12 +69,4 @@ abstract class AnalyzerBase extends PluginBase implements AnalyzerInterface { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - /** - * {@inheritdoc} - */ - public function checkRequirements() { - // By default, plugins have no requirements. - return []; - } - } diff --git a/src/Plugin/inmail/Analyzer/AnalyzerInterface.php b/src/Plugin/inmail/Analyzer/AnalyzerInterface.php index 7891bd8..2d2af2f 100644 --- a/src/Plugin/inmail/Analyzer/AnalyzerInterface.php +++ b/src/Plugin/inmail/Analyzer/AnalyzerInterface.php @@ -6,7 +6,6 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\inmail\MIME\MessageInterface; -use Drupal\inmail\PluginRequirementsInterface; use Drupal\inmail\ProcessorResultInterface; /** @@ -14,7 +13,7 @@ use Drupal\inmail\ProcessorResultInterface; * * @ingroup analyzer */ -interface AnalyzerInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, PluginRequirementsInterface { +interface AnalyzerInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface { /** * Analyze the given message. diff --git a/src/Plugin/inmail/Deliverer/DelivererBase.php b/src/Plugin/inmail/Deliverer/DelivererBase.php index 8c9adea..412dce4 100644 --- a/src/Plugin/inmail/Deliverer/DelivererBase.php +++ b/src/Plugin/inmail/Deliverer/DelivererBase.php @@ -1,8 +1,7 @@ pluginDefinition['label']; } - /** - * {@inheritdoc} - */ - public function checkRequirements() { - // By default, plugins have no requirements. - return []; - } - } diff --git a/src/Plugin/inmail/Deliverer/DelivererInterface.php b/src/Plugin/inmail/Deliverer/DelivererInterface.php index 9df55d4..81a35ea 100644 --- a/src/Plugin/inmail/Deliverer/DelivererInterface.php +++ b/src/Plugin/inmail/Deliverer/DelivererInterface.php @@ -3,7 +3,6 @@ namespace Drupal\inmail\Plugin\inmail\Deliverer; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\inmail\PluginRequirementsInterface; /** * Defines methods for deliverers. @@ -12,7 +11,7 @@ use Drupal\inmail\PluginRequirementsInterface; * * @ingroup deliverer */ -interface DelivererInterface extends PluginInspectionInterface, PluginRequirementsInterface { +interface DelivererInterface extends PluginInspectionInterface { /** * Returns the deliverer label. diff --git a/src/Plugin/inmail/Handler/HandlerBase.php b/src/Plugin/inmail/Handler/HandlerBase.php index e85ecc1..5e3dce3 100644 --- a/src/Plugin/inmail/Handler/HandlerBase.php +++ b/src/Plugin/inmail/Handler/HandlerBase.php @@ -3,7 +3,7 @@ namespace Drupal\inmail\Plugin\inmail\Handler; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\PluginBase; +use Drupal\inmail\PluginBase; /** * Base class for message handler plugins. @@ -72,12 +72,4 @@ abstract class HandlerBase extends PluginBase implements HandlerInterface { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - /** - * {@inheritdoc} - */ - public function checkRequirements() { - // By default, plugins have no requirements. - return []; - } - } diff --git a/src/Plugin/inmail/Handler/HandlerInterface.php b/src/Plugin/inmail/Handler/HandlerInterface.php index 240657a..b89b065 100644 --- a/src/Plugin/inmail/Handler/HandlerInterface.php +++ b/src/Plugin/inmail/Handler/HandlerInterface.php @@ -6,7 +6,6 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\inmail\MIME\MessageInterface; -use Drupal\inmail\PluginRequirementsInterface; use Drupal\inmail\ProcessorResultInterface; /** @@ -14,7 +13,7 @@ use Drupal\inmail\ProcessorResultInterface; * * @ingroup handler */ -interface HandlerInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, PluginRequirementsInterface { +interface HandlerInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface { /** * Returns helpful explanation for using and configuring the handler. diff --git a/src/PluginBase.php b/src/PluginBase.php new file mode 100644 index 0000000..a5914e3 --- /dev/null +++ b/src/PluginBase.php @@ -0,0 +1,26 @@ +getAnalyzerResult(DefaultAnalyzerResult::TOPIC); - // Do the fake body update. This should not be executed because of the - // plugin requirements check. - $default_result->setBody('The body has been updated by MissingRequirementAnalyzer.'); + // Do the fake body update. This should not be executed as we only execute + // available analyzers. + $default_result->setBody('The body has been updated by UnavailableAnalyzer.'); } /** @@ -34,10 +34,17 @@ class MissingRequirementAnalyzer extends AnalyzerBase { */ public function checkRequirements() { return [ - 'title' => $this->t('Missing test extension'), - 'description' => $this->t('Missing Requirement Analyzer cannot be used because of missing test extension'), + 'title' => $this->t('Unavailable Analyzer'), + 'description' => $this->t('Unavailable Analyzer cannot be used.'), 'severity' => REQUIREMENT_ERROR, ]; } + /** + * {@inheritdoc} + */ + public function isAvailable() { + return FALSE; + } + } diff --git a/tests/src/Kernel/ProcessorTest.php b/tests/src/Kernel/ProcessorTest.php index d6a56e2..f385fb1 100644 --- a/tests/src/Kernel/ProcessorTest.php +++ b/tests/src/Kernel/ProcessorTest.php @@ -62,9 +62,9 @@ EOF; $processor = \Drupal::service('inmail.processor'); AnalyzerConfig::create(['id' => 'test_analyzer', 'plugin' => 'test_analyzer'])->save(); - // Add an analyzer with missing requirements. - $missing_requirement_analyzer = AnalyzerConfig::create(['id' => 'missing_requirement_analyzer', 'plugin' => 'missing_requirement_analyzer']); - $missing_requirement_analyzer->save(); + // Add an unavailable analyzer. + $unavailable_analyzer = AnalyzerConfig::create(['id' => 'unavailable_analyzer', 'plugin' => 'unavailable_analyzer']); + $unavailable_analyzer->save(); HandlerConfig::create(['id' => 'result_keeper', 'plugin' => 'result_keeper'])->save(); $processor->process($raw, DelivererConfig::create(['id' => 'test'])); @@ -80,11 +80,12 @@ EOF; // Assert the requirements message. $this->assertEquals([ - 'title' => t('Missing test extension'), - 'description' => t('Missing Requirement Analyzer cannot be used because of missing test extension'), + 'title' => t('Unavailable Analyzer'), + 'description' => t('Unavailable Analyzer cannot be used.'), 'severity' => REQUIREMENT_ERROR, - ], $missing_requirement_analyzer->checkRequirements()); - // The body message has not changed. It implies MissingRequirementAnalyzer + ], $unavailable_analyzer->checkRequirements()); + $this->assertEquals(FALSE, $unavailable_analyzer->isAvailable()); + // The body message has not changed. It implies UnavailableAnalyzer // did not run. $this->assertEquals('Hello world!', $default_result->getBody()); }