Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to properly use '%' for escaping YAML configuration in Symfony?

$
0
0

When it comes to creating bundles, it's often necessary to support some configuration for the developers who'll use the bundle.

In this case, I need the configuration's value to support %someText%, without Symfony trying to resolve this value as parameter.

Example: Incorrect Configuration

my_bundle_name:
    my_configuration: '%someText%'

So, I figured, I'll have to escape the % (so it looks like %%) to make things work - resulting in the value %%someText%%.

If some parameter value includes the % character, you need to escape it by adding another % so Symfony doesn't consider it a reference to a parameter name [...]

Example: No Error, but Unexpected Result

my_bundle_name:
    my_configuration: '%%someText%%'

While this solves the You have requested a non-existent parameter[...] error, the actual value is now %%someText%%. I expected Symfony to return the unsecaped value instead, i.e., %someText%.

Sure I could str_replace('%%', '%', '%%someText%%'), but this feels hack-ish.

How would I change the value to actually get %someText% instead?

Here's where the configuration is loaded:

// Namespace and use statements omitted for brevity
class MyBundleNameExtension extends ConfigurableExtension
{
    /**
     * @inheritDoc
     */
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
        $loader->load('config.yml');

        p_r($mergedConfig["my_configuration"]); // Prints %%someText%%
    }
}

I've verified that using both ' and " yields the same result.


Viewing all articles
Browse latest Browse all 3925

Trending Articles