Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
103
core/lib/Drupal/Component/Plugin/Context/Context.php
Normal file
103
core/lib/Drupal/Component/Plugin/Context/Context.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Component\Plugin\Context\Context.
|
||||
*/
|
||||
|
||||
namespace Drupal\Component\Plugin\Context;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\ContextException;
|
||||
use Symfony\Component\Validator\Constraints\Type;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
|
||||
/**
|
||||
* A generic context class for wrapping data a plugin needs to operate.
|
||||
*/
|
||||
class Context implements ContextInterface {
|
||||
|
||||
/**
|
||||
* The value of the context.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $contextValue;
|
||||
|
||||
/**
|
||||
* The definition to which a context must conform.
|
||||
*
|
||||
* @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface
|
||||
*/
|
||||
protected $contextDefinition;
|
||||
|
||||
/**
|
||||
* Sets the contextDefinition for us without needing to call the setter.
|
||||
*
|
||||
* @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
|
||||
* The context definition.
|
||||
*/
|
||||
public function __construct(ContextDefinitionInterface $context_definition) {
|
||||
$this->contextDefinition = $context_definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextValue().
|
||||
*/
|
||||
public function setContextValue($value) {
|
||||
$this->contextValue = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextValue().
|
||||
*/
|
||||
public function getContextValue() {
|
||||
// Support optional contexts.
|
||||
if (!isset($this->contextValue)) {
|
||||
$definition = $this->getContextDefinition();
|
||||
$default_value = $definition->getDefaultValue();
|
||||
|
||||
if (!isset($default_value) && $definition->isRequired()) {
|
||||
$type = $definition->getDataType();
|
||||
throw new ContextException(sprintf("The %s context is required and not present.", $type));
|
||||
}
|
||||
// Keep the default value here so that subsequent calls don't have to look
|
||||
// it up again.
|
||||
$this->contextValue = $default_value;
|
||||
}
|
||||
return $this->contextValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setContextDefinition(ContextDefinitionInterface $context_definition) {
|
||||
$this->contextDefinition = $context_definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextDefinition().
|
||||
*/
|
||||
public function getContextDefinition() {
|
||||
return $this->contextDefinition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Context\ContextInterface::getConstraints().
|
||||
*/
|
||||
public function getConstraints() {
|
||||
if (empty($this->contextDefinition['class'])) {
|
||||
throw new ContextException("An error was encountered while trying to validate the context.");
|
||||
}
|
||||
return array(new Type($this->contextDefinition['class']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Context\ContextInterface::validate().
|
||||
*/
|
||||
public function validate() {
|
||||
$validator = Validation::createValidatorBuilder()
|
||||
->getValidator();
|
||||
return $validator->validateValue($this->getContextValue(), $this->getConstraints());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,179 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Component\Plugin\Context\ContextDefinitionInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Component\Plugin\Context;
|
||||
|
||||
/**
|
||||
* Interface for context definitions.
|
||||
*
|
||||
* @todo WARNING: This interface is going to receive some additions as part of
|
||||
* https://www.drupal.org/node/2346999.
|
||||
*/
|
||||
interface ContextDefinitionInterface {
|
||||
|
||||
/**
|
||||
* Gets a human readable label.
|
||||
*
|
||||
* @return string
|
||||
* The label.
|
||||
*/
|
||||
public function getLabel();
|
||||
|
||||
/**
|
||||
* Sets the human readable label.
|
||||
*
|
||||
* @param string $label
|
||||
* The label to set.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel($label);
|
||||
|
||||
/**
|
||||
* Gets a human readable description.
|
||||
*
|
||||
* @return string|null
|
||||
* The description, or NULL if no description is available.
|
||||
*/
|
||||
public function getDescription();
|
||||
|
||||
/**
|
||||
* Sets the human readable description.
|
||||
*
|
||||
* @param string|null $description
|
||||
* The description to set.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDescription($description);
|
||||
|
||||
/**
|
||||
* Gets the data type needed by the context.
|
||||
*
|
||||
* If the context is multiple-valued, this represents the type of each value.
|
||||
*
|
||||
* @return string
|
||||
* The data type.
|
||||
*/
|
||||
public function getDataType();
|
||||
|
||||
/**
|
||||
* Sets the data type needed by the context.
|
||||
*
|
||||
* @param string $data_type
|
||||
* The data type to set.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDataType($data_type);
|
||||
|
||||
/**
|
||||
* Determines whether the data is multi-valued, i.e. a list of data items.
|
||||
*
|
||||
* @return bool
|
||||
* Whether the data is multi-valued; i.e. a list of data items.
|
||||
*/
|
||||
public function isMultiple();
|
||||
|
||||
/**
|
||||
* Sets whether the data is multi-valued.
|
||||
*
|
||||
* @param bool $multiple
|
||||
* (optional) Whether the data is multi-valued. Defaults to TRUE.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMultiple($multiple = TRUE);
|
||||
|
||||
/**
|
||||
* Determines whether the context is required.
|
||||
*
|
||||
* For required data a non-NULL value is mandatory.
|
||||
*
|
||||
* @return bool
|
||||
* Whether a data value is required.
|
||||
*/
|
||||
public function isRequired();
|
||||
|
||||
/**
|
||||
* Sets whether the data is required.
|
||||
*
|
||||
* @param bool $required
|
||||
* (optional) Whether the data is multi-valued. Defaults to TRUE.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRequired($required = TRUE);
|
||||
|
||||
/**
|
||||
* Gets the default value for this context definition.
|
||||
*
|
||||
* @return mixed
|
||||
* The default value or NULL if no default value is set.
|
||||
*/
|
||||
public function getDefaultValue();
|
||||
|
||||
/**
|
||||
* Sets the default data value.
|
||||
*
|
||||
* @param mixed $default_value
|
||||
* The default value to be set or NULL to remove any default value.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefaultValue($default_value);
|
||||
|
||||
/**
|
||||
* Gets an array of validation constraints.
|
||||
*
|
||||
* @return array
|
||||
* An array of validation constraint definitions, keyed by constraint name.
|
||||
* Each constraint definition can be used for instantiating
|
||||
* \Symfony\Component\Validator\Constraint objects.
|
||||
*/
|
||||
public function getConstraints();
|
||||
|
||||
/**
|
||||
* Sets the array of validation constraints.
|
||||
*
|
||||
* NOTE: This will override any previously set constraints. In most cases
|
||||
* ContextDefinitionInterface::addConstraint() should be used instead.
|
||||
*
|
||||
* @param array $constraints
|
||||
* The array of constraints.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see self::addConstraint()
|
||||
*/
|
||||
public function setConstraints(array $constraints);
|
||||
|
||||
/**
|
||||
* Adds a validation constraint.
|
||||
*
|
||||
* @param string $constraint_name
|
||||
* The name of the constraint to add, i.e. its plugin id.
|
||||
* @param array|null $options
|
||||
* The constraint options as required by the constraint plugin, or NULL.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addConstraint($constraint_name, $options = NULL);
|
||||
|
||||
/**
|
||||
* Gets a validation constraint.
|
||||
*
|
||||
* @param string $constraint_name
|
||||
* The name of the constraint, i.e. its plugin id.
|
||||
*
|
||||
* @return array
|
||||
* A validation constraint definition which can be used for instantiating a
|
||||
* \Symfony\Component\Validator\Constraint object.
|
||||
*/
|
||||
public function getConstraint($constraint_name);
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Component\Plugin\Context\ContextInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Component\Plugin\Context;
|
||||
|
||||
/**
|
||||
* A generic context interface for wrapping data a plugin needs to operate.
|
||||
*/
|
||||
interface ContextInterface {
|
||||
|
||||
/**
|
||||
* Sets the context value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* The value of this context, matching the context definition.
|
||||
*
|
||||
* @see \Drupal\Component\Plugin\Context\ContextInterface::setContextDefinition().
|
||||
*/
|
||||
public function setContextValue($value);
|
||||
|
||||
/**
|
||||
* Gets the context value.
|
||||
*
|
||||
* @return mixed
|
||||
* The currently set context value, or NULL if it is not set.
|
||||
*/
|
||||
public function getContextValue();
|
||||
|
||||
/**
|
||||
* Sets the definition that the context must conform to.
|
||||
*
|
||||
* @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
|
||||
* A defining characteristic representation of the context against which
|
||||
* that context can be validated.
|
||||
*/
|
||||
public function setContextDefinition(ContextDefinitionInterface $context_definition);
|
||||
|
||||
/**
|
||||
* Gets the provided definition that the context must conform to.
|
||||
*
|
||||
* @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
|
||||
* The defining characteristic representation of the context.
|
||||
*/
|
||||
public function getContextDefinition();
|
||||
|
||||
/**
|
||||
* Gets a list of validation constraints.
|
||||
*
|
||||
* @return array
|
||||
* Array of constraints, each being an instance of
|
||||
* \Symfony\Component\Validator\Constraint.
|
||||
*/
|
||||
public function getConstraints();
|
||||
|
||||
/**
|
||||
* Validates the set context value.
|
||||
*
|
||||
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
|
||||
* A list of constraint violations. If the list is empty, validation
|
||||
* succeeded.
|
||||
*/
|
||||
public function validate();
|
||||
|
||||
}
|
Reference in a new issue