13
13
14
14
use ApiPlatform \Core \Annotation \ApiResource ;
15
15
use Doctrine \DBAL \Types \Type ;
16
+ use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
17
+ use Doctrine \Persistence \Mapping \Driver \AnnotationDriver ;
18
+ use Doctrine \Persistence \Mapping \Driver \MappingDriver ;
16
19
use Symfony \Bundle \MakerBundle \ConsoleStyle ;
17
20
use Symfony \Bundle \MakerBundle \DependencyBuilder ;
18
21
use Symfony \Bundle \MakerBundle \Doctrine \DoctrineHelper ;
@@ -160,6 +163,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
160
163
'Entity \\'
161
164
);
162
165
166
+ $ mappingDriver = $ this ->doctrineHelper ->getMappingDriverForNamespace ($ entityClassDetails ->getFullName ());
163
167
$ classExists = class_exists ($ entityClassDetails ->getFullName ());
164
168
if (!$ classExists ) {
165
169
$ broadcast = $ input ->getOption ('broadcast ' );
@@ -168,7 +172,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
168
172
$ input ->getOption ('api-resource ' ),
169
173
false ,
170
174
true ,
171
- $ broadcast
175
+ $ broadcast ,
176
+ $ mappingDriver instanceof AttributeDriver
172
177
);
173
178
174
179
if ($ broadcast ) {
@@ -186,10 +191,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
186
191
$ generator ->writeChanges ();
187
192
}
188
193
189
- if (!$ this ->doesEntityUseAnnotationMapping ($ entityClassDetails ->getFullName ())) {
190
- throw new RuntimeCommandException (sprintf ('Only annotation mapping is supported by make:entity, but the <info>%s</info> class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping configuration, and then re-run this command with the <info>--regenerate</info> flag. ' , $ entityClassDetails ->getFullName ()));
191
- }
192
-
193
194
if ($ classExists ) {
194
195
$ entityPath = $ this ->getPathOfClass ($ entityClassDetails ->getFullName ());
195
196
$ io ->text ([
@@ -204,7 +205,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
204
205
}
205
206
206
207
$ currentFields = $ this ->getPropertyNames ($ entityClassDetails ->getFullName ());
207
- $ manipulator = $ this ->createClassManipulator ($ entityPath , $ io , $ overwrite );
208
+ $ manipulator = $ this ->createClassManipulator ($ entityPath , $ io , $ overwrite, $ mappingDriver );
208
209
209
210
$ isFirstField = true ;
210
211
while (true ) {
@@ -232,7 +233,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
232
233
$ otherManipulator = $ manipulator ;
233
234
} else {
234
235
$ otherManipulatorFilename = $ this ->getPathOfClass ($ newField ->getInverseClass ());
235
- $ otherManipulator = $ this ->createClassManipulator ($ otherManipulatorFilename , $ io , $ overwrite );
236
+ $ otherManipulator = $ this ->createClassManipulator ($ otherManipulatorFilename , $ io , $ overwrite, $ mappingDriver );
236
237
}
237
238
switch ($ newField ->getType ()) {
238
239
case EntityRelation::MANY_TO_ONE :
@@ -247,7 +248,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
247
248
// the new field being added to THIS entity is the inverse
248
249
$ newFieldName = $ newField ->getInverseProperty ();
249
250
$ otherManipulatorFilename = $ this ->getPathOfClass ($ newField ->getOwningClass ());
250
- $ otherManipulator = $ this ->createClassManipulator ($ otherManipulatorFilename , $ io , $ overwrite );
251
+ $ otherManipulator = $ this ->createClassManipulator ($ otherManipulatorFilename , $ io , $ overwrite, $ mappingDriver );
251
252
252
253
// The *other* class will receive the ManyToOne
253
254
$ otherManipulator ->addManyToOneRelation ($ newField ->getOwningRelation ());
@@ -791,9 +792,13 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $
791
792
return $ io ->askQuestion ($ question );
792
793
}
793
794
794
- private function createClassManipulator (string $ path , ConsoleStyle $ io , bool $ overwrite ): ClassSourceManipulator
795
+ private function createClassManipulator (string $ path , ConsoleStyle $ io , bool $ overwrite, ? MappingDriver $ mappingDriver = null ): ClassSourceManipulator
795
796
{
796
- $ manipulator = new ClassSourceManipulator ($ this ->fileManager ->getFileContents ($ path ), $ overwrite );
797
+ $ useAnnotations = null === $ mappingDriver || $ mappingDriver instanceof AnnotationDriver && !$ mappingDriver instanceof AttributeDriver;
798
+ $ useAttributes = $ mappingDriver instanceof AttributeDriver;
799
+
800
+ $ manipulator = new ClassSourceManipulator ($ this ->fileManager ->getFileContents ($ path ), $ overwrite , $ useAnnotations , true , $ useAttributes );
801
+
797
802
$ manipulator ->setIo ($ io );
798
803
799
804
return $ manipulator ;
@@ -832,6 +837,7 @@ private function getPropertyNames(string $class): array
832
837
}, $ reflClass ->getProperties ());
833
838
}
834
839
840
+ // not needed anymore
835
841
private function doesEntityUseAnnotationMapping (string $ className ): bool
836
842
{
837
843
if (!class_exists ($ className )) {
0 commit comments