TypedDataManager.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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();
  152. // Only property instances of complex data types should be cached by the
  153. // property name, as they represent different properties. Properties of list
  154. // data types are the items of the list and the property name represents
  155. // only the delta in that list and not an unique property, which is why all
  156. // items should use the same prototype.
  157. if ($object instanceof ComplexDataInterface) {
  158. $parts[] = $property_name;
  159. }
  160. $key = implode(':', $parts);
  161. // Create the prototype if needed.
  162. if (!isset($this->prototypes[$key])) {
  163. // Fetch the data definition for the child object from the parent.
  164. if ($object instanceof ComplexDataInterface) {
  165. $definition = $object->getDataDefinition()->getPropertyDefinition($property_name);
  166. }
  167. elseif ($object instanceof ListInterface) {
  168. $definition = $object->getItemDefinition();
  169. }
  170. else {
  171. throw new \InvalidArgumentException("The passed object has to either implement the ComplexDataInterface or the ListInterface.");
  172. }
  173. if (!$definition) {
  174. throw new \InvalidArgumentException("Property $property_name is unknown.");
  175. }
  176. // Create the prototype without any value, but with initial parenting
  177. // so that constructors can set up the objects correctly.
  178. $this->prototypes[$key] = $this->create($definition, NULL, $property_name, $object);
  179. }
  180. // Clone the prototype, update its parenting information, and assign the
  181. // value.
  182. $property = clone $this->prototypes[$key];
  183. $property->setContext($property_name, $object);
  184. if (isset($value)) {
  185. $property->setValue($value, FALSE);
  186. }
  187. return $property;
  188. }
  189. /**
  190. * Sets the validator for validating typed data.
  191. *
  192. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  193. * The validator object to set.
  194. */
  195. public function setValidator(ValidatorInterface $validator) {
  196. $this->validator = $validator;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function getValidator() {
  202. if (!isset($this->validator)) {
  203. $this->validator = new RecursiveValidator(
  204. new ExecutionContextFactory(new DrupalTranslator()),
  205. new ConstraintValidatorFactory($this->classResolver),
  206. $this
  207. );
  208. }
  209. return $this->validator;
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function setValidationConstraintManager(ConstraintManager $constraintManager) {
  215. $this->constraintManager = $constraintManager;
  216. }
  217. /**
  218. * {@inheritdoc}
  219. */
  220. public function getValidationConstraintManager() {
  221. return $this->constraintManager;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getDefaultConstraints(DataDefinitionInterface $definition) {
  227. $constraints = [];
  228. $type_definition = $this->getDefinition($definition->getDataType());
  229. // Auto-generate a constraint for data types implementing a primitive
  230. // interface.
  231. if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
  232. $constraints['PrimitiveType'] = [];
  233. }
  234. // Add in constraints specified by the data type.
  235. if (isset($type_definition['constraints'])) {
  236. $constraints += $type_definition['constraints'];
  237. }
  238. // Add the NotNull constraint for required data.
  239. if ($definition->isRequired()) {
  240. $constraints['NotNull'] = [];
  241. }
  242. // Check if the class provides allowed values.
  243. if (is_subclass_of($definition->getClass(), 'Drupal\Core\TypedData\OptionsProviderInterface')) {
  244. $constraints['AllowedValues'] = [];
  245. }
  246. return $constraints;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function clearCachedDefinitions() {
  252. parent::clearCachedDefinitions();
  253. $this->prototypes = [];
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function getCanonicalRepresentation(TypedDataInterface $data) {
  259. $data_definition = $data->getDataDefinition();
  260. // In case a list is passed, respect the 'wrapped' key of its data type.
  261. if ($data_definition instanceof ListDataDefinitionInterface) {
  262. $data_definition = $data_definition->getItemDefinition();
  263. }
  264. // Get the plugin definition of the used data type.
  265. $type_definition = $this->getDefinition($data_definition->getDataType());
  266. if (!empty($type_definition['unwrap_for_canonical_representation'])) {
  267. return $data->getValue();
  268. }
  269. return $data;
  270. }
  271. }