TypedDataManager.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. use Drupal\Component\Plugin\Exception\PluginException;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\DependencyInjection\ClassResolverInterface;
  6. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Drupal\Core\Plugin\DefaultPluginManager;
  9. use Drupal\Core\TypedData\Validation\ExecutionContextFactory;
  10. use Drupal\Core\TypedData\Validation\RecursiveValidator;
  11. use Drupal\Core\Validation\ConstraintManager;
  12. use Drupal\Core\Validation\ConstraintValidatorFactory;
  13. use Drupal\Core\Validation\DrupalTranslator;
  14. use Symfony\Component\Validator\Validator\ValidatorInterface;
  15. /**
  16. * Manages data type plugins.
  17. */
  18. class TypedDataManager extends DefaultPluginManager implements TypedDataManagerInterface {
  19. use DependencySerializationTrait;
  20. /**
  21. * The validator used for validating typed data.
  22. *
  23. * @var \Symfony\Component\Validator\Validator\ValidatorInterface
  24. */
  25. protected $validator;
  26. /**
  27. * The validation constraint manager to use for instantiating constraints.
  28. *
  29. * @var \Drupal\Core\Validation\ConstraintManager
  30. */
  31. protected $constraintManager;
  32. /**
  33. * An array of typed data property prototypes.
  34. *
  35. * @var array
  36. */
  37. protected $prototypes = [];
  38. /**
  39. * The class resolver.
  40. *
  41. * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
  42. */
  43. protected $classResolver;
  44. /**
  45. * Constructs a new TypedDataManager.
  46. *
  47. * @param \Traversable $namespaces
  48. * An object that implements \Traversable which contains the root paths
  49. * keyed by the corresponding namespace to look for plugin implementations.
  50. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  51. * Cache backend instance to use.
  52. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  53. * The module handler.
  54. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
  55. * The class resolver.
  56. */
  57. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) {
  58. $this->alterInfo('data_type_info');
  59. $this->setCacheBackend($cache_backend, 'typed_data_types_plugins');
  60. $this->classResolver = $class_resolver;
  61. parent::__construct('Plugin/DataType', $namespaces, $module_handler, NULL, 'Drupal\Core\TypedData\Annotation\DataType');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function createInstance($data_type, array $configuration = []) {
  67. $data_definition = $configuration['data_definition'];
  68. $type_definition = $this->getDefinition($data_type);
  69. if (!isset($type_definition)) {
  70. throw new \InvalidArgumentException("Invalid data type '$data_type' has been given");
  71. }
  72. // Allow per-data definition overrides of the used classes, i.e. take over
  73. // classes specified in the type definition.
  74. $class = $data_definition->getClass();
  75. if (!isset($class)) {
  76. throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
  77. }
  78. $typed_data = $class::createInstance($data_definition, $configuration['name'], $configuration['parent']);
  79. $typed_data->setTypedDataManager($this);
  80. return $typed_data;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function create(DataDefinitionInterface $definition, $value = NULL, $name = NULL, $parent = NULL) {
  86. $typed_data = $this->createInstance($definition->getDataType(), [
  87. 'data_definition' => $definition,
  88. 'name' => $name,
  89. 'parent' => $parent,
  90. ]);
  91. if (isset($value)) {
  92. $typed_data->setValue($value, FALSE);
  93. }
  94. return $typed_data;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function createDataDefinition($data_type) {
  100. $type_definition = $this->getDefinition($data_type);
  101. if (!isset($type_definition)) {
  102. throw new \InvalidArgumentException("Invalid data type '$data_type' has been given");
  103. }
  104. $class = $type_definition['definition_class'];
  105. $data_definition = $class::createFromDataType($data_type);
  106. if (method_exists($data_definition, 'setTypedDataManager')) {
  107. $data_definition->setTypedDataManager($this);
  108. }
  109. return $data_definition;
  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. // Include the settings serialized as JSON as part of the key. The JSON is
  147. // a shorter string than the serialized form, so array access is faster.
  148. $parts[] = json_encode($settings);
  149. }
  150. // Property path for the requested data object.
  151. $parts[] = $object->getPropertyPath() . '.' . $property_name;
  152. $key = implode(':', $parts);
  153. // Create the prototype if needed.
  154. if (!isset($this->prototypes[$key])) {
  155. // Fetch the data definition for the child object from the parent.
  156. if ($object instanceof ComplexDataInterface) {
  157. $definition = $object->getDataDefinition()->getPropertyDefinition($property_name);
  158. }
  159. elseif ($object instanceof ListInterface) {
  160. $definition = $object->getItemDefinition();
  161. }
  162. else {
  163. throw new \InvalidArgumentException("The passed object has to either implement the ComplexDataInterface or the ListInterface.");
  164. }
  165. if (!$definition) {
  166. throw new \InvalidArgumentException("Property $property_name is unknown.");
  167. }
  168. // Create the prototype without any value, but with initial parenting
  169. // so that constructors can set up the objects correctly.
  170. $this->prototypes[$key] = $this->create($definition, NULL, $property_name, $object);
  171. }
  172. // Clone the prototype, update its parenting information, and assign the
  173. // value.
  174. $property = clone $this->prototypes[$key];
  175. $property->setContext($property_name, $object);
  176. if (isset($value)) {
  177. $property->setValue($value, FALSE);
  178. }
  179. return $property;
  180. }
  181. /**
  182. * Sets the validator for validating typed data.
  183. *
  184. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  185. * The validator object to set.
  186. */
  187. public function setValidator(ValidatorInterface $validator) {
  188. $this->validator = $validator;
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function getValidator() {
  194. if (!isset($this->validator)) {
  195. $this->validator = new RecursiveValidator(
  196. new ExecutionContextFactory(new DrupalTranslator()),
  197. new ConstraintValidatorFactory($this->classResolver),
  198. $this
  199. );
  200. }
  201. return $this->validator;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function setValidationConstraintManager(ConstraintManager $constraintManager) {
  207. $this->constraintManager = $constraintManager;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getValidationConstraintManager() {
  213. return $this->constraintManager;
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function getDefaultConstraints(DataDefinitionInterface $definition) {
  219. $constraints = [];
  220. $type_definition = $this->getDefinition($definition->getDataType());
  221. // Auto-generate a constraint for data types implementing a primitive
  222. // interface.
  223. if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
  224. $constraints['PrimitiveType'] = [];
  225. }
  226. // Add in constraints specified by the data type.
  227. if (isset($type_definition['constraints'])) {
  228. $constraints += $type_definition['constraints'];
  229. }
  230. // Add the NotNull constraint for required data.
  231. if ($definition->isRequired()) {
  232. $constraints['NotNull'] = [];
  233. }
  234. // Check if the class provides allowed values.
  235. if (is_subclass_of($definition->getClass(), 'Drupal\Core\TypedData\OptionsProviderInterface')) {
  236. $constraints['AllowedValues'] = [];
  237. }
  238. return $constraints;
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function clearCachedDefinitions() {
  244. parent::clearCachedDefinitions();
  245. $this->prototypes = [];
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function getCanonicalRepresentation(TypedDataInterface $data) {
  251. $data_definition = $data->getDataDefinition();
  252. // In case a list is passed, respect the 'wrapped' key of its data type.
  253. if ($data_definition instanceof ListDataDefinitionInterface) {
  254. $data_definition = $data_definition->getItemDefinition();
  255. }
  256. // Get the plugin definition of the used data type.
  257. $type_definition = $this->getDefinition($data_definition->getDataType());
  258. if (!empty($type_definition['unwrap_for_canonical_representation'])) {
  259. return $data->getValue();
  260. }
  261. return $data;
  262. }
  263. }