Skip to content

Commit 9f19ddc

Browse files
committed
[DependencyInjection] Add urlencode function to EnvVarProcessor
1 parent 63d6d4f commit 9f19ddc

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
@@ -824,6 +824,56 @@ Symfony provides the following env var processors:
824824
825825
The ``env(defined:...)`` env var processor was introduced in Symfony 6.4.
826826
827+
.. _urlencode_environment_variable_processor:
828+
829+
``env(urlencode:FOO)``
830+
Urlencode the content of ``FOO`` env var. This is especially useful when
831+
``FOO`` value is not compatible with DSN syntax.
832+
833+
.. configuration-block::
834+
835+
.. code-block:: yaml
836+
837+
# config/packages/framework.yaml
838+
parameters:
839+
env(DATABASE_URL): 'mysql://db_user:foo@b$r@127.0.0.1:3306/db_name'
840+
encoded_database_url: '%env(urlencode:DATABASE_URL)%'
841+
842+
.. code-block:: xml
843+
844+
<!-- config/packages/framework.xml -->
845+
<?xml version="1.0" encoding="UTF-8" ?>
846+
<container xmlns="http://symfony.com/schema/dic/services"
847+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
848+
xmlns:framework="http://symfony.com/schema/dic/symfony"
849+
xsi:schemaLocation="http://symfony.com/schema/dic/services
850+
https://symfony.com/schema/dic/services/services-1.0.xsd
851+
http://symfony.com/schema/dic/symfony
852+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
853+
854+
<parameters>
855+
<parameter key="env(DATABASE_URL)">mysql://db_user:foo@b$r@127.0.0.1:3306/db_name</parameter>
856+
<parameter key="encoded_database_url">%env(urlencode:DATABASE_URL)%</parameter>
857+
</parameters>
858+
</container>
859+
860+
.. code-block:: php
861+
862+
// config/packages/framework.php
863+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
864+
865+
use Symfony\Component\DependencyInjection\ContainerBuilder;
866+
use Symfony\Config\FrameworkConfig;
867+
868+
return static function (ContainerBuilder $container): void {
869+
$container->setParameter('env(DATABASE_URL)', 'mysql://db_user:foo@b$r@127.0.0.1:3306/db_name');
870+
$container->setParameter('encoded_database_url', '%env(urlencode:DATABASE_URL)%');
871+
};
872+
873+
.. versionadded:: 6.4
874+
875+
The ``env(urlencode:...)`` env var processor was introduced in Symfony 6.4.
876+
827877
It is also possible to combine any number of processors:
828878
829879
.. 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)