diff --git a/config/install/sms.gateway.config.yml b/config/install/sms.gateway.config.yml deleted file mode 100644 index aa353e0..0000000 --- a/config/install/sms.gateway.config.yml +++ /dev/null @@ -1,3 +0,0 @@ -log: - enabled: true - plugin_id: log diff --git a/config/install/sms.gateway.log.yml b/config/install/sms.gateway.log.yml new file mode 100644 index 0000000..7de765f --- /dev/null +++ b/config/install/sms.gateway.log.yml @@ -0,0 +1,3 @@ +# Default configuration for log gateway. +enabled: true +plugin_id: log diff --git a/config/schema/sms.schema.yml b/config/schema/sms.schema.yml index 91dff02..9979935 100644 --- a/config/schema/sms.schema.yml +++ b/config/schema/sms.schema.yml @@ -20,15 +20,9 @@ sms.settings: - type: string # Configuration for sms gateways. -sms.gateway.config: - type: sequence - label: 'All SMS Gateway configuration' - sequence: - - type: sms.gateway.setting - -sms.gateway.setting: - type: mapping - label: 'Single SMS Gateway configuration' +sms.gateway.*: + type: config_object + label: 'SMS Gateway configuration' mapping: label: type: label diff --git a/src/Gateway/GatewayManager.php b/src/Gateway/GatewayManager.php index 95beef9..e865dc3 100644 --- a/src/Gateway/GatewayManager.php +++ b/src/Gateway/GatewayManager.php @@ -23,7 +23,7 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter * * @var \Drupal\Core\Config\ConfigFactory */ - protected $config; + protected $configFactory; /** * List of gateways managed by this gateway manager. @@ -51,19 +51,19 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter * * @param \Traversable $namespaces * The namespaces to search for the gateway plugins. - * @param \Drupal\Core\Config\ConfigFactory + * @param \Drupal\Core\Config\ConfigFactory $config_factory * Handles the instantiation of gateways based on stored configuration. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend * Cache backend instance to use. * @param \Drupal\Core\Extension\ModuleHandlerInterface * Module handler for calling module hooks. */ - public function __construct(\Traversable $namespaces, ConfigFactory $config, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { + public function __construct(\Traversable $namespaces, ConfigFactory $config_factory, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { parent::__construct('Plugin/Gateway', $namespaces, $module_handler, 'Drupal\sms\Gateway\GatewayInterface', 'Drupal\sms\Annotation\SmsGateway'); $this->setCacheBackend($cache_backend, 'sms_gateways'); $this->alterInfo('sms_gateway_info'); - $this->config = $config; - $this->defaultGateway = $this->config->get('sms.settings')->get('default_gateway'); + $this->configFactory = $config_factory; + $this->defaultGateway = $this->configFactory->get('sms.settings')->get('default_gateway'); } /** @@ -92,7 +92,7 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter */ public function getDefaultGateway() { if (!isset($this->defaultGateway)) { - $this->defaultGateway = $this->config->get('sms.settings') + $this->defaultGateway = $this->configFactory->get('sms.settings') ->get('default_gateway'); } return $this->getGateway($this->defaultGateway); @@ -105,7 +105,7 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter // Cannot make a disabled gateway default. if ($this->getGateway($gateway_id)->isEnabled()) { $this->defaultGateway = $gateway_id; - $this->config->getEditable('sms.settings') + $this->configFactory->getEditable('sms.settings') ->set('default_gateway', $this->defaultGateway) ->save(); } @@ -164,12 +164,12 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter * {@inheritdoc} */ public function saveGateways(array $gateways) { - $config = $this->config->getEditable('sms.gateway.config'); /** @var \Drupal\sms\Gateway\GatewayInterface $gateway */ foreach ($gateways as $gateway) { - $config->set($gateway->getName(), $gateway->getConfiguration()); + $this->configFactory->getEditable('sms.gateway.' . $gateway->getName()) + ->setData($gateway->getConfiguration()) + ->save(); } - $config->save(); // @todo Implement more granular cache invalidations. // Clear static caches. $this->gateways = NULL; @@ -204,13 +204,18 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter */ protected function buildGateways() { // Get configured gateways. - $instance_configs = $this->config->get('sms.gateway.config')->getRawData(); - foreach ($instance_configs as $id => $instance_config) { + $names = $this->configFactory->listAll('sms.gateway.'); + foreach ($this->configFactory->loadMultiple($names) as $id => $instance_config) { + $id = substr($id, 12); + $settings = $instance_config->getRawData(); + // Set some sane defaults. + $settings += ['name' => $id]; + // Note that DefaultFactory::createInstance will get the right definitions. - if (!isset($instance_config['plugin_id'])) { + if (!isset($settings['plugin_id'])) { throw new \InvalidArgumentException(sprintf('Gateway "%s" configured without a plugin id.', $id)); } - $this->gateways[$id] = $this->createInstance($instance_config['plugin_id'], $instance_config); + $this->gateways[$id] = $this->createInstance($settings['plugin_id'], $settings); $this->names[$id] = $this->gateways[$id]->getLabel(); } } diff --git a/src/Tests/SmsFrameworkWebTest.php b/src/Tests/SmsFrameworkWebTest.php index 6683724..99e5f8d 100644 --- a/src/Tests/SmsFrameworkWebTest.php +++ b/src/Tests/SmsFrameworkWebTest.php @@ -54,6 +54,9 @@ class SmsFrameworkWebTest extends SmsFrameworkWebTestBase { for ($i = 0; $i < 3; $i++) { $name = $this->randomMachineName(); $this->gatewayManager->addGateway('test', ['name' => $name]); + // GatewayManagerInterface::getAvailableGateways() sorts by the names + // before adding, so we need to simulate in the expected result. + sort($gateways); $gateways[] = $name; $this->assertEqual($gateways, array_keys($this->gatewayManager->getAvailableGateways())); } diff --git a/src/Tests/SmsFrameworkWebTestBase.php b/src/Tests/SmsFrameworkWebTestBase.php index b4aabec..e718097 100644 --- a/src/Tests/SmsFrameworkWebTestBase.php +++ b/src/Tests/SmsFrameworkWebTestBase.php @@ -29,7 +29,7 @@ abstract class SmsFrameworkWebTestBase extends WebTestBase { public function setUp() { parent::setUp(); $this->gatewayManager = $this->container->get('plugin.manager.sms_gateway'); - // Add an instance of test gateway and set it as default. + // Add an instance of test gateway. $this->gatewayManager->addGateway('test', ['name' => 'test']); }