Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Executable;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Plugin\ContextAwarePluginBase;
|
Chris@0
|
6 use Drupal\Component\Plugin\Exception\PluginException;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides the basic architecture for executable plugins.
|
Chris@0
|
10 */
|
Chris@0
|
11 abstract class ExecutablePluginBase extends ContextAwarePluginBase implements ExecutableInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Gets an array of definitions of available configuration options.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @todo: This needs to go into an interface.
|
Chris@0
|
17 *
|
Chris@17
|
18 * @return \Drupal\Core\TypedData\DataDefinitionInterface[]
|
Chris@0
|
19 * An array of typed data definitions describing available configuration
|
Chris@0
|
20 * options, keyed by option name.
|
Chris@0
|
21 */
|
Chris@0
|
22 public function getConfigDefinitions() {
|
Chris@0
|
23 $definition = $this->getPluginDefinition();
|
Chris@0
|
24 if (!empty($definition['configuration'])) {
|
Chris@0
|
25 return $definition['configuration'];
|
Chris@0
|
26 }
|
Chris@0
|
27 return [];
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Gets the definition of a configuration option.
|
Chris@0
|
32 *
|
Chris@17
|
33 * @param string $key
|
Chris@17
|
34 * The key of the configuration option to get.
|
Chris@17
|
35 *
|
Chris@0
|
36 * @todo: This needs to go into an interface.
|
Chris@0
|
37 *
|
Chris@17
|
38 * @return \Drupal\Core\TypedData\DataDefinitionInterface|false
|
Chris@0
|
39 * The typed data definition describing the configuration option, or FALSE
|
Chris@0
|
40 * if the option does not exist.
|
Chris@0
|
41 */
|
Chris@0
|
42 public function getConfigDefinition($key) {
|
Chris@0
|
43 $definition = $this->getPluginDefinition();
|
Chris@0
|
44 if (!empty($definition['configuration'][$key])) {
|
Chris@0
|
45 return $definition['configuration'][$key];
|
Chris@0
|
46 }
|
Chris@0
|
47 return FALSE;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Gets all configuration values.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @todo: This needs to go into an interface.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @return array
|
Chris@0
|
56 * The array of all configuration values, keyed by configuration option
|
Chris@0
|
57 * name.
|
Chris@0
|
58 */
|
Chris@0
|
59 public function getConfig() {
|
Chris@0
|
60 return $this->configuration;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Sets the value of a particular configuration option.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @param string $key
|
Chris@0
|
67 * The key of the configuration option to set.
|
Chris@0
|
68 * @param mixed $value
|
Chris@0
|
69 * The value to set.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @todo This doesn't belong here. Move this into a new base class in
|
Chris@0
|
72 * https://www.drupal.org/node/1764380.
|
Chris@0
|
73 * @todo This does not set a value in \Drupal::config(), so the name is confusing.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return \Drupal\Core\Executable\ExecutablePluginBase
|
Chris@0
|
76 * The executable object for chaining.
|
Chris@17
|
77 *
|
Chris@17
|
78 * @throws \Drupal\Component\Plugin\Exception\PluginException
|
Chris@17
|
79 * If the provided configuration value does not pass validation.
|
Chris@0
|
80 */
|
Chris@0
|
81 public function setConfig($key, $value) {
|
Chris@0
|
82 if ($definition = $this->getConfigDefinition($key)) {
|
Chris@0
|
83 $typed_data = \Drupal::typedDataManager()->create($definition, $value);
|
Chris@0
|
84
|
Chris@0
|
85 if ($typed_data->validate()->count() > 0) {
|
Chris@0
|
86 throw new PluginException("The provided configuration value does not pass validation.");
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89 $this->configuration[$key] = $value;
|
Chris@0
|
90 return $this;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 }
|