Skip to content

[DependencyInjection] Use PHP-DSL env() configurator when possible #16803

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

Merged
merged 1 commit into from
May 16, 2022
Merged
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
8 changes: 8 additions & 0 deletions configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,18 @@ This example shows how you could configure the database connection using an env
'dbal' => [
// by convention the env var names are always uppercase
'url' => '%env(resolve:DATABASE_URL)%',
// or
'url' => env('DATABASE_URL')->resolve(),
],
]);
};

.. versionadded:: 5.3

The ``env()`` configurator syntax was introduced in 5.3.
In ``PHP`` configuration files, it will allow to autocomplete methods based
on processors name (i.e. ``env('SOME_VAR')->default('foo')``).

.. seealso::

The values of env vars can only be strings, but Symfony includes some
Expand Down
10 changes: 9 additions & 1 deletion configuration/env_var_processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ processor to turn the value of the ``HTTP_PORT`` env var into an integer:

return static function (FrameworkConfig $framework) {
$framework->router()
->httpPort('%env(int:HTTP_PORT)%')
// or
->httpPort(env('HTTP_PORT')->int())
;
};

.. versionadded:: 5.3

The ``env()`` configurator syntax was introduced in 5.3.
In ``PHP`` configuration files, it will allow to autocomplete methods based
on processors name (i.e. ``env('SOME_VAR')->default('foo')``).

Built-In Environment Variable Processors
----------------------------------------

Expand Down Expand Up @@ -241,7 +249,7 @@ Symfony provides the following env var processors:
$container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD');
$security->accessControl()
->path('^/health-check$')
->methods(['%env(const:HEALTH_CHECK_METHOD)%']);
->methods([env('HEALTH_CHECK_METHOD')->const()]);
};

``env(base64:FOO)``
Expand Down
2 changes: 1 addition & 1 deletion configuration/secrets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ If you stored a ``DATABASE_PASSWORD`` secret, you can reference it by:
return static function (DoctrineConfig $doctrine) {
$doctrine->dbal()
->connection('default')
->password('%env(DATABASE_PASSWORD)%')
->password(env('DATABASE_PASSWORD'))
;
};

Expand Down
4 changes: 2 additions & 2 deletions doctrine/multiple_entity_managers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ The following configuration code shows how you can configure two entity managers
// configure these for your database server
$doctrine->dbal()
->connection('default')
->url('%env(resolve:DATABASE_URL)%')
->url(env('DATABASE_URL')->resolve())
->driver('pdo_mysql')
->serverVersion('5.7')
->charset('utf8mb4');

// configure these for your database server
$doctrine->dbal()
->connection('customer')
->url('%env(resolve:DATABASE_CUSTOMER_URL)%')
->url(env('DATABASE_CUSTOMER_URL')->resolve())
->driver('pdo_mysql')
->serverVersion('5.7')
->charset('utf8mb4');
Expand Down
2 changes: 1 addition & 1 deletion lock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ this behavior by using the ``lock`` key like:
->resource('default', ['sqlsrv:server=127.0.0.1;Database=app'])
->resource('default', ['oci:host=127.0.0.1;dbname=app'])
->resource('default', ['mongodb://127.0.0.1/app?collection=lock'])
->resource('default', ['%env(LOCK_DSN)%'])
->resource('default', [env('LOCK_DSN')])

// named locks
->resource('invoice', ['semaphore', 'redis://r2.docker'])
Expand Down
8 changes: 4 additions & 4 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ over SMTP by configuring the DSN in your ``.env`` file (the ``user``,
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('framework', [
'mailer' => [
'dsn' => '%env(MAILER_DSN)%',
'dsn' => env('MAILER_DSN'),
],
]);
};
Expand Down Expand Up @@ -1170,8 +1170,8 @@ This can be configured by replacing the ``dsn`` configuration entry with a

return static function (FrameworkConfig $framework) {
$framework->mailer()
->transport('main', '%env(MAILER_DSN)%')
->transport('alternative', '%env(MAILER_DSN_IMPORTANT)%')
->transport('main', env('MAILER_DSN'))
->transport('alternative', env('MAILER_DSN_IMPORTANT'))
;
};

Expand Down Expand Up @@ -1243,7 +1243,7 @@ you have a transport called ``async``, you can route the message there:

return static function (FrameworkConfig $framework) {
$framework->messenger()
->transport('async')->dsn('%env(MESSENGER_TRANSPORT_DSN)%');
->transport('async')->dsn(env('MESSENGER_TRANSPORT_DSN'));

$framework->messenger()
->routing('Symfony\Component\Mailer\Messenger\SendEmailMessage')
Expand Down
14 changes: 7 additions & 7 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ that uses this configuration:
return static function (FrameworkConfig $framework) {
$framework->messenger()
->transport('async')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
;

$framework->messenger()
->transport('async')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
->options([])
;
};
Expand Down Expand Up @@ -593,11 +593,11 @@ different messages to them. For example:
$messenger = $framework->messenger();

$messenger->transport('async_priority_high')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
->options(['queue_name' => 'high']);

$messenger->transport('async_priority_low')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
->options(['queue_name' => 'low']);

$messenger->routing('App\Message\SmsNotification')->senders(['async_priority_low']);
Expand Down Expand Up @@ -847,7 +847,7 @@ this is configurable for each transport:
$messenger = $framework->messenger();

$messenger->transport('async_priority_high')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
// default configuration
->retryStrategy()
->maxRetries(3)
Expand Down Expand Up @@ -1063,7 +1063,7 @@ override the failure transport for only specific transports:
$messenger->failureTransport('failed_default');

$messenger->transport('async_priority_high')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
->failureTransport('failed_high_priority');

// since no failed transport is configured, the one used will be
Expand Down Expand Up @@ -1151,7 +1151,7 @@ options. Options can be passed to the transport via a DSN string or configuratio
$messenger = $framework->messenger();

$messenger->transport('my_transport')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->dsn(env('MESSENGER_TRANSPORT_DSN'))
->options(['auto_setup' => false]);
};

Expand Down
12 changes: 6 additions & 6 deletions notifier.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ configure the ``texter_transports``:

return static function (FrameworkConfig $framework) {
$framework->notifier()
->texterTransport('twilio', '%env(TWILIO_DSN)%')
->texterTransport('twilio', env('TWILIO_DSN'))
;
};

Expand Down Expand Up @@ -255,7 +255,7 @@ Chatters are configured using the ``chatter_transports`` setting:

return static function (FrameworkConfig $framework) {
$framework->notifier()
->chatterTransport('slack', '%env(SLACK_DSN)%')
->chatterTransport('slack', env('SLACK_DSN'))
;
};

Expand Down Expand Up @@ -319,7 +319,7 @@ notification emails:

return static function (FrameworkConfig $framework) {
$framework->mailer()
->dsn('%env(MAILER_DSN)%')
->dsn(env('MAILER_DSN'))
->envelope()
->sender('notifications@example.com')
;
Expand Down Expand Up @@ -390,7 +390,7 @@ configure the ``texter_transports``:

return static function (FrameworkConfig $framework) {
$framework->notifier()
->texterTransport('expo', '%env(EXPO_DSN)%')
->texterTransport('expo', env('EXPO_DSN'))
;
};

Expand Down Expand Up @@ -454,10 +454,10 @@ transport:
$framework->notifier()
// Send notifications to Slack and use Telegram if
// Slack errored
->chatterTransport('main', '%env(SLACK_DSN)% || %env(TELEGRAM_DSN)%')
->chatterTransport('main', env('SLACK_DSN').' || '.env('TELEGRAM_DSN'))

// Send notifications to the next scheduled transport calculated by round robin
->chatterTransport('roundrobin', '%env(SLACK_DSN)% && %env(TELEGRAM_DSN)%')
->chatterTransport('roundrobin', env('SLACK_DSN').' && '.env('TELEGRAM_DSN'))
;
};

Expand Down
14 changes: 7 additions & 7 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ some environment variable that stores the name of the IDE/editor:

return static function (FrameworkConfig $framework) {
// the env var stores the IDE/editor name (e.g. 'phpstorm', 'vscode', etc.)
$framework->ide('%env(resolve:CODE_EDITOR)%');
$framework->ide(env('CODE_EDITOR')->resolve());
};

.. versionadded:: 5.3
Expand Down Expand Up @@ -596,14 +596,14 @@ can also :ref:`disable CSRF protection on individual forms <form-csrf-customizat
.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
framework:
# ...
csrf_protection: true

.. code-block:: xml

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
Expand All @@ -617,9 +617,9 @@ can also :ref:`disable CSRF protection on individual forms <form-csrf-customizat
<framework:csrf-protection enabled="true"/>
</framework:config>
</container>

.. code-block:: php

// config/packages/framework.php
use Symfony\Config\FrameworkConfig;
return static function (FrameworkConfig $framework) {
Expand Down Expand Up @@ -3210,7 +3210,7 @@ A list of lock stores to be created by the framework extension.

return static function (FrameworkConfig $framework) {
$framework->lock()
->resource('default', ['%env(LOCK_DSN)%']);
->resource('default', [env('LOCK_DSN')]);
};

.. seealso::
Expand Down
4 changes: 2 additions & 2 deletions security/access_control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ Take the following ``access_control`` entries as an example:
$security->accessControl()
->path('^/admin')
->roles(['ROLE_USER_IP'])
->ips(['%env(TRUSTED_IPS)%'])
->ips([env('TRUSTED_IPS')])
;
$security->accessControl()
->path('^/admin')
->roles(['ROLE_USER_IP'])
->ips(['127.0.0.1', '::1', '%env(TRUSTED_IPS)%'])
->ips(['127.0.0.1', '::1', env('TRUSTED_IPS')])
;
};

Expand Down
4 changes: 2 additions & 2 deletions session/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ first register a new handler service with your database credentials:

$services->set(PdoSessionHandler::class)
->args([
'%env(DATABASE_URL)%',
env('DATABASE_URL'),
// you can also use PDO configuration, but requires passing two arguments:
// 'mysql:dbname=mydatabase; host=myhost; port=myport',
// ['db_username' => 'myuser', 'db_password' => 'mypassword'],
Expand Down Expand Up @@ -334,7 +334,7 @@ passed to the ``PdoSessionHandler`` service:

$services->set(PdoSessionHandler::class)
->args([
'%env(DATABASE_URL)%',
env('DATABASE_URL'),
['db_table' => 'customer_session', 'db_id_col' => 'guid'],
])
;
Expand Down
2 changes: 1 addition & 1 deletion translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ configure the ``providers`` option:
'translator' => [
'providers' => [
'loco' => [
'dsn' => '%env(LOCO_DSN)%',
'dsn' => env('LOCO_DSN'),
'domains' => ['messages'],
'locales' => ['en', 'fr'],
],
Expand Down