PageRenderTime 23ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/TypedData/TypedDataManager.php

https://gitlab.com/geeta7/drupal
PHP | 294 lines | 155 code | 30 blank | 109 comment | 19 complexity | 186af9d95565a9e938ab47f27d75dbc7 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\TypedData\TypedDataManager.
  5. */
  6. namespace Drupal\Core\TypedData;
  7. use Drupal\Component\Plugin\Exception\PluginException;
  8. use Drupal\Core\Cache\CacheBackendInterface;
  9. use Drupal\Core\DependencyInjection\ClassResolverInterface;
  10. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  11. use Drupal\Core\Extension\ModuleHandlerInterface;
  12. use Drupal\Core\Plugin\DefaultPluginManager;
  13. use Drupal\Core\TypedData\Validation\ExecutionContextFactory;
  14. use Drupal\Core\TypedData\Validation\RecursiveValidator;
  15. use Drupal\Core\Validation\ConstraintManager;
  16. use Drupal\Core\Validation\ConstraintValidatorFactory;
  17. use Drupal\Core\Validation\DrupalTranslator;
  18. use Symfony\Component\Validator\Validator\ValidatorInterface;
  19. /**
  20. * Manages data type plugins.
  21. */
  22. class TypedDataManager extends DefaultPluginManager implements TypedDataManagerInterface {
  23. use DependencySerializationTrait;
  24. /**
  25. * The validator used for validating typed data.
  26. *
  27. * @var \Symfony\Component\Validator\Validator\ValidatorInterface
  28. */
  29. protected $validator;
  30. /**
  31. * The validation constraint manager to use for instantiating constraints.
  32. *
  33. * @var \Drupal\Core\Validation\ConstraintManager
  34. */
  35. protected $constraintManager;
  36. /**
  37. * An array of typed data property prototypes.
  38. *
  39. * @var array
  40. */
  41. protected $prototypes = array();
  42. /**
  43. * The class resolver.
  44. *
  45. * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
  46. */
  47. protected $classResolver;
  48. /**
  49. * Constructs a new TypedDataManager.
  50. *
  51. * @param \Traversable $namespaces
  52. * An object that implements \Traversable which contains the root paths
  53. * keyed by the corresponding namespace to look for plugin implementations.
  54. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  55. * Cache backend instance to use.
  56. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  57. * The module handler.
  58. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
  59. * The class resolver.
  60. */
  61. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) {
  62. $this->alterInfo('data_type_info');
  63. $this->setCacheBackend($cache_backend, 'typed_data_types_plugins');
  64. $this->classResolver = $class_resolver;
  65. parent::__construct('Plugin/DataType', $namespaces, $module_handler, NULL, 'Drupal\Core\TypedData\Annotation\DataType');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function createInstance($data_type, array $configuration = array()) {
  71. $data_definition = $configuration['data_definition'];
  72. $type_definition = $this->getDefinition($data_type);
  73. if (!isset($type_definition)) {
  74. throw new \InvalidArgumentException("Invalid data type '$data_type' has been given");
  75. }
  76. // Allow per-data definition overrides of the used classes, i.e. take over
  77. // classes specified in the type definition.
  78. $class = $data_definition->getClass();
  79. if (!isset($class)) {
  80. throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
  81. }
  82. $typed_data = $class::createInstance($data_definition, $configuration['name'], $configuration['parent']);
  83. $typed_data->setTypedDataManager($this);
  84. return $typed_data;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function create(DataDefinitionInterface $definition, $value = NULL, $name = NULL, $parent = NULL) {
  90. $typed_data = $this->createInstance($definition->getDataType(), array(
  91. 'data_definition' => $definition,
  92. 'name' => $name,
  93. 'parent' => $parent,
  94. ));
  95. if (isset($value)) {
  96. $typed_data->setValue($value, FALSE);
  97. }
  98. return $typed_data;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function createDataDefinition($data_type) {
  104. $type_definition = $this->getDefinition($data_type);
  105. if (!isset($type_definition)) {
  106. throw new \InvalidArgumentException("Invalid data type '$data_type' has been given");
  107. }
  108. $class = $type_definition['definition_class'];
  109. return $class::createFromDataType($data_type);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function createListDataDefinition($item_type) {
  115. $type_definition = $this->getDefinition($item_type);
  116. if (!isset($type_definition)) {
  117. throw new \InvalidArgumentException("Invalid data type '$item_type' has been given");
  118. }
  119. $class = $type_definition['list_definition_class'];
  120. return $class::createFromItemType($item_type);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getInstance(array $options) {
  126. return $this->getPropertyInstance($options['object'], $options['property'], $options['value']);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getPropertyInstance(TypedDataInterface $object, $property_name, $value = NULL) {
  132. // For performance, try to reuse existing prototypes instead of
  133. // constructing new objects when possible. A prototype is reused when
  134. // creating a data object:
  135. // - for a similar root object (same data type and settings),
  136. // - at the same property path under that root object.
  137. $root_definition = $object->getRoot()->getDataDefinition();
  138. // If the root object is a list, we want to look at the data type and the
  139. // settings of its item definition.
  140. if ($root_definition instanceof ListDataDefinition) {
  141. $root_definition = $root_definition->getItemDefinition();
  142. }
  143. // Root data type and settings.
  144. $parts[] = $root_definition->getDataType();
  145. if ($settings = $root_definition->getSettings()) {
  146. // Hash the settings into a string. crc32 is the fastest way to hash
  147. // something for non-cryptographic purposes.
  148. $parts[] = hash('crc32b', serialize($settings));
  149. }
  150. // Property path for the requested data object. When creating a list item,
  151. // use 0 in the key as all items look the same.
  152. $parts[] = $object->getPropertyPath() . '.' . (is_numeric($property_name) ? 0 : $property_name);
  153. $key = implode(':', $parts);
  154. // Create the prototype if needed.
  155. if (!isset($this->prototypes[$key])) {
  156. // Fetch the data definition for the child object from the parent.
  157. if ($object instanceof ComplexDataInterface) {
  158. $definition = $object->getDataDefinition()->getPropertyDefinition($property_name);
  159. }
  160. elseif ($object instanceof ListInterface) {
  161. $definition = $object->getItemDefinition();
  162. }
  163. else {
  164. throw new \InvalidArgumentException("The passed object has to either implement the ComplexDataInterface or the ListInterface.");
  165. }
  166. if (!$definition) {
  167. throw new \InvalidArgumentException("Property $property_name is unknown.");
  168. }
  169. // Create the prototype without any value, but with initial parenting
  170. // so that constructors can set up the objects correclty.
  171. $this->prototypes[$key] = $this->create($definition, NULL, $property_name, $object);
  172. }
  173. // Clone the prototype, update its parenting information, and assign the
  174. // value.
  175. $property = clone $this->prototypes[$key];
  176. $property->setContext($property_name, $object);
  177. if (isset($value)) {
  178. $property->setValue($value, FALSE);
  179. }
  180. return $property;
  181. }
  182. /**
  183. * Sets the validator for validating typed data.
  184. *
  185. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  186. * The validator object to set.
  187. */
  188. public function setValidator(ValidatorInterface $validator) {
  189. $this->validator = $validator;
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function getValidator() {
  195. if (!isset($this->validator)) {
  196. $this->validator = new RecursiveValidator(
  197. new ExecutionContextFactory(new DrupalTranslator()),
  198. new ConstraintValidatorFactory($this->classResolver),
  199. $this
  200. );
  201. }
  202. return $this->validator;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function setValidationConstraintManager(ConstraintManager $constraintManager) {
  208. $this->constraintManager = $constraintManager;
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function getValidationConstraintManager() {
  214. return $this->constraintManager;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getDefaultConstraints(DataDefinitionInterface $definition) {
  220. $constraints = array();
  221. $type_definition = $this->getDefinition($definition->getDataType());
  222. // Auto-generate a constraint for data types implementing a primitive
  223. // interface.
  224. if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
  225. $constraints['PrimitiveType'] = array();
  226. }
  227. // Add in constraints specified by the data type.
  228. if (isset($type_definition['constraints'])) {
  229. $constraints += $type_definition['constraints'];
  230. }
  231. // Add the NotNull constraint for required data.
  232. if ($definition->isRequired()) {
  233. $constraints['NotNull'] = array();
  234. }
  235. // Check if the class provides allowed values.
  236. if (is_subclass_of($definition->getClass(), 'Drupal\Core\TypedData\OptionsProviderInterface')) {
  237. $constraints['AllowedValues'] = array();
  238. }
  239. return $constraints;
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. public function clearCachedDefinitions() {
  245. parent::clearCachedDefinitions();
  246. $this->prototypes = array();
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function getCanonicalRepresentation(TypedDataInterface $data) {
  252. $data_definition = $data->getDataDefinition();
  253. // In case a list is passed, respect the 'wrapped' key of its data type.
  254. if ($data_definition instanceof ListDataDefinitionInterface) {
  255. $data_definition = $data_definition->getItemDefinition();
  256. }
  257. // Get the plugin definition of the used data type.
  258. $type_definition = $this->getDefinition($data_definition->getDataType());
  259. if (!empty($type_definition['unwrap_for_canonical_representation'])) {
  260. return $data->getValue();
  261. }
  262. return $data;
  263. }
  264. }