DoctrineExtractor.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\PropertyInfo;
  11. use Doctrine\DBAL\Types\Types;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Mapping\ClassMetadata;
  14. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  15. use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
  16. use Doctrine\Persistence\Mapping\MappingException;
  17. use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
  18. use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
  19. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  20. use Symfony\Component\PropertyInfo\Type;
  21. /**
  22. * Extracts data using Doctrine ORM and ODM metadata.
  23. *
  24. * @author Kévin Dunglas <dunglas@gmail.com>
  25. */
  26. class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface
  27. {
  28. private $entityManager;
  29. private $classMetadataFactory;
  30. public function __construct(EntityManagerInterface $entityManager)
  31. {
  32. $this->entityManager = $entityManager;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getProperties(string $class, array $context = [])
  38. {
  39. if (null === $metadata = $this->getMetadata($class)) {
  40. return null;
  41. }
  42. $properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
  43. if ($metadata instanceof ClassMetadataInfo && class_exists(\Doctrine\ORM\Mapping\Embedded::class) && $metadata->embeddedClasses) {
  44. $properties = array_filter($properties, function ($property) {
  45. return false === strpos($property, '.');
  46. });
  47. $properties = array_merge($properties, array_keys($metadata->embeddedClasses));
  48. }
  49. return $properties;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getTypes(string $class, string $property, array $context = [])
  55. {
  56. if (null === $metadata = $this->getMetadata($class)) {
  57. return null;
  58. }
  59. if ($metadata->hasAssociation($property)) {
  60. $class = $metadata->getAssociationTargetClass($property);
  61. if ($metadata->isSingleValuedAssociation($property)) {
  62. if ($metadata instanceof ClassMetadataInfo) {
  63. $associationMapping = $metadata->getAssociationMapping($property);
  64. $nullable = $this->isAssociationNullable($associationMapping);
  65. } else {
  66. $nullable = false;
  67. }
  68. return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
  69. }
  70. $collectionKeyType = Type::BUILTIN_TYPE_INT;
  71. if ($metadata instanceof ClassMetadataInfo) {
  72. $associationMapping = $metadata->getAssociationMapping($property);
  73. if (isset($associationMapping['indexBy'])) {
  74. /** @var ClassMetadataInfo $subMetadata */
  75. $subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
  76. // Check if indexBy value is a property
  77. $fieldName = $associationMapping['indexBy'];
  78. if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
  79. $fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
  80. //Not a property, maybe a column name?
  81. if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
  82. //Maybe the column name is the association join column?
  83. $associationMapping = $subMetadata->getAssociationMapping($fieldName);
  84. /** @var ClassMetadataInfo $subMetadata */
  85. $indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
  86. $subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
  87. //Not a property, maybe a column name?
  88. if (null === ($typeOfField = $subMetadata->getTypeOfField($indexProperty))) {
  89. $fieldName = $subMetadata->getFieldForColumn($indexProperty);
  90. $typeOfField = $subMetadata->getTypeOfField($fieldName);
  91. }
  92. }
  93. }
  94. if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
  95. return null;
  96. }
  97. }
  98. }
  99. return [new Type(
  100. Type::BUILTIN_TYPE_OBJECT,
  101. false,
  102. 'Doctrine\Common\Collections\Collection',
  103. true,
  104. new Type($collectionKeyType),
  105. new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
  106. )];
  107. }
  108. if ($metadata instanceof ClassMetadataInfo && class_exists(\Doctrine\ORM\Mapping\Embedded::class) && isset($metadata->embeddedClasses[$property])) {
  109. return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class'])];
  110. }
  111. if ($metadata->hasField($property)) {
  112. $typeOfField = $metadata->getTypeOfField($property);
  113. if (!$builtinType = $this->getPhpType($typeOfField)) {
  114. return null;
  115. }
  116. $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
  117. switch ($builtinType) {
  118. case Type::BUILTIN_TYPE_OBJECT:
  119. switch ($typeOfField) {
  120. case Types::DATE_MUTABLE:
  121. case Types::DATETIME_MUTABLE:
  122. case Types::DATETIMETZ_MUTABLE:
  123. case 'vardatetime':
  124. case Types::TIME_MUTABLE:
  125. return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
  126. case Types::DATE_IMMUTABLE:
  127. case Types::DATETIME_IMMUTABLE:
  128. case Types::DATETIMETZ_IMMUTABLE:
  129. case Types::TIME_IMMUTABLE:
  130. return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
  131. case Types::DATEINTERVAL:
  132. return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateInterval')];
  133. }
  134. break;
  135. case Type::BUILTIN_TYPE_ARRAY:
  136. switch ($typeOfField) {
  137. case Types::ARRAY:
  138. case Types::JSON_ARRAY:
  139. return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
  140. case Types::SIMPLE_ARRAY:
  141. return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
  142. }
  143. }
  144. return [new Type($builtinType, $nullable)];
  145. }
  146. return null;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function isReadable(string $class, string $property, array $context = [])
  152. {
  153. return null;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function isWritable(string $class, string $property, array $context = [])
  159. {
  160. if (
  161. null === ($metadata = $this->getMetadata($class))
  162. || ClassMetadata::GENERATOR_TYPE_NONE === $metadata->generatorType
  163. || !\in_array($property, $metadata->getIdentifierFieldNames(), true)
  164. ) {
  165. return null;
  166. }
  167. return false;
  168. }
  169. private function getMetadata(string $class): ?ClassMetadata
  170. {
  171. try {
  172. return $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
  173. } catch (MappingException | OrmMappingException $exception) {
  174. return null;
  175. }
  176. }
  177. /**
  178. * Determines whether an association is nullable.
  179. *
  180. * @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
  181. */
  182. private function isAssociationNullable(array $associationMapping): bool
  183. {
  184. if (isset($associationMapping['id']) && $associationMapping['id']) {
  185. return false;
  186. }
  187. if (!isset($associationMapping['joinColumns'])) {
  188. return true;
  189. }
  190. $joinColumns = $associationMapping['joinColumns'];
  191. foreach ($joinColumns as $joinColumn) {
  192. if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. }
  198. /**
  199. * Gets the corresponding built-in PHP type.
  200. */
  201. private function getPhpType(string $doctrineType): ?string
  202. {
  203. switch ($doctrineType) {
  204. case Types::SMALLINT:
  205. case Types::INTEGER:
  206. return Type::BUILTIN_TYPE_INT;
  207. case Types::FLOAT:
  208. return Type::BUILTIN_TYPE_FLOAT;
  209. case Types::BIGINT:
  210. case Types::STRING:
  211. case Types::TEXT:
  212. case Types::GUID:
  213. case Types::DECIMAL:
  214. return Type::BUILTIN_TYPE_STRING;
  215. case Types::BOOLEAN:
  216. return Type::BUILTIN_TYPE_BOOL;
  217. case Types::BLOB:
  218. case Types::BINARY:
  219. return Type::BUILTIN_TYPE_RESOURCE;
  220. case Types::OBJECT:
  221. case Types::DATE_MUTABLE:
  222. case Types::DATETIME_MUTABLE:
  223. case Types::DATETIMETZ_MUTABLE:
  224. case 'vardatetime':
  225. case Types::TIME_MUTABLE:
  226. case Types::DATE_IMMUTABLE:
  227. case Types::DATETIME_IMMUTABLE:
  228. case Types::DATETIMETZ_IMMUTABLE:
  229. case Types::TIME_IMMUTABLE:
  230. case Types::DATEINTERVAL:
  231. return Type::BUILTIN_TYPE_OBJECT;
  232. case Types::ARRAY:
  233. case Types::SIMPLE_ARRAY:
  234. case Types::JSON_ARRAY:
  235. return Type::BUILTIN_TYPE_ARRAY;
  236. }
  237. return null;
  238. }
  239. }