From a5106546fe98ee22106dfc20595c1be14ef54825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 19 Oct 2020 14:34:25 +0200 Subject: [PATCH] EZP-31825: Fixed crashes when ezcontentquery field parameters are invalid --- composer.json | 3 +- .../ExceptionSafeQueryFieldServiceSpec.php | 55 ++++++++++++++ src/API/ExceptionSafeQueryFieldService.php | 74 +++++++++++++++++++ ...stemsEzPlatformQueryFieldTypeExtension.php | 3 + .../Resources/config/prod/services.yaml | 8 ++ 5 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 spec/API/ExceptionSafeQueryFieldServiceSpec.php create mode 100644 src/API/ExceptionSafeQueryFieldService.php create mode 100644 src/Symfony/Resources/config/prod/services.yaml diff --git a/composer.json b/composer.json index 5e97442..9215a8c 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "symfony/framework-bundle": "^5.0", "symfony/http-kernel": "^5.0", "symfony/translation": "^5.0", - "symfony/yaml": "^5.0" + "symfony/yaml": "^5.0", + "psr/log": "^1.1" }, "autoload": { "psr-4": { diff --git a/spec/API/ExceptionSafeQueryFieldServiceSpec.php b/spec/API/ExceptionSafeQueryFieldServiceSpec.php new file mode 100644 index 0000000..fdce568 --- /dev/null +++ b/spec/API/ExceptionSafeQueryFieldServiceSpec.php @@ -0,0 +1,55 @@ +countContentItems(...$arguments)->willThrow('Exception'); + $queryFieldService->loadContentItems(...$arguments)->willThrow('Exception'); + + $arguments[] = Argument::type('int'); + $arguments[] = Argument::type('int'); + $queryFieldService->loadContentItemsSlice(...$arguments)->willThrow('Exception'); + + $this->beConstructedWith($queryFieldService); + } + + function it_is_initializable() + { + $this->shouldHaveType(ExceptionSafeQueryFieldService::class); + } + + function it_should_return_empty_results_on_count_content_items() + { + $result = $this->countContentItems(new Content([]), 'any'); + $result->shouldBe(0); + } + + function it_should_return_empty_results_on_load_content_items() + { + $result = $this->loadContentItems(new Content([]), 'any'); + $result->shouldBe([]); + } + + function it_should_return_empty_results_on_load_content_items_slice() + { + $result = $this->loadContentItemsSlice(new Content([]), 'any', 0, 5); + $result->shouldBe([]); + } +} diff --git a/src/API/ExceptionSafeQueryFieldService.php b/src/API/ExceptionSafeQueryFieldService.php new file mode 100644 index 0000000..99fbcb4 --- /dev/null +++ b/src/API/ExceptionSafeQueryFieldService.php @@ -0,0 +1,74 @@ +inner = $inner; + $this->logger = $logger ?: new NullLogger(); + } + + public function loadContentItems(Content $content, string $fieldDefinitionIdentifier): iterable + { + try { + return $this->inner->loadContentItems($content, $fieldDefinitionIdentifier); + } catch (\Throwable $e) { + $this->logger->error($e->getMessage(), [ + 'exception' => $e, + ]); + + return []; + } + } + + public function countContentItems(Content $content, string $fieldDefinitionIdentifier): int + { + try { + return $this->inner->countContentItems($content, $fieldDefinitionIdentifier); + } catch (\Throwable $e) { + $this->logger->error($e->getMessage(), [ + 'exception' => $e, + ]); + + return 0; + } + } + + public function loadContentItemsSlice(Content $content, string $fieldDefinitionIdentifier, int $offset, int $limit): iterable + { + try { + return $this->inner->loadContentItemsSlice($content, $fieldDefinitionIdentifier, $offset, $limit); + } catch (\Throwable $e) { + $this->logger->error($e->getMessage(), [ + 'exception' => $e, + ]); + + return []; + } + } + + public function getPaginationConfiguration(Content $content, string $fieldDefinitionIdentifier): int + { + return $this->inner->getPaginationConfiguration($content, $fieldDefinitionIdentifier); + } +} diff --git a/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php b/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php index 3fdd095..3cc3b52 100644 --- a/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php +++ b/src/Symfony/DependencyInjection/EzSystemsEzPlatformQueryFieldTypeExtension.php @@ -25,6 +25,9 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('default_parameters.yaml'); $loader->load('services.yaml'); + if (!$container->hasParameter('kernel.debug') || !$container->getParameter('kernel.debug')) { + $loader->load('prod/services.yaml'); + } $this->addContentViewConfig($container); } diff --git a/src/Symfony/Resources/config/prod/services.yaml b/src/Symfony/Resources/config/prod/services.yaml new file mode 100644 index 0000000..7ac98ef --- /dev/null +++ b/src/Symfony/Resources/config/prod/services.yaml @@ -0,0 +1,8 @@ +services: + _defaults: + autoconfigure: true + autowire: true + public: true + + EzSystems\EzPlatformQueryFieldType\API\ExceptionSafeQueryFieldService: + decorates: 'EzSystems\EzPlatformQueryFieldType\API\QueryFieldServiceInterface'