From 57e37826d5567c78a2f675f1ab43bf940b439bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 26 Aug 2024 13:34:04 +0200 Subject: [PATCH] IBX-8784: Prevent initialization of RichText config as Twig global variable --- phpstan-baseline.neon | 25 +++++-------------- .../DependencyInjection/Configuration.php | 13 +++++----- .../IbexaFieldTypeRichTextExtension.php | 22 ++++++++-------- src/bundle/Resources/config/templating.yaml | 4 ++- .../RichTextConfigurationExtension.php | 9 ++++++- .../Configuration/ConfigurationTest.php | 2 ++ .../output/000-attribute-type-number.yaml | 1 + .../output/001-attribute-type-string.yaml | 1 + .../output/002-attribute-type-boolean.yaml | 1 + .../output/003-attribute-type-choice.yaml | 1 + .../output/004-attribute-type-link.yaml | 1 + 11 files changed, 41 insertions(+), 39 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4924532e..88df03fb 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -100,16 +100,6 @@ parameters: count: 1 path: src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php - - - message: "#^Method Ibexa\\\\Bundle\\\\FieldTypeRichText\\\\DependencyInjection\\\\IbexaFieldTypeRichTextExtension\\:\\:load\\(\\) has no return type specified\\.$#" - count: 1 - path: src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php - - - - message: "#^Method Ibexa\\\\Bundle\\\\FieldTypeRichText\\\\DependencyInjection\\\\IbexaFieldTypeRichTextExtension\\:\\:load\\(\\) has parameter \\$configs with no value type specified in iterable type array\\.$#" - count: 1 - path: src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php - - message: "#^Method Ibexa\\\\Bundle\\\\FieldTypeRichText\\\\DependencyInjection\\\\IbexaFieldTypeRichTextExtension\\:\\:prepend\\(\\) has no return type specified\\.$#" count: 1 @@ -150,11 +140,6 @@ parameters: count: 1 path: src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php - - - message: "#^Parameter \\#1 \\$configuration of method Symfony\\\\Component\\\\DependencyInjection\\\\Extension\\\\Extension\\:\\:processConfiguration\\(\\) expects Symfony\\\\Component\\\\Config\\\\Definition\\\\ConfigurationInterface, Symfony\\\\Component\\\\Config\\\\Definition\\\\ConfigurationInterface\\|null given\\.$#" - count: 1 - path: src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php - - message: "#^Parameter \\#1 \\$filename of static method Symfony\\\\Component\\\\Yaml\\\\Yaml\\:\\:parseFile\\(\\) expects string, string\\|false given\\.$#" count: 2 @@ -1314,10 +1299,7 @@ parameters: message: "#^Cannot access property \\$id on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field\\|null\\.$#" count: 1 path: tests/integration/Repository/RichTextFieldTypeIntegrationTest.php - - - message: "#^Cannot call method fetchOne\\(\\) on Doctrine\\\\DBAL\\\\ForwardCompatibility\\\\Result\\|int\\|string\\.$#" - count: 1 - path: tests/integration/Repository/RichTextFieldTypeIntegrationTest.php + - message: "#^Cannot access property \\$value on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field\\|null\\.$#" count: 1 @@ -1333,6 +1315,11 @@ parameters: count: 1 path: tests/integration/Repository/RichTextFieldTypeIntegrationTest.php + - + message: "#^Cannot call method fetchOne\\(\\) on Doctrine\\\\DBAL\\\\ForwardCompatibility\\\\Result\\|int\\|string\\.$#" + count: 1 + path: tests/integration/Repository/RichTextFieldTypeIntegrationTest.php + - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\FieldTypeRichText\\\\Repository\\\\RichTextFieldTypeIntegrationTest\\:\\:__construct\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/bundle/DependencyInjection/Configuration.php b/src/bundle/DependencyInjection/Configuration.php index e4fa52b2..d03a0aad 100644 --- a/src/bundle/DependencyInjection/Configuration.php +++ b/src/bundle/DependencyInjection/Configuration.php @@ -18,18 +18,19 @@ class Configuration extends SiteAccessConfiguration { public const CUSTOM_TAG_ATTRIBUTE_TYPES = ['number', 'string', 'boolean', 'choice', 'link']; - /** - * Generates the configuration tree builder. - * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder - */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(IbexaFieldTypeRichTextExtension::EXTENSION_NAME); $rootNode = $treeBuilder->getRootNode(); $sections = $rootNode->children(); + + $rootNode + ->children() + ->booleanNode('expose_config_as_global')->defaultFalse()->end() + ->end(); + $this ->addEnabledAttributeTypesSection($sections); $this diff --git a/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php b/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php index 957e42d7..2ebbc298 100644 --- a/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php +++ b/src/bundle/DependencyInjection/IbexaFieldTypeRichTextExtension.php @@ -14,15 +14,15 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; use Symfony\Component\Yaml\Yaml; /** * Ibexa RichText Field Type Bundle extension. */ -class IbexaFieldTypeRichTextExtension extends Extension implements PrependExtensionInterface +class IbexaFieldTypeRichTextExtension extends ConfigurableExtension implements PrependExtensionInterface { public const EXTENSION_NAME = 'ibexa_fieldtype_richtext'; @@ -45,15 +45,15 @@ public function getAlias() } /** - * Load Ibexa RichText Field Type Bundle configuration. - * - * @param array $configs - * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container - * - * @throws \Exception + * @param array $mergedConfig */ - public function load(array $configs, ContainerBuilder $container) + protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void { + $container->setParameter( + 'ibexa.field_type.richtext.expose_config_as_global', + $mergedConfig['expose_config_as_global'], + ); + $settingsLoader = new Loader\YamlFileLoader( $container, new FileLocator(__DIR__ . '/../Resources/config/settings') @@ -84,9 +84,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('command.yaml'); $loader->load('controller.yaml'); - $configuration = $this->getConfiguration($configs, $container); - $config = $this->processConfiguration($configuration, $configs); - $this->registerRichTextConfiguration($config, $container); + $this->registerRichTextConfiguration($mergedConfig, $container); (new IbexaEncoreConfigurationDumper($container))->dumpCustomConfiguration( self::WEBPACK_CONFIG_NAMES diff --git a/src/bundle/Resources/config/templating.yaml b/src/bundle/Resources/config/templating.yaml index 42d26faf..e6831569 100644 --- a/src/bundle/Resources/config/templating.yaml +++ b/src/bundle/Resources/config/templating.yaml @@ -11,4 +11,6 @@ services: Ibexa\Bundle\FieldTypeRichText\Templating\Twig\Extension\YoutubeIdExtractorExtension: ~ - Ibexa\Bundle\FieldTypeRichText\Templating\Twig\Extension\RichTextConfigurationExtension: ~ + Ibexa\Bundle\FieldTypeRichText\Templating\Twig\Extension\RichTextConfigurationExtension: + arguments: + $exposeGlobals: '%ibexa.field_type.richtext.expose_config_as_global%' diff --git a/src/bundle/Templating/Twig/Extension/RichTextConfigurationExtension.php b/src/bundle/Templating/Twig/Extension/RichTextConfigurationExtension.php index e85b4013..5484a5af 100644 --- a/src/bundle/Templating/Twig/Extension/RichTextConfigurationExtension.php +++ b/src/bundle/Templating/Twig/Extension/RichTextConfigurationExtension.php @@ -22,13 +22,20 @@ final class RichTextConfigurationExtension extends AbstractExtension implements /** @var \Ibexa\Contracts\FieldTypeRichText\Configuration\ProviderService */ private $configurationProvider; - public function __construct(ProviderService $configurationProvider) + private bool $exposeGlobals; + + public function __construct(ProviderService $configurationProvider, bool $exposeGlobals = false) { $this->configurationProvider = $configurationProvider; + $this->exposeGlobals = $exposeGlobals; } public function getGlobals(): array { + if (!$this->exposeGlobals) { + return []; + } + $config = $this->configurationProvider->getConfiguration(); return [ diff --git a/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php b/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php index 7c49c8a4..2d65bfef 100644 --- a/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php +++ b/tests/bundle/DependencyInjection/Configuration/ConfigurationTest.php @@ -74,6 +74,7 @@ public function providerForTestProcessingConfiguration(): array 'custom_tags' => [], 'custom_styles' => [], 'enabled_attribute_types' => ['number', 'string', 'boolean', 'choice', 'link'], + 'expose_config_as_global' => false, ], ], 'Alloy editor configs from multiple sources' => [ @@ -113,6 +114,7 @@ public function providerForTestProcessingConfiguration(): array 'custom_tags' => [], 'custom_styles' => [], 'enabled_attribute_types' => ['number', 'string', 'boolean', 'choice', 'link'], + 'expose_config_as_global' => false, ], ], ]; diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml index 6ae7f80a..c9adef07 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/000-attribute-type-number.yaml @@ -15,3 +15,4 @@ enabled_attribute_types: - 'boolean' - 'choice' - 'link' +expose_config_as_global: false diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml index a6422979..990d0c4d 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/001-attribute-type-string.yaml @@ -15,3 +15,4 @@ enabled_attribute_types: - 'boolean' - 'choice' - 'link' +expose_config_as_global: false diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml index ca1254f0..6cb04cc5 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/002-attribute-type-boolean.yaml @@ -15,3 +15,4 @@ enabled_attribute_types: - 'boolean' - 'choice' - 'link' +expose_config_as_global: false diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml index e41bfec1..22d96f15 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/003-attribute-type-choice.yaml @@ -16,3 +16,4 @@ enabled_attribute_types: - 'boolean' - 'choice' - 'link' +expose_config_as_global: false diff --git a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml index 9195a030..ba08c42e 100644 --- a/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml +++ b/tests/bundle/DependencyInjection/Fixtures/custom_tags/output/004-attribute-type-link.yaml @@ -15,3 +15,4 @@ enabled_attribute_types: - 'boolean' - 'choice' - 'link' +expose_config_as_global: false