Skip to content

Commit 397d3b5

Browse files
authored
Added strict_mode for REST APIs (#124)
1 parent 171a913 commit 397d3b5

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,11 @@ parameters:
5555
count: 1
5656
path: src/bundle/DependencyInjection/Compiler/ValueObjectVisitorPass.php
5757

58-
-
59-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has no return type specified\\.$#"
60-
count: 1
61-
path: src/bundle/DependencyInjection/Configuration.php
62-
6358
-
6459
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has parameter \\$rootNode with no type specified\\.$#"
6560
count: 1
6661
path: src/bundle/DependencyInjection/Configuration.php
6762

68-
-
69-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:load\\(\\) has no return type specified\\.$#"
70-
count: 1
71-
path: src/bundle/DependencyInjection/IbexaRestExtension.php
72-
7363
-
7464
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:prepend\\(\\) has no return type specified\\.$#"
7565
count: 1

src/bundle/DependencyInjection/Configuration.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111

1212
class Configuration extends SiteAccessConfiguration
1313
{
14-
public function getConfigTreeBuilder()
14+
public function getConfigTreeBuilder(): TreeBuilder
1515
{
1616
$treeBuilder = new TreeBuilder(IbexaRestExtension::EXTENSION_NAME);
1717

18-
$this->addRestRootResourcesSection($treeBuilder->getRootNode());
18+
$rootNode = $treeBuilder->getRootNode();
19+
$rootNode
20+
->children()
21+
->booleanNode('strict_mode')
22+
->defaultValue('%kernel.debug%')
23+
->info('Throw exceptions for missing normalizers.')
24+
->end()
25+
->end();
26+
27+
$this->addRestRootResourcesSection($rootNode);
1928

2029
return $treeBuilder;
2130
}
2231

23-
public function addRestRootResourcesSection($rootNode)
32+
private function addRestRootResourcesSection($rootNode): void
2433
{
2534
$systemNode = $this->generateScopeBaseNode($rootNode);
2635
$systemNode

src/bundle/DependencyInjection/IbexaRestExtension.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
use Symfony\Component\DependencyInjection\ContainerBuilder;
1313
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1414
use Symfony\Component\DependencyInjection\Loader;
15-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15+
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
1616
use Symfony\Component\Yaml\Yaml;
1717

1818
/**
1919
* This is the class that loads and manages your bundle configuration.
2020
*
2121
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
2222
*/
23-
class IbexaRestExtension extends Extension implements PrependExtensionInterface
23+
class IbexaRestExtension extends ConfigurableExtension implements PrependExtensionInterface
2424
{
2525
public const EXTENSION_NAME = 'ibexa_rest';
2626

@@ -30,12 +30,11 @@ public function getAlias(): string
3030
}
3131

3232
/**
33-
* {@inheritdoc}
33+
* @param array<string, mixed> $mergedConfig
3434
*/
35-
public function load(array $configs, ContainerBuilder $container)
35+
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
3636
{
37-
$configuration = $this->getConfiguration($configs, $container);
38-
$config = $this->processConfiguration($configuration, $configs);
37+
$container->setParameter('ibexa.rest.strict_mode', $mergedConfig['strict_mode']);
3938

4039
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
4140
$loader->load('services.yml');
@@ -45,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
4544
$loader->load('default_settings.yml');
4645

4746
$processor = new ConfigurationProcessor($container, 'ibexa.site_access.config');
48-
$processor->mapConfigArray('rest_root_resources', $config);
47+
$processor->mapConfigArray('rest_root_resources', $mergedConfig);
4948
}
5049

5150
public function prepend(ContainerBuilder $container)

src/bundle/Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ services:
344344
arguments:
345345
$normalizer: '@ibexa.rest.serializer'
346346
$logger: '@logger'
347+
$strictMode: '%ibexa.rest.strict_mode%'
347348
tags:
348349
- { name: monolog.logger, channel: ibexa.rest }
349350

@@ -357,6 +358,7 @@ services:
357358
arguments:
358359
$normalizer: '@ibexa.rest.serializer'
359360
$logger: '@logger'
361+
$strictMode: '%ibexa.rest.strict_mode%'
360362
tags:
361363
- { name: monolog.logger, channel: ibexa.rest }
362364

src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface
1919

2020
private NormalizerInterface $normalizer;
2121

22+
private bool $strictMode;
23+
2224
public function __construct(
2325
NormalizerInterface $normalizer,
24-
?LoggerInterface $logger = null
26+
?LoggerInterface $logger = null,
27+
bool $strictMode = false
2528
) {
2629
$this->normalizer = $normalizer;
2730
$this->logger = $logger ?? new NullLogger();
31+
$this->strictMode = $strictMode;
2832
}
2933

3034
/**
@@ -152,6 +156,9 @@ private function generateObjectValue($parent, object $value)
152156
try {
153157
$value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]);
154158
} catch (ExceptionInterface $e) {
159+
if ($this->strictMode) {
160+
throw $e;
161+
}
155162
$message = sprintf(
156163
'Unable to normalize value for type "%s". %s. '
157164
. 'Ensure that a normalizer is registered with tag: "%s".',

src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface
1919

2020
private NormalizerInterface $normalizer;
2121

22+
private bool $strictMode;
23+
2224
public function __construct(
2325
NormalizerInterface $normalizer,
24-
?LoggerInterface $logger = null
26+
?LoggerInterface $logger = null,
27+
bool $strictMode = false
2528
) {
2629
$this->normalizer = $normalizer;
2730
$this->logger = $logger ?? new NullLogger();
31+
$this->strictMode = $strictMode;
2832
}
2933

3034
/**
@@ -243,6 +247,9 @@ private function generateObjectValue(object $value, \XmlWriter $writer, ?string
243247
try {
244248
$value = $this->normalizer->normalize($value, 'xml');
245249
} catch (ExceptionInterface $e) {
250+
if ($this->strictMode) {
251+
throw $e;
252+
}
246253
$message = sprintf(
247254
'Unable to normalize value for type "%s". %s. '
248255
. 'Ensure that a normalizer is registered with tag: "%s".',

0 commit comments

Comments
 (0)