Skip to content

Commit 4eb2f25

Browse files
committed
[DependencyInjection] Add urlencode function to EnvVarProcessor
1 parent 188994b commit 4eb2f25

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

configuration/env_var_processors.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,56 @@ Symfony provides the following env var processors:
812812
// config/services.php
813813
$container->setParameter('typed_env', '%env(defined:FOO)%');
814814
815+
.. _urlencode_environment_variable_processor:
816+
817+
``env(urlencode:FOO)``
818+
Urlencode the content of ``FOO`` env var. This is especially useful when
819+
``FOO`` value is not compatible with DSN syntax.
820+
821+
.. configuration-block::
822+
823+
.. code-block:: yaml
824+
825+
# config/packages/framework.yaml
826+
parameters:
827+
env(DATABASE_URL): 'mysql://db_user:foo@b$r@127.0.0.1:3306/db_name'
828+
encoded_database_url: '%env(urlencode:DATABASE_URL)%'
829+
830+
.. code-block:: xml
831+
832+
<!-- config/packages/framework.xml -->
833+
<?xml version="1.0" encoding="UTF-8" ?>
834+
<container xmlns="http://symfony.com/schema/dic/services"
835+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
836+
xmlns:framework="http://symfony.com/schema/dic/symfony"
837+
xsi:schemaLocation="http://symfony.com/schema/dic/services
838+
https://symfony.com/schema/dic/services/services-1.0.xsd
839+
http://symfony.com/schema/dic/symfony
840+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
841+
842+
<parameters>
843+
<parameter key="env(DATABASE_URL)">mysql://db_user:foo@b$r@127.0.0.1:3306/db_name</parameter>
844+
<parameter key="encoded_database_url">%env(urlencode:DATABASE_URL)%</parameter>
845+
</parameters>
846+
</container>
847+
848+
.. code-block:: php
849+
850+
// config/packages/framework.php
851+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
852+
853+
use Symfony\Component\DependencyInjection\ContainerBuilder;
854+
use Symfony\Config\FrameworkConfig;
855+
856+
return static function (ContainerBuilder $container): void {
857+
$container->setParameter('env(DATABASE_URL)', 'mysql://db_user:foo@b$r@127.0.0.1:3306/db_name');
858+
$container->setParameter('encoded_database_url', '%env(urlencode:DATABASE_URL)%');
859+
};
860+
861+
.. versionadded:: 7.1
862+
863+
The ``env(urlencode:...)`` env var processor was introduced in Symfony 7.1.
864+
815865
It is also possible to combine any number of processors:
816866
817867
.. configuration-block::

doctrine.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ The database connection information is stored as an environment variable called
5959

6060
If the username, password, host or database name contain any character considered
6161
special in a URI (such as ``+``, ``@``, ``$``, ``#``, ``/``, ``:``, ``*``, ``!``, ``%``),
62-
you must encode them. See `RFC 3986`_ for the full list of reserved characters or
63-
use the :phpfunction:`urlencode` function to encode them. In this case you need to
64-
remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml`` to avoid errors:
65-
``url: '%env(DATABASE_URL)%'``
62+
you must encode them. See `RFC 3986`_ for the full list of reserved characters.
63+
You can use the :phpfunction:`urlencode` function to encode them or
64+
the :ref:`urlencode environment variable processor <urlencode_environment_variable_processor>`.
65+
In this case you need to remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml``
66+
to avoid errors: ``url: '%env(DATABASE_URL)%'``
6667

6768
Now that your connection parameters are setup, Doctrine can create the ``db_name``
6869
database for you:

0 commit comments

Comments
 (0)