diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index aa062e3bb..c4a1c0df3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -55,21 +55,11 @@ parameters: count: 1 path: src/bundle/DependencyInjection/Compiler/ValueObjectVisitorPass.php - - - message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has no return type specified\\.$#" - count: 1 - path: src/bundle/DependencyInjection/Configuration.php - - message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has parameter \\$rootNode with no type specified\\.$#" count: 1 path: src/bundle/DependencyInjection/Configuration.php - - - message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:load\\(\\) has no return type specified\\.$#" - count: 1 - path: src/bundle/DependencyInjection/IbexaRestExtension.php - - message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:prepend\\(\\) has no return type specified\\.$#" count: 1 diff --git a/src/bundle/DependencyInjection/Configuration.php b/src/bundle/DependencyInjection/Configuration.php index 287fa30ea..b4619eec4 100644 --- a/src/bundle/DependencyInjection/Configuration.php +++ b/src/bundle/DependencyInjection/Configuration.php @@ -11,16 +11,25 @@ class Configuration extends SiteAccessConfiguration { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(IbexaRestExtension::EXTENSION_NAME); - $this->addRestRootResourcesSection($treeBuilder->getRootNode()); + $rootNode = $treeBuilder->getRootNode(); + $rootNode + ->children() + ->booleanNode('strict_mode') + ->defaultValue('%kernel.debug%') + ->info('Throw exceptions for missing normalizers.') + ->end() + ->end(); + + $this->addRestRootResourcesSection($rootNode); return $treeBuilder; } - public function addRestRootResourcesSection($rootNode) + private function addRestRootResourcesSection($rootNode): void { $systemNode = $this->generateScopeBaseNode($rootNode); $systemNode diff --git a/src/bundle/DependencyInjection/IbexaRestExtension.php b/src/bundle/DependencyInjection/IbexaRestExtension.php index 7994dae41..331db144f 100644 --- a/src/bundle/DependencyInjection/IbexaRestExtension.php +++ b/src/bundle/DependencyInjection/IbexaRestExtension.php @@ -12,7 +12,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; use Symfony\Component\Yaml\Yaml; /** @@ -20,7 +20,7 @@ * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} */ -class IbexaRestExtension extends Extension implements PrependExtensionInterface +class IbexaRestExtension extends ConfigurableExtension implements PrependExtensionInterface { public const EXTENSION_NAME = 'ibexa_rest'; @@ -30,12 +30,11 @@ public function getAlias(): string } /** - * {@inheritdoc} + * @param array $mergedConfig */ - public function load(array $configs, ContainerBuilder $container) + protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void { - $configuration = $this->getConfiguration($configs, $container); - $config = $this->processConfiguration($configuration, $configs); + $container->setParameter('ibexa.rest.strict_mode', $mergedConfig['strict_mode']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); @@ -45,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('default_settings.yml'); $processor = new ConfigurationProcessor($container, 'ibexa.site_access.config'); - $processor->mapConfigArray('rest_root_resources', $config); + $processor->mapConfigArray('rest_root_resources', $mergedConfig); } public function prepend(ContainerBuilder $container) diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 7cf7853eb..69d504442 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -344,6 +344,7 @@ services: arguments: $normalizer: '@ibexa.rest.serializer' $logger: '@logger' + $strictMode: '%ibexa.rest.strict_mode%' tags: - { name: monolog.logger, channel: ibexa.rest } @@ -357,6 +358,7 @@ services: arguments: $normalizer: '@ibexa.rest.serializer' $logger: '@logger' + $strictMode: '%ibexa.rest.strict_mode%' tags: - { name: monolog.logger, channel: ibexa.rest } diff --git a/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php b/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php index 89af196d9..77437c97d 100644 --- a/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/Json/FieldTypeHashGenerator.php @@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface private NormalizerInterface $normalizer; + private bool $strictMode; + public function __construct( NormalizerInterface $normalizer, - ?LoggerInterface $logger = null + ?LoggerInterface $logger = null, + bool $strictMode = false ) { $this->normalizer = $normalizer; $this->logger = $logger ?? new NullLogger(); + $this->strictMode = $strictMode; } /** @@ -152,6 +156,9 @@ private function generateObjectValue($parent, object $value) try { $value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]); } catch (ExceptionInterface $e) { + if ($this->strictMode) { + throw $e; + } $message = sprintf( 'Unable to normalize value for type "%s". %s. ' . 'Ensure that a normalizer is registered with tag: "%s".', diff --git a/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php b/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php index c3fcaf953..9a41b1152 100644 --- a/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php @@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface private NormalizerInterface $normalizer; + private bool $strictMode; + public function __construct( NormalizerInterface $normalizer, - ?LoggerInterface $logger = null + ?LoggerInterface $logger = null, + bool $strictMode = false ) { $this->normalizer = $normalizer; $this->logger = $logger ?? new NullLogger(); + $this->strictMode = $strictMode; } /** @@ -243,6 +247,9 @@ private function generateObjectValue(object $value, \XmlWriter $writer, ?string try { $value = $this->normalizer->normalize($value, 'xml'); } catch (ExceptionInterface $e) { + if ($this->strictMode) { + throw $e; + } $message = sprintf( 'Unable to normalize value for type "%s". %s. ' . 'Ensure that a normalizer is registered with tag: "%s".',