Last active
December 15, 2015 14:29
-
-
Save sdboyer/5275238 to your computer and use it in GitHub Desktop.
quick POC of a config-sensitive plugin factory, and the config and plugin interfaces it entails. even if we don't do this, i think it's worth noting that i think we'd be much better served in general if we tried to do less constructor injection with plugins - setter injection is typically fine. the only drawback to it is that it could be set aga…
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
<?php | |
/** | |
* @file | |
* Contains Drupal\Core\Plugin\Factory\ConfigDrivenFactory. | |
*/ | |
namespace Drupal\Core\Plugin\Factory; | |
use Drupal\Component\Plugin\Discovery\DiscoveryInterface; | |
use Drupal\Component\Plugin\Factory\DefaultFactory; | |
use Drupal\Core\Config\Entity\ConfigStorageController; | |
class ConfigDrivenFactory implements ConfigDrivenFactoryInterface { | |
/** | |
* The object that retrieves the definitions of the plugins that this factory instantiates. | |
* | |
* The plugin definition includes the plugin class and possibly other | |
* information necessary for proper instantiation. | |
* | |
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface | |
*/ | |
protected $discovery; | |
/** | |
* The storage controller for the type of ConfigEntity this factory loads. | |
* | |
* @var \Drupal\Core\Config\Entity\ConfigStorageController | |
*/ | |
protected $storageController; | |
/** | |
* Constructs a ConfigDrivenFactory. | |
* | |
* @param DiscoveryInterface $discovery | |
* The discovery service used to resolve plugin ids. | |
* @param ConfigStorageController $storageController | |
* The storage controller for the type of ConfigEntity to load. | |
*/ | |
public function __construct(DiscoveryInterface $discovery, ConfigStorageController $storageController) { | |
$this->discovery = $discovery; | |
$this->storageController = $storageController; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function createInstanceFromConfig($config_id) { | |
$config = $this->storageController->load($config_id); | |
$plugin_id = $config->getPluginId(); | |
$plugin_class = DefaultFactory::getPluginClass($config->getPluginId(), $this->discovery); | |
$instance = new $plugin_class($plugin_id); | |
$instance->setConfig($config); | |
return $instance; | |
} | |
} |
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
<?php | |
/** | |
* @file | |
* Contains Drupal\Core\Plugin\Factory\ConfigDrivenFactoryInterface. | |
*/ | |
namespace Drupal\Core\Plugin\Factory; | |
use Drupal\Core\Plugin\ConfigEntityAwarePluginInterface; | |
/** | |
* Factory interface for plugins that use ConfigEntity for their configuration. | |
*/ | |
interface ConfigDrivenFactoryInterface { | |
/** | |
* Returns a configured instance of a plugin. | |
* | |
* @param string $config_id | |
* An array of configuration relevant to the plugin instance. | |
* | |
* @return ConfigEntityAwarePluginInterface | |
* A fully configured plugin instance. | |
*/ | |
public function createInstanceFromConfig($config_id); | |
} |
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
<?php | |
/** | |
* @file | |
* Contains Drupal\Core\Plugin\ConfigEntityAwarePluginInterface. | |
*/ | |
namespace Drupal\Core\Plugin; | |
use Drupal\Core\Config\Entity\EmbeddedPluginConfigInterface; | |
/** | |
* Interface for plugins that use ConfigEntity for their configuration storage. | |
*/ | |
interface ConfigEntityAwarePluginInterface { | |
/** | |
* Sets the ConfigEntity for this plugin. | |
* | |
* @param EmbeddedPluginConfigInterface $config | |
* The ConfigEntity object. | |
* | |
* @return void | |
*/ | |
public function setConfig(EmbeddedPluginConfigInterface $config); | |
/** | |
* Returns the ConfigEntity for this plugin. | |
* | |
* @return EmbeddedPluginConfigInterface | |
*/ | |
public function getConfig(); | |
} |
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
<?php | |
/** | |
* @file | |
* Contains Drupal\Core\Config\Entity\EmbeddedPluginConfigInterface. | |
*/ | |
namespace Drupal\Core\Config\Entity; | |
/** | |
* Interface for ConfigEntities that are used as embedded config by plugins. | |
*/ | |
interface EmbeddedPluginConfigInterface extends ConfigEntityInterface { | |
/** | |
* Returns the id of the plugin in which this configuration is embedded. | |
* | |
* @return string | |
*/ | |
public function getPluginId(); | |
/** | |
* Sets the id of the plugin in which this configuration is embedded. | |
* | |
* @param string $plugin_id | |
* The id of the plugin. | |
* | |
* @return void | |
*/ | |
public function setPluginId($plugin_id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment