From 8dd479689e6f96601fbeae4af993aecce06a1faf Mon Sep 17 00:00:00 2001 From: Gustavo Caetano Date: Tue, 19 Jul 2022 21:25:36 -0300 Subject: [PATCH 1/2] Prevent configuration from breaking on problematic Resources --- src/Http/Controllers/ImportController.php | 46 ++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/Http/Controllers/ImportController.php b/src/Http/Controllers/ImportController.php index 7cfbc4e..5e350d2 100644 --- a/src/Http/Controllers/ImportController.php +++ b/src/Http/Controllers/ImportController.php @@ -43,7 +43,7 @@ public function configure(NovaRequest $request, string $file): Response $rows = $import->take(10)->all(); - $resources = $this->getAvailableResourcesForImport($request); + $resources = $this->getAvailableResourcesForImport($request); $fields = $resources->mapWithKeys(function ($resource) use ($request) { return $this->getAvailableFieldsForImport($resource, $request); @@ -64,7 +64,7 @@ public function configure(NovaRequest $request, string $file): Response } /** - * + * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ @@ -197,28 +197,32 @@ public function review(NovaRequest $request, string $file): Response protected function getAvailableFieldsForImport(string $resource, NovaRequest $request): array { - $novaResource = new $resource(new $resource::$model); - $fieldsCollection = collect($novaResource->creationFields($request)); - - if (method_exists($novaResource, 'excludeAttributesFromImport')) { - $fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) { - return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request)); + try { + $novaResource = new $resource(new $resource::$model); + $fieldsCollection = collect($novaResource->creationFields($request)); + + if (method_exists($novaResource, 'excludeAttributesFromImport')) { + $fieldsCollection = $fieldsCollection->filter(function (Field $field) use ($novaResource, $request) { + return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request)); + }); + } + + $fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) { + return [ + 'name' => $field->name, + 'attribute' => $field->attribute, + 'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute), + ]; }); - } - - $fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) { + + // Note: ->values() is used here to avoid this array being turned into an object due to + // non-sequential keys (which might happen due to the filtering above. return [ - 'name' => $field->name, - 'attribute' => $field->attribute, - 'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute), + $novaResource->uriKey() => $fields->values(), ]; - }); - - // Note: ->values() is used here to avoid this array being turned into an object due to - // non-sequential keys (which might happen due to the filtering above. - return [ - $novaResource->uriKey() => $fields->values(), - ]; + } catch (\Exception $e) { + return []; + } } protected function getAvailableResourcesForImport(NovaRequest $request): Collection From 386df6b65c059b5239102ddc70b0902675799ce1 Mon Sep 17 00:00:00 2001 From: Gustavo Caetano Date: Tue, 19 Jul 2022 21:27:37 -0300 Subject: [PATCH 2/2] Prevent configuration from breaking on problematic Resources --- src/Http/Controllers/ImportController.php | 41 +++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Http/Controllers/ImportController.php b/src/Http/Controllers/ImportController.php index 7cfbc4e..bdc64b2 100644 --- a/src/Http/Controllers/ImportController.php +++ b/src/Http/Controllers/ImportController.php @@ -197,28 +197,33 @@ public function review(NovaRequest $request, string $file): Response protected function getAvailableFieldsForImport(string $resource, NovaRequest $request): array { - $novaResource = new $resource(new $resource::$model); - $fieldsCollection = collect($novaResource->creationFields($request)); - - if (method_exists($novaResource, 'excludeAttributesFromImport')) { - $fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) { - return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request)); + try { + $novaResource = new $resource(new $resource::$model); + $fieldsCollection = collect($novaResource->creationFields($request)); + + if (method_exists($novaResource, 'excludeAttributesFromImport')) { + $fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) { + return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request)); + }); + } + + $fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) { + return [ + 'name' => $field->name, + 'attribute' => $field->attribute, + 'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute), + ]; }); - } - - $fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) { + + // Note: ->values() is used here to avoid this array being turned into an object due to + // non-sequential keys (which might happen due to the filtering above. return [ - 'name' => $field->name, - 'attribute' => $field->attribute, - 'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute), + $novaResource->uriKey() => $fields->values(), ]; - }); + } catch (\Throwable $th) { + return []; + } - // Note: ->values() is used here to avoid this array being turned into an object due to - // non-sequential keys (which might happen due to the filtering above. - return [ - $novaResource->uriKey() => $fields->values(), - ]; } protected function getAvailableResourcesForImport(NovaRequest $request): Collection