diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php new file mode 100644 index 0000000..5650707 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -0,0 +1,52 @@ +contextDefinition = $contextDefinition; + } + + public function setContext($context) { + $context = $this->validate($context); + $this->context = $context; + } + + public function getContext() { + return $this->context; + } + + public function validate($context) { + if (is_string($this->contextDefinition)) { + if ($context instanceof $this->contextDefinition) { + return $context; + } + throw new ContextException("The context passed for $key was not an instance of $this->contextDefinition."); + } + throw new ContextException("An error was encountered while trying to validate the context for $key."); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php b/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php new file mode 100644 index 0000000..d7555d9 --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/ContextualPluginBase.php @@ -0,0 +1,87 @@ +getDefinition(); + return !empty($definition['context']) ? $definition['context'] : NULL; + } + + /** + * Returns the a specific context class definition of the plugin. + * + * @return string + * The name of a class to which the set context must conform. + */ + public function getContextDefinition($key) { + $definition = $this->getDefinition(); + if (empty($definition['context'][$key])) { + throw new PluginException("The $key context is not a valid context."); + } + return $definition['context'][$key]; + } + + /** + * Returns the set values for all defined contexts. + * + * @return array + * Returns the array of all set contexts. + */ + public function getContexts() { + if (empty($this->configuration['context'])) { + throw new PluginException("There are no set contexts."); + } + return $this->configuration['context']; + } + + /** + * Returns the set value for a defined context. + * + * @return object + * Returns instantiated object of a context. + */ + public function getContext($key) { + $definition = $this->getDefinition(); + if (empty($definition['context'][$key])) { + throw new PluginException("The $key context is not a valid context."); + } + if (empty($this->configuration['context'][$key])) { + throw new PluginException("The $key context is not yet set."); + } + + return $this->configuration['context'][$key]; + } + + /** + * Sets a defined context to the passed value. + * + * @return $this + * Returns the plugin instance. + */ + public function setContext($key, $value) { + $class = $this->getContextDefinition($key); + $this->configuration['context'][$key] = new Context($class); + $this->configuration['context'][$key]->setContext($value); + + return $this; + } + +} diff --git a/core/lib/Drupal/Component/Plugin/Exception/ContextException.php b/core/lib/Drupal/Component/Plugin/Exception/ContextException.php new file mode 100644 index 0000000..fbb1c2d --- /dev/null +++ b/core/lib/Drupal/Component/Plugin/Exception/ContextException.php @@ -0,0 +1,15 @@ +contextDefinition)) { + if ($context instanceof $this->contextDefinition) { + return $context; + } + throw new ContextException("The context passed for $key was not an instance of $this->contextDefinition."); + } + // Check to see if we have a typed data definition instead of a class name. + if (is_array($this->contextDefinition)) { + $typed_data = typed_data()->create($this->contextDefinition, $context); + // If we do have a typed data definition, validate it and return the + // typed data instance instead. + if ($typed_data->validate()) { + return $typed_data; + } + throw new ContextException("The context passed for $key could not be validated through typed data."); + } + throw new ContextException("An error was encountered while trying to validate the context for $key."); + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php b/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php new file mode 100644 index 0000000..6516651 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContextualPluginBase.php @@ -0,0 +1,28 @@ +getContextDefinition($key); + $this->configuration['context'][$key] = new Context($class); + $this->configuration['context'][$key]->setContext($value); + + return $this; + } + +}