From e0e8a746985057c61b8affc44dabb3684a8452f5 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sat, 14 May 2022 11:46:25 +0200 Subject: [PATCH] Use PHP-DSL `env()` configurator when possible --- configuration.rst | 8 ++++++++ configuration/env_var_processors.rst | 10 +++++++++- configuration/secrets.rst | 2 +- doctrine/multiple_entity_managers.rst | 4 ++-- lock.rst | 2 +- mailer.rst | 8 ++++---- messenger.rst | 14 +++++++------- notifier.rst | 12 ++++++------ reference/configuration/framework.rst | 14 +++++++------- security/access_control.rst | 4 ++-- session/database.rst | 4 ++-- translation.rst | 2 +- 12 files changed, 50 insertions(+), 34 deletions(-) diff --git a/configuration.rst b/configuration.rst index e74eaa8b862..621cbc27094 100644 --- a/configuration.rst +++ b/configuration.rst @@ -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 diff --git a/configuration/env_var_processors.rst b/configuration/env_var_processors.rst index 2e73b823da4..4d2615fc3b6 100644 --- a/configuration/env_var_processors.rst +++ b/configuration/env_var_processors.rst @@ -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 ---------------------------------------- @@ -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)`` diff --git a/configuration/secrets.rst b/configuration/secrets.rst index 950f68528a3..5783ca9d918 100644 --- a/configuration/secrets.rst +++ b/configuration/secrets.rst @@ -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')) ; }; diff --git a/doctrine/multiple_entity_managers.rst b/doctrine/multiple_entity_managers.rst index e94ef907f57..856e796a2e9 100644 --- a/doctrine/multiple_entity_managers.rst +++ b/doctrine/multiple_entity_managers.rst @@ -136,7 +136,7 @@ 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'); @@ -144,7 +144,7 @@ The following configuration code shows how you can configure two entity managers // 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'); diff --git a/lock.rst b/lock.rst index 9fb207b927f..5d864fb0089 100644 --- a/lock.rst +++ b/lock.rst @@ -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']) diff --git a/mailer.rst b/mailer.rst index b79a61bb5cb..07fe907ab9a 100644 --- a/mailer.rst +++ b/mailer.rst @@ -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'), ], ]); }; @@ -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')) ; }; @@ -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') diff --git a/messenger.rst b/messenger.rst index 016274aec79..4e1dae322de 100644 --- a/messenger.rst +++ b/messenger.rst @@ -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([]) ; }; @@ -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']); @@ -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) @@ -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 @@ -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]); }; diff --git a/notifier.rst b/notifier.rst index 786614e2ecd..79b4480a096 100644 --- a/notifier.rst +++ b/notifier.rst @@ -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')) ; }; @@ -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')) ; }; @@ -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') ; @@ -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')) ; }; @@ -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')) ; }; diff --git a/reference/configuration/framework.rst b/reference/configuration/framework.rst index ce8f8291db2..d5ee18b0e1a 100644 --- a/reference/configuration/framework.rst +++ b/reference/configuration/framework.rst @@ -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 @@ -596,14 +596,14 @@ can also :ref:`disable CSRF protection on individual forms - + .. code-block:: php - + // config/packages/framework.php use Symfony\Config\FrameworkConfig; return static function (FrameworkConfig $framework) { @@ -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:: diff --git a/security/access_control.rst b/security/access_control.rst index 57c70fce5df..df9536fef2c 100644 --- a/security/access_control.rst +++ b/security/access_control.rst @@ -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')]) ; }; diff --git a/session/database.rst b/session/database.rst index 16715c2b150..050eebb6a9d 100644 --- a/session/database.rst +++ b/session/database.rst @@ -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'], @@ -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'], ]) ; diff --git a/translation.rst b/translation.rst index 6c18cf3e6f0..dc5288a09f0 100644 --- a/translation.rst +++ b/translation.rst @@ -714,7 +714,7 @@ configure the ``providers`` option: 'translator' => [ 'providers' => [ 'loco' => [ - 'dsn' => '%env(LOCO_DSN)%', + 'dsn' => env('LOCO_DSN'), 'domains' => ['messages'], 'locales' => ['en', 'fr'], ],