فهرست منبع

Transaction fix

ilGala 4 سال پیش
والد
کامیت
c1c4292757
1فایلهای تغییر یافته به همراه49 افزوده شده و 43 حذف شده
  1. 49 43
      src/Extracting/Strategies/Responses/UseApiResourceTags.php

+ 49 - 43
src/Extracting/Strategies/Responses/UseApiResourceTags.php

@@ -66,53 +66,60 @@ class UseApiResourceTags extends Strategy
      */
     public function getApiResourceResponse(array $tags, Route $route)
     {
-        if (empty($apiResourceTag = $this->getApiResourceTag($tags))) {
-            return null;
-        }
+        try {
+            $this->startDbTransaction();
 
-        [$statusCode, $apiResourceClass] = $this->getStatusCodeAndApiResourceClass($apiResourceTag);
-        [$model, $factoryStates, $relations, $pagination] = $this->getClassToBeTransformedAndAttributes($tags);
-        $modelInstance = $this->instantiateApiResourceModel($model, $factoryStates, $relations);
+            if (empty($apiResourceTag = $this->getApiResourceTag($tags))) {
+                return null;
+            }
 
-        try {
-            $resource = new $apiResourceClass($modelInstance);
-        } catch (Exception $e) {
-            // If it is a ResourceCollection class, it might throw an error
-            // when trying to instantiate with something other than a collection
-            $resource = new $apiResourceClass(collect([$modelInstance]));
-        }
-        if (strtolower($apiResourceTag->getName()) == 'apiresourcecollection') {
-            // Collections can either use the regular JsonResource class (via `::collection()`,
-            // or a ResourceCollection (via `new`)
-            // See https://laravel.com/docs/5.8/eloquent-resources
-            $models = [$modelInstance, $this->instantiateApiResourceModel($model, $factoryStates, $relations)];
-            // Pagination can be in two forms:
-            // [15] : means ::paginate(15)
-            // [15, 'simple'] : means ::simplePaginate(15)
-            if (count($pagination) == 1) {
-                $perPage = $pagination[0];
-                $paginator = new LengthAwarePaginator(
-                // For some reason, the LengthAware paginator needs only first page items to work correctly
-                    collect($models)->slice(0, $perPage),
-                    count($models),
-                    $perPage
-                );
-                $list = $paginator;
-            } elseif (count($pagination) == 2 && $pagination[1] == 'simple') {
-                $perPage = $pagination[0];
-                $paginator = new Paginator($models, $perPage);
-                $list = $paginator;
-            } else {
-                $list = collect($models);
+            [$statusCode, $apiResourceClass] = $this->getStatusCodeAndApiResourceClass($apiResourceTag);
+            [$model, $factoryStates, $relations, $pagination] = $this->getClassToBeTransformedAndAttributes($tags);
+            $modelInstance = $this->instantiateApiResourceModel($model, $factoryStates, $relations);
+
+            try {
+                $resource = new $apiResourceClass($modelInstance);
+            } catch (Exception $e) {
+                // If it is a ResourceCollection class, it might throw an error
+                // when trying to instantiate with something other than a collection
+                $resource = new $apiResourceClass(collect([$modelInstance]));
             }
-            /** @var JsonResource $resource */
-            $resource = $resource instanceof ResourceCollection
-                ? new $apiResourceClass($list)
-                : $apiResourceClass::collection($list);
+            if (strtolower($apiResourceTag->getName()) == 'apiresourcecollection') {
+                // Collections can either use the regular JsonResource class (via `::collection()`,
+                // or a ResourceCollection (via `new`)
+                // See https://laravel.com/docs/5.8/eloquent-resources
+                $models = [$modelInstance, $this->instantiateApiResourceModel($model, $factoryStates, $relations)];
+                // Pagination can be in two forms:
+                // [15] : means ::paginate(15)
+                // [15, 'simple'] : means ::simplePaginate(15)
+                if (count($pagination) == 1) {
+                    $perPage = $pagination[0];
+                    $paginator = new LengthAwarePaginator(
+                    // For some reason, the LengthAware paginator needs only first page items to work correctly
+                        collect($models)->slice(0, $perPage),
+                        count($models),
+                        $perPage
+                    );
+                    $list = $paginator;
+                } elseif (count($pagination) == 2 && $pagination[1] == 'simple') {
+                    $perPage = $pagination[0];
+                    $paginator = new Paginator($models, $perPage);
+                    $list = $paginator;
+                } else {
+                    $list = collect($models);
+                }
+                /** @var JsonResource $resource */
+                $resource = $resource instanceof ResourceCollection
+                    ? new $apiResourceClass($list)
+                    : $apiResourceClass::collection($list);
+            }
+        }  catch (Exception $e) {
+            c::debug($e->getMessage());
+            e::dumpExceptionIfVerbose($e);
+        } finally {
+            $this->endDbTransaction();
         }
 
-        $this->endDbTransaction();
-
         /** @var Response $response */
         $response = $resource->toResponse(app(Request::class)->setRouteResolver(function () use ($route) {
             return $route;
@@ -175,7 +182,6 @@ class UseApiResourceTags extends Strategy
      */
     protected function instantiateApiResourceModel(string $type, array $factoryStates = [], array $relations = [])
     {
-        $this->startDbTransaction();
         try {
             // Try Eloquent model factory