Преглед изворни кода

shema snippets makers : various fixes

Olivier Massot пре 11 месеци
родитељ
комит
55c167d814

+ 3 - 1
src/Commands/Doctrine/SchemaValidateCommand.php

@@ -15,7 +15,9 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
 /**
- * Overrides the default doctrine:schema:update command.
+ * Valide le schéma Doctrine en le comparant à la V1
+ *
+ * @see https://ressources-opentalent.atlassian.net/wiki/spaces/DEV/pages/240551231/V+rifier+le+sch+ma+Doctrine+de+la+V2+et+g+n+rer+les+entit+s+et+propri+t+s+manquantes
  */
 #[AsCommand(
     name: 'ot:schema:validate',

+ 36 - 1
src/Service/Doctrine/SchemaValidation/SchemaSnippetsMaker.php

@@ -61,6 +61,7 @@ class SchemaSnippetsMaker
             $class = $this->makeSnippetEntityClass($entity);
 
             $methods = [];
+            $collections = [];
 
             $expectedFields = [];
             if (!is_array($differences)) {
@@ -82,9 +83,19 @@ class SchemaSnippetsMaker
 
                 $class->addMember($prop);
 
+                if ($prop->getType() === Collection::class) {
+                    $collections[] = $prop;
+                }
+
                 $methods = [...$methods, ...$this->makeMethodsSnippetForProp($prop)];
             }
 
+            if ($collections) {
+                $class->addMember(
+                    $this->makeSnippetConstructor($collections)
+                );
+            }
+
             foreach ($methods as $method) {
                 $class->addMember($method);
             }
@@ -266,7 +277,15 @@ class SchemaSnippetsMaker
      */
     protected function getNamespaceValue(string $entity): string
     {
-        return $this->entityUtils->getNamespaceFromName($entity) ?? ('App\\Entity\\' . $entity);
+        try {
+            $fullQualifiedName = str_contains($entity, "\\") ?
+                $entity :
+                $this->entityUtils->getFullNameFromEntityName($entity);
+
+            return $this->entityUtils->getNamespaceFromName($fullQualifiedName);
+        } catch (\LogicException) {
+            return 'App\\Entity';
+        }
     }
 
     /**
@@ -439,6 +458,22 @@ class SchemaSnippetsMaker
         return $prop;
     }
 
+    /**
+     * Make the '__construct' method with collections initialization
+     * @param array $collections
+     * @return Method
+     */
+    protected function makeSnippetConstructor(array $collections): Method {
+        $constructor = new Method('__construct');
+        $constructor->setPublic();
+
+        foreach ($collections as $collection) {
+            $constructor->addBody('$this->' . $collection->getName() . ' = new ArrayCollection();');
+        }
+
+        return $constructor;
+    }
+
     /**
      * Make a 'getter' method for the given property
      *