View source
<?php
namespace Drupal\Core\TypedData;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\TypedData\Validation\ExecutionContextFactory;
use Drupal\Core\TypedData\Validation\RecursiveValidator;
use Drupal\Core\Validation\ConstraintManager;
use Drupal\Core\Validation\ConstraintValidatorFactory;
use Drupal\Core\Validation\DrupalTranslator;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class TypedDataManager extends DefaultPluginManager implements TypedDataManagerInterface {
use DependencySerializationTrait;
protected $validator;
protected $constraintManager;
protected $prototypes = [];
protected $classResolver;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) {
$this
->alterInfo('data_type_info');
$this
->setCacheBackend($cache_backend, 'typed_data_types_plugins');
$this->classResolver = $class_resolver;
parent::__construct('Plugin/DataType', $namespaces, $module_handler, NULL, 'Drupal\\Core\\TypedData\\Annotation\\DataType');
}
public function createInstance($data_type, array $configuration = []) {
$data_definition = $configuration['data_definition'];
$type_definition = $this
->getDefinition($data_type);
if (!isset($type_definition)) {
throw new \InvalidArgumentException("Invalid data type '{$data_type}' has been given");
}
$class = $data_definition
->getClass();
if (!isset($class)) {
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
}
$typed_data = $class::createInstance($data_definition, $configuration['name'], $configuration['parent']);
$typed_data
->setTypedDataManager($this);
return $typed_data;
}
public function create(DataDefinitionInterface $definition, $value = NULL, $name = NULL, $parent = NULL) {
$typed_data = $this
->createInstance($definition
->getDataType(), [
'data_definition' => $definition,
'name' => $name,
'parent' => $parent,
]);
if (isset($value)) {
$typed_data
->setValue($value, FALSE);
}
return $typed_data;
}
public function createDataDefinition($data_type) {
$type_definition = $this
->getDefinition($data_type);
if (!isset($type_definition)) {
throw new \InvalidArgumentException("Invalid data type '{$data_type}' has been given");
}
$class = $type_definition['definition_class'];
$data_definition = $class::createFromDataType($data_type);
if (method_exists($data_definition, 'setTypedDataManager')) {
$data_definition
->setTypedDataManager($this);
}
return $data_definition;
}
public function createListDataDefinition($item_type) {
$type_definition = $this
->getDefinition($item_type);
if (!isset($type_definition)) {
throw new \InvalidArgumentException("Invalid data type '{$item_type}' has been given");
}
$class = $type_definition['list_definition_class'];
return $class::createFromItemType($item_type);
}
public function getInstance(array $options) {
return $this
->getPropertyInstance($options['object'], $options['property'], $options['value']);
}
public function getPropertyInstance(TypedDataInterface $object, $property_name, $value = NULL) {
$root_definition = $object
->getRoot()
->getDataDefinition();
if ($root_definition instanceof ListDataDefinition) {
$root_definition = $root_definition
->getItemDefinition();
}
$parts[] = $root_definition
->getDataType();
if ($settings = $root_definition
->getSettings()) {
$parts[] = json_encode($settings);
}
$parts[] = $object
->getPropertyPath();
if ($object instanceof ComplexDataInterface) {
$parts[] = $property_name;
}
$key = implode(':', $parts);
if (!isset($this->prototypes[$key])) {
if ($object instanceof ComplexDataInterface) {
$definition = $object
->getDataDefinition()
->getPropertyDefinition($property_name);
}
elseif ($object instanceof ListInterface) {
$definition = $object
->getItemDefinition();
}
else {
throw new \InvalidArgumentException("The passed object has to either implement the ComplexDataInterface or the ListInterface.");
}
if (!$definition) {
throw new \InvalidArgumentException("Property {$property_name} is unknown.");
}
$this->prototypes[$key] = $this
->create($definition, NULL, $property_name, $object);
}
$property = clone $this->prototypes[$key];
$property
->setContext($property_name, $object);
if (isset($value)) {
$property
->setValue($value, FALSE);
}
return $property;
}
public function setValidator(ValidatorInterface $validator) {
$this->validator = $validator;
}
public function getValidator() {
if (!isset($this->validator)) {
$this->validator = new RecursiveValidator(new ExecutionContextFactory(new DrupalTranslator()), new ConstraintValidatorFactory($this->classResolver), $this);
}
return $this->validator;
}
public function setValidationConstraintManager(ConstraintManager $constraintManager) {
$this->constraintManager = $constraintManager;
}
public function getValidationConstraintManager() {
return $this->constraintManager;
}
public function getDefaultConstraints(DataDefinitionInterface $definition) {
$constraints = [];
$type_definition = $this
->getDefinition($definition
->getDataType());
if (is_subclass_of($type_definition['class'], '\\Drupal\\Core\\TypedData\\PrimitiveInterface')) {
$constraints['PrimitiveType'] = [];
}
if (isset($type_definition['constraints'])) {
$constraints += $type_definition['constraints'];
}
if ($definition
->isRequired()) {
$constraints['NotNull'] = [];
}
if (is_subclass_of($definition
->getClass(), 'Drupal\\Core\\TypedData\\OptionsProviderInterface')) {
$constraints['AllowedValues'] = [];
}
return $constraints;
}
public function clearCachedDefinitions() {
parent::clearCachedDefinitions();
$this->prototypes = [];
}
public function getCanonicalRepresentation(TypedDataInterface $data) {
$data_definition = $data
->getDataDefinition();
if ($data_definition instanceof ListDataDefinitionInterface) {
$data_definition = $data_definition
->getItemDefinition();
}
$type_definition = $this
->getDefinition($data_definition
->getDataType());
if (!empty($type_definition['unwrap_for_canonical_representation'])) {
return $data
->getValue();
}
return $data;
}
}