Skip to content

[Config] Improve casting config nodes to array #51273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->prototype('scalar')->end()
->end()
->arrayNode('trusted_hosts')
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->scalarNode('trusted_proxies')->end()
Expand Down Expand Up @@ -416,10 +416,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->end()
->end()
->arrayNode('supports')
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')
->cannotBeEmpty()
->validate()
Expand Down Expand Up @@ -550,20 +547,14 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->example('is_fully_authenticated() and is_granted(\'ROLE_JOURNALIST\') and subject.getTitle() == \'My first article\'')
->end()
->arrayNode('from')
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->requiresAtLeastOneElement()
->prototype('scalar')
->cannotBeEmpty()
->end()
->end()
->arrayNode('to')
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->requiresAtLeastOneElement()
->prototype('scalar')
->cannotBeEmpty()
Expand Down Expand Up @@ -934,7 +925,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
->children()
->arrayNode('fallbacks')
->info('Defaults to the value of "default_locale".')
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->defaultValue([])
->end()
Expand Down Expand Up @@ -1420,7 +1411,7 @@ private function addLockSection(ArrayNodeDefinition $rootNode, callable $enableI
->end()
->prototype('array')
->performNoDeepMerging()
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->end()
Expand Down Expand Up @@ -2180,7 +2171,7 @@ private function addNotifierSection(ArrayNodeDefinition $rootNode, callable $ena
->arrayNode('channel_policy')
->useAttributeAsKey('name')
->prototype('array')
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode): void
->scalarNode('host')->defaultNull()->end()
->integerNode('port')->defaultNull()->end()
->arrayNode('ips')
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->arrayNode('attributes')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public function addConfiguration(NodeDefinition $node): void
->scalarNode('realm')->defaultNull()->end()
->arrayNode('token_extractors')
->fixXmlConfig('token_extractors')
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->cannotBeEmpty()
->defaultValue([
'security.access_token_extractor.header',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ public function addConfiguration(NodeDefinition $node): void
->end()
->scalarNode('service')->end()
->arrayNode('user_providers')
->beforeNormalization()
->ifString()->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->booleanNode('catch_exceptions')->defaultTrue()->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function ifTrue(\Closure $closure = null): static
public function ifString(): static
{
$this->ifPart = \is_string(...);
$this->allowedTypes = self::TYPE_STRING;
$this->allowedTypes = self::TYPE_STRING; // XXX why is this used by ConfigBuilderGenerator instead of the prototype?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default it's type TYPE_ANY what results in mixed. For me it seems that this is a workaround to interpret this as a string after the check.


return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->children()
->arrayNode('simple_array')
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->arrayNode('keyed_array')
->useAttributeAsKey('name')
->prototype('array')
->beforeNormalization()
->ifString()->then(fn ($v) => [$v])
->end()
->beforeNormalization()->castToArray()->end()
->prototype('scalar')->end()
->end()
->end()
Expand Down