|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Biurad opensource projects. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Flange\Database\Doctrine\Form; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\DBAL\Types\Types; |
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
|
|
|
|
|
18
|
|
|
use Doctrine\ORM\Mapping\JoinColumnMapping; |
|
19
|
|
|
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException; |
|
20
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
21
|
|
|
use Doctrine\Persistence\Mapping\MappingException; |
|
22
|
|
|
use Doctrine\Persistence\Proxy; |
|
23
|
|
|
use Flange\Database\Doctrine\Form\Type\EntityType; |
|
24
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
|
25
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
|
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType; |
|
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; |
|
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateType; |
|
29
|
|
|
use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
|
30
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
|
31
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
|
32
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
33
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TimeType; |
|
34
|
|
|
use Symfony\Component\Form\FormTypeGuesserInterface; |
|
35
|
|
|
use Symfony\Component\Form\Guess\Guess; |
|
|
|
|
|
|
36
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
|
37
|
|
|
use Symfony\Component\Form\Guess\ValueGuess; |
|
38
|
|
|
|
|
39
|
|
|
class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface |
|
40
|
|
|
{ |
|
41
|
|
|
protected ManagerRegistry $registry; |
|
42
|
|
|
|
|
43
|
|
|
private array $cache = []; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(ManagerRegistry $registry) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->registry = $registry; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function guessType(string $class, string $property): ?TypeGuess |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$ret = $this->getMetadata($class)) { |
|
53
|
|
|
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
[$metadata, $name] = $ret; |
|
57
|
|
|
|
|
58
|
|
|
if ($metadata->hasAssociation($property)) { |
|
59
|
|
|
$multiple = $metadata->isCollectionValuedAssociation($property); |
|
60
|
|
|
$mapping = $metadata->getAssociationMapping($property); |
|
61
|
|
|
|
|
62
|
|
|
return new TypeGuess(EntityType::class, ['em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple], Guess::HIGH_CONFIDENCE); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return match ($metadata->getTypeOfField($property)) { |
|
66
|
|
|
'array', // DBAL < 4 |
|
67
|
|
|
Types::SIMPLE_ARRAY => new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE), |
|
68
|
|
|
Types::BOOLEAN => new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE), |
|
69
|
|
|
Types::DATETIME_MUTABLE, |
|
70
|
|
|
Types::DATETIMETZ_MUTABLE, |
|
71
|
|
|
'vardatetime' => new TypeGuess(DateTimeType::class, [], Guess::HIGH_CONFIDENCE), |
|
72
|
|
|
Types::DATETIME_IMMUTABLE, |
|
73
|
|
|
Types::DATETIMETZ_IMMUTABLE => new TypeGuess(DateTimeType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE), |
|
74
|
|
|
Types::DATEINTERVAL => new TypeGuess(DateIntervalType::class, [], Guess::HIGH_CONFIDENCE), |
|
75
|
|
|
Types::DATE_MUTABLE => new TypeGuess(DateType::class, [], Guess::HIGH_CONFIDENCE), |
|
76
|
|
|
Types::DATE_IMMUTABLE => new TypeGuess(DateType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE), |
|
77
|
|
|
Types::TIME_MUTABLE => new TypeGuess(TimeType::class, [], Guess::HIGH_CONFIDENCE), |
|
78
|
|
|
Types::TIME_IMMUTABLE => new TypeGuess(TimeType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE), |
|
79
|
|
|
Types::DECIMAL => new TypeGuess(NumberType::class, ['input' => 'string'], Guess::MEDIUM_CONFIDENCE), |
|
80
|
|
|
Types::FLOAT => new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE), |
|
81
|
|
|
Types::INTEGER, |
|
82
|
|
|
Types::BIGINT, |
|
83
|
|
|
Types::SMALLINT => new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE), |
|
84
|
|
|
Types::STRING => new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE), |
|
85
|
|
|
Types::TEXT => new TypeGuess(TextareaType::class, [], Guess::MEDIUM_CONFIDENCE), |
|
86
|
|
|
default => new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE), |
|
87
|
|
|
}; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function guessRequired(string $class, string $property): ?ValueGuess |
|
91
|
|
|
{ |
|
92
|
|
|
$classMetadatas = $this->getMetadata($class); |
|
93
|
|
|
|
|
94
|
|
|
if (!$classMetadatas) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** @var ClassMetadataInfo $classMetadata */ |
|
99
|
|
|
$classMetadata = $classMetadatas[0]; |
|
100
|
|
|
|
|
101
|
|
|
// Check whether the field exists and is nullable or not |
|
102
|
|
|
if (isset($classMetadata->fieldMappings[$property])) { |
|
103
|
|
|
if (!$classMetadata->isNullable($property) && Types::BOOLEAN !== $classMetadata->getTypeOfField($property)) { |
|
104
|
|
|
return new ValueGuess(true, Guess::HIGH_CONFIDENCE); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return new ValueGuess(false, Guess::MEDIUM_CONFIDENCE); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Check whether the association exists, is a to-one association and its |
|
111
|
|
|
// join column is nullable or not |
|
112
|
|
|
if ($classMetadata->isAssociationWithSingleJoinColumn($property)) { |
|
113
|
|
|
$mapping = $classMetadata->getAssociationMapping($property); |
|
114
|
|
|
|
|
115
|
|
|
if (null === self::getMappingValue($mapping['joinColumns'][0], 'nullable')) { |
|
116
|
|
|
// The "nullable" option defaults to true, in that case the |
|
117
|
|
|
// field should not be required. |
|
118
|
|
|
return new ValueGuess(false, Guess::HIGH_CONFIDENCE); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return new ValueGuess(!self::getMappingValue($mapping['joinColumns'][0], 'nullable'), Guess::HIGH_CONFIDENCE); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return null; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function guessMaxLength(string $class, string $property): ?ValueGuess |
|
128
|
|
|
{ |
|
129
|
|
|
$ret = $this->getMetadata($class); |
|
130
|
|
|
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) { |
|
131
|
|
|
$mapping = $ret[0]->getFieldMapping($property); |
|
132
|
|
|
|
|
133
|
|
|
if (isset($mapping['length'])) { |
|
134
|
|
|
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT])) { |
|
138
|
|
|
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function guessPattern(string $class, string $property): ?ValueGuess |
|
146
|
|
|
{ |
|
147
|
|
|
$ret = $this->getMetadata($class); |
|
148
|
|
|
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) { |
|
149
|
|
|
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT])) { |
|
150
|
|
|
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @template T of object |
|
159
|
|
|
* |
|
160
|
|
|
* @param class-string<T> $class |
|
|
|
|
|
|
161
|
|
|
* |
|
162
|
|
|
* @return array{0:ClassMetadata<T>, 1:string}|null |
|
|
|
|
|
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getMetadata(string $class): ?array |
|
165
|
|
|
{ |
|
166
|
|
|
// normalize class name |
|
167
|
|
|
$class = self::getRealClass(ltrim($class, '\\')); |
|
168
|
|
|
|
|
169
|
|
|
if (\array_key_exists($class, $this->cache)) { |
|
170
|
|
|
return $this->cache[$class]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$this->cache[$class] = null; |
|
174
|
|
|
foreach ($this->registry->getManagers() as $name => $em) { |
|
175
|
|
|
try { |
|
176
|
|
|
return $this->cache[$class] = [$em->getClassMetadata($class), $name]; |
|
177
|
|
|
} catch (MappingException) { |
|
178
|
|
|
// not an entity or mapped super class |
|
179
|
|
|
} catch (LegacyMappingException) { |
|
180
|
|
|
// not an entity or mapped super class, using Doctrine ORM 2.2 |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private static function getRealClass(string $class): string |
|
188
|
|
|
{ |
|
189
|
|
|
if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) { |
|
190
|
|
|
return $class; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return substr($class, $pos + Proxy::MARKER_LENGTH + 2); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
private static function getMappingValue(array|JoinColumnMapping $mapping, string $key): mixed |
|
197
|
|
|
{ |
|
198
|
|
|
if ($mapping instanceof JoinColumnMapping) { |
|
|
|
|
|
|
199
|
|
|
return $mapping->$key; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $mapping[$key] ?? null; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths