Get bundle config value with interpolated parameters in Symfony3 - symfony

I have a bundle with its custom *Extension class where I need to read configuration from another bundle (SecurityBundle in particular) where I have for example.
security:
...
firewalls:
main:
pattern: '^%url_prefix%'
I'm wondering how can I get value for security.firewalls.main.pattern with interpolated url_prefix parameter?

Retrieving configuration values (of any bundle different than the one into what your extension is located, because you are in an extension) is not supported, and it seems that'll not be in the future.
The only ways are:
Define a parameter representing the whole option's value (as pointed by this answer on a similar question):
# app/config/security.yml
parameters:
firewalls.main.pattern: '^%url_prefix%'
# ...
security:
# ...
firewalls:
main:
pattern: '^%url_prefix%'
Parse your config.yml using the Yaml component:
$yamlPath = $this->getParameter('kernel.root_dir').'/config/security.yml';
$config = Symfony\Component\Yaml\Yaml::parse(file_get_contents($yamlPath));
// The option value
$value = $config['security']['firewalls']['main']['pattern'];
I think that's really a pity to don't be able to retrieve a config option from any container-aware context without doing such hacks.

Related

Parameter in parameter in services.yml

I am using symfony5 and what I want to achive is to use a parameter in another parameter. Is it possible? Of course I've searchd for it, but did not find a solution. For example:
media:
extendions:
- pdf
path: '%kernel.project_dir%/media/'
incomingPdf:
#path: '%kernel.project_dir%/media/incoming/pdf'
path: '#=parameter("media")["path"]incoming/pdf'
It says, expressions is not allowed in parameters.
In incomingPdf I want to use defined media['path']s value.

symfony 4 - default normalization to snake_case

trying to get the sf4 serializer component to use snake_case as the default:
Symfony\Component\Serializer\Normalizer\ObjectNormalizer:
public: true
arguments: ['#serializer.mapping.class_metadata_factory', '#serializer.name_converter.camel_case_to_snake_case']
tags: [serializer.normalizer]
works.
but now DateTime is being normalized to empty arrays.
I don't get why, without the config changes, its normalized to a date string, as you would expect.
What am i doing wrong here?
turns out, you simply need to enable a name_converter the proper way like this instead:
# config/packages/framework.yaml
framework:
# ...
serializer:
name_converter: 'serializer.name_converter.camel_case_to_snake_case'
see
https://symfony.com/doc/current/serializer.html#enabling-a-name-converter
https://github.com/symfony/symfony/issues/40818

Symfony FOSRestBundle enableMaxDepthChecks default to true

Is it possible to set the default value for enableMaxDepthChecks to true?
Currently I have to add the following annotation to every route:
#View(serializerEnableMaxDepthChecks=true)
Is there a way to set this in the config or elsewhere to be the default?
Place this into your config.yml
jms_serializer:
default_context:
serialization:
enable_max_depth_checks: true
BUT! After making these changes, the parameter serializerEnableMaxDepthChecks becomes non-working, so you can't set it to false , and I don't know why...

Set the default propel connection for entire php process

Propel 1.6 and Symfony 1.4
I'm looking for a way to programmatically set the default propel connection for the length of an entire php process. The issue is that I'm using an alternative db for testing purposes and I have a good deal of code that doesn't pass the PropelPDO object currently.
Can this be done? Any tips? Thanks.
Why not use environments in your databases.yml?
dev:
propel:
class: sfPropelDatabase
param:
classname: DebugPDO
etc, etc
stage:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
etc, etc
prod:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
etc, etc
So, the solution to this was to use the following, pretty clean and sweet:
//override the "default" "propel" dsn and set it to our testing db!
\Propel::setConnection(
"propel",
Propel::getConnection(SqliteSetup::$databaseName)
);

How to access arrays in symfony's configuration files?

Does the symfony2 can handle only flat parameters?
Say we have:
services:
manager:
class: blabla
arguments: [%app.vat%]
and in app.yml :
parameters:
app.vat: 24.5
it works, but
parameters:
app:
vat: 24.5
does not work. Is there some special syntax to access arrays or this is not possible?
This is indeed possible. You can access the values from your example in your code like this:
$config = $this->get('service_container')->getParameter('app.vat');
If this is still not working you should try to rename "app" into something else (e.g. "application"). Symfony preserves the name "app" on many places and handles it in a special way.

Resources