|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Doctrine; |
| 13 | + |
| 14 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 15 | +use Doctrine\ORM\Mapping\ClassMetadataInfo; |
| 16 | +use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Sadicov Vladimir <[email protected]> |
| 20 | + * |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class DoctrineEntityHelper |
| 24 | +{ |
| 25 | + private $metadataFactory; |
| 26 | + |
| 27 | + public function __construct(ManagerRegistry $registry = null) |
| 28 | + { |
| 29 | + $this->metadataFactory = null !== $registry ? new DoctrineMetadataFactory($registry) : null; |
| 30 | + } |
| 31 | + |
| 32 | + private function isDoctrineInstalled(): bool |
| 33 | + { |
| 34 | + return null !== $this->metadataFactory; |
| 35 | + } |
| 36 | + |
| 37 | + public function getEntitiesForAutocomplete(): array |
| 38 | + { |
| 39 | + $entities = []; |
| 40 | + |
| 41 | + if ($this->isDoctrineInstalled()) { |
| 42 | + $allMetadata = $this->metadataFactory->getAllMetadata(); |
| 43 | + /** @var ClassMetadataInfo $metadata */ |
| 44 | + foreach ($allMetadata as $metadata) { |
| 45 | + $entityClassDetails = new ClassNameDetails($metadata->name, 'App\\Entity'); |
| 46 | + $entities[] = $entityClassDetails->getRelativeName(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return $entities; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $entityClassName |
| 55 | + * |
| 56 | + * @return null|DoctrineEntityDetails |
| 57 | + * |
| 58 | + * @throws \Exception |
| 59 | + */ |
| 60 | + public function createDoctrineDetails(string $entityClassName) |
| 61 | + { |
| 62 | + $metadata = $this->getEntityMetadata($entityClassName); |
| 63 | + |
| 64 | + if (null !== $metadata) { |
| 65 | + return new DoctrineEntityDetails( |
| 66 | + $metadata->customRepositoryClassName, |
| 67 | + $metadata->identifier[0], |
| 68 | + $metadata->fieldMappings, |
| 69 | + $this->getFormFieldsFromEntity($metadata) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public function getFormFieldsFromEntity(ClassMetadataInfo $metadata): array |
| 77 | + { |
| 78 | + $fields = (array) $metadata->fieldNames; |
| 79 | + // Remove the primary key field if it's not managed manually |
| 80 | + if (!$metadata->isIdentifierNatural()) { |
| 81 | + $fields = array_diff($fields, $metadata->identifier); |
| 82 | + } |
| 83 | + foreach ($metadata->associationMappings as $fieldName => $relation) { |
| 84 | + if (ClassMetadataInfo::ONE_TO_MANY !== $relation['type']) { |
| 85 | + $fields[] = $fieldName; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $fields; |
| 90 | + } |
| 91 | + |
| 92 | + public function getEntityMetadata($entityClassName) |
| 93 | + { |
| 94 | + if (null === $this->metadataFactory) { |
| 95 | + throw new \Exception('Somehow the doctrine service is missing. Is DoctrineBundle installed?'); |
| 96 | + } |
| 97 | + |
| 98 | + return $this->metadataFactory->getMetadataForClass($entityClassName); |
| 99 | + } |
| 100 | +} |
0 commit comments