From f8a198019b7fb552e37cd343a1b3bd33d182b10b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 27 Sep 2018 10:14:36 +0200
Subject: [PATCH] =?UTF-8?q?Pr=C3=A9g=C3=A9n=C3=A8re=20les=20repr=C3=A9sent?=
 =?UTF-8?q?ations=20de=20base=20des=20ressources.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/ModelGenerator.php | 62 ++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 29 deletions(-)

diff --git a/src/ModelGenerator.php b/src/ModelGenerator.php
index 528cd95..be77d09 100644
--- a/src/ModelGenerator.php
+++ b/src/ModelGenerator.php
@@ -22,7 +22,6 @@ namespace Irstea\NgModelGeneratorBundle;
 use ApiPlatform\Core\Documentation\Documentation;
 use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
 use Irstea\NgModelGeneratorBundle\Exceptions\Exception;
-use Irstea\NgModelGeneratorBundle\Exceptions\TypeError;
 use Irstea\NgModelGeneratorBundle\Iterators\IteratorBuilder;
 use Irstea\NgModelGeneratorBundle\Metadata\MetadataFactoryInterface;
 use Irstea\NgModelGeneratorBundle\Metadata\ResourceMetadata;
@@ -261,24 +260,25 @@ final class ModelGenerator
     {
         $repositories = [];
 
-        /**
-         * @var ClassName
-         * @var ResourceMetadata $resourceMeta
-         */
+        // Premier passage pour prégénérer des référénces aux ressources
+        foreach ($this->getResourceMetadata() as $class => $resourceMeta) {
+            /* @var ClassName $class */
+            /* @var ResourceMetadata $resourceMeta */
+            $ref = $this->typeFactory->defer($class->getFullName());
+            $this->typeFactory->add($resourceMeta->getBaseName(), $ref);
+        }
+
+        // Maintenant on génére les repositories
         foreach ($this->getResourceMetadata() as $class => $resourceMeta) {
+            /* @var ClassName $class */
+            /* @var ResourceMetadata $resourceMeta */
             try {
                 $repo = $this->buildRepositories($resourceMeta);
-                if (!$repo) {
-                    continue;
+                if ($repo) {
+                    $repositories[$repo->getName()] = $repo;
                 }
-                $repositories[$repo->getName()] = $this->typeFactory->getOrCreate(
-                    $repo->getName(),
-                    function () use ($repo): Type {
-                        return $repo;
-                    }
-                )->getAsType(Repository::class);
             } catch (Exception $ex) {
-                printf("/* Erreur pendant le mapping de %s:\n *   %s */\n", $class, $ex->getMessage());
+                printf("Erreur pendant le mapping de %s: %s\n", $class, $ex->getMessage());
             }
         }
 
@@ -290,38 +290,42 @@ final class ModelGenerator
     /**
      * @param ResourceMetadata $resourceMeta
      *
-     * @return Repository`null
+     * @return Repository|null
      */
     private function buildRepositories(ResourceMetadata $resourceMeta): ?Repository
     {
-        $operations = [];
-        $iri = null;
-
         $defaultNormalization = $resourceMeta->getDefaultNormalization();
+
         $defaultNormalizationMapper = $this->serializationMapperFactory->create($defaultNormalization, true);
 
+        /**
+         * @var Type
+         * @var Property   $identifier
+         * @var Property[] $properties
+         */
         [$defaultRepr, $identifier, $properties] = $defaultNormalizationMapper->getResourceData();
 
         if (!$properties) {
-            throw new TypeError(
-                sprintf(
-                    'No property found for %s (groups: [%s])',
-                    $resourceMeta->getBaseName(),
-                    implode(', ', $defaultNormalization->getGroups())
-                )
+            printf(
+                "No property found for %s (groups: [%s])\n",
+                $resourceMeta->getBaseName(),
+                implode(', ', $defaultNormalization->getGroups())
             );
+
+            return null;
         }
 
         if (!$identifier) {
-            throw new TypeError(
-                sprintf(
-                    'No identifier found for %s (groups: [%s])',
+            printf(
+                "No identifier found for %s (groups: [%s])\n",
                     $resourceMeta->getBaseName(),
                     implode(', ', $defaultNormalization->getGroups())
-                )
-            );
+                );
+
+            return null;
         }
 
+        $operations = [];
         $pathParser = new PathParser($resourceMeta, $properties);
 
         foreach ($resourceMeta->getOperations() as $operation) {
-- 
GitLab