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@0
|
18 * @return array
|
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@0
|
33 * @todo: This needs to go into an interface.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @return array
|
Chris@0
|
36 * The typed data definition describing the configuration option, or FALSE
|
Chris@0
|
37 * if the option does not exist.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function getConfigDefinition($key) {
|
Chris@0
|
40 $definition = $this->getPluginDefinition();
|
Chris@0
|
41 if (!empty($definition['configuration'][$key])) {
|
Chris@0
|
42 return $definition['configuration'][$key];
|
Chris@0
|
43 }
|
Chris@0
|
44 return FALSE;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Gets all configuration values.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @todo: This needs to go into an interface.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @return array
|
Chris@0
|
53 * The array of all configuration values, keyed by configuration option
|
Chris@0
|
54 * name.
|
Chris@0
|
55 */
|
Chris@0
|
56 public function getConfig() {
|
Chris@0
|
57 return $this->configuration;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Sets the value of a particular configuration option.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $key
|
Chris@0
|
64 * The key of the configuration option to set.
|
Chris@0
|
65 * @param mixed $value
|
Chris@0
|
66 * The value to set.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @todo This doesn't belong here. Move this into a new base class in
|
Chris@0
|
69 * https://www.drupal.org/node/1764380.
|
Chris@0
|
70 * @todo This does not set a value in \Drupal::config(), so the name is confusing.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return \Drupal\Core\Executable\ExecutablePluginBase
|
Chris@0
|
73 * The executable object for chaining.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function setConfig($key, $value) {
|
Chris@0
|
76 if ($definition = $this->getConfigDefinition($key)) {
|
Chris@0
|
77 $typed_data = \Drupal::typedDataManager()->create($definition, $value);
|
Chris@0
|
78
|
Chris@0
|
79 if ($typed_data->validate()->count() > 0) {
|
Chris@0
|
80 throw new PluginException("The provided configuration value does not pass validation.");
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 $this->configuration[$key] = $value;
|
Chris@0
|
84 return $this;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 }
|