Skip to content

[DependencyInjection] Document the date_time and date_time_format env var processors #18835

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

Closed
wants to merge 3 commits into from
Closed
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
104 changes: 104 additions & 0 deletions configuration/env_var_processors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,110 @@ Symfony provides the following env var processors:
``env(float:FOO)``
Casts ``FOO`` to a float.

``env(date_time:FOO)``
Copy link
Contributor

@94noni 94noni Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event if other example uses FOO, what about here use a real date_time value format ?
I mean is it the Y-m-d H:i:s ?
obviously, for env(int:FOO) we know int refers to what, but date time is more ambiguous and doc must be straightfroward
wdyt?

also, you must add a version added directive to document that such new processor have been added on sf v6.4 👍🏻

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation updated to the latest implementation. I have added the "version added directives" and the usage examples to describe how to use the both processors. I think that the examples are sufficient and the FOO can stay in processor name to make the documentation consistent.

Casts ``FOO`` to a \DateTimeImmutable class instance. ``FOO`` can be any valid \DateTimeImmutable constructor argument like ``2023-09-15 12:34:56`` or ``now``.

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
parameters:
env(DATE_TIME): '2023-09-15 12:34:56'
date_time_immutable_object: '%env(date_time:DATE_TIME)%'

.. code-block:: xml

<!-- config/packages/framework.yaml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://symfony.com/schema/dic/security"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/security
https://symfony.com/schema/dic/security/security-1.0.xsd">

<parameters>
<parameter key="env(DATE_TIME)">2023-09-15 12:34:56</parameter>
<parameter key="date_time_immutable_object">%env(date_time:DATE_TIME)%</parameter>
</parameters>
</container>

.. code-block:: php

// config/packages/framework.php
use Symfony\Component\DependencyInjection\ContainerBuilder;

return static function (ContainerBuilder $container): void {
$container->setParameter('env(DATE_TIME)', '2023-09-15 12:34:56');
$container->setParameter('date_time_immutable_object', '%env(date_time:DATE_TIME)%');
};

.. versionadded:: 6.4

The ``env(date_time:...)`` env var processor was introduced in Symfony 6.4.

``env(date_time_format:FOO:BAR)``
Casts ``BAR`` to formatted date time string using format defined in parameter ``FOO``.
This processor should be used in conjunction with ``env(date_time:FOO)``.

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
parameters:
env(DATE_TIME): 'now'

date_time_format: 'Y-m-d H:i:s'
formatted_date_time_string: '%env(date_time_format:date_time_format:date_time:DATE_TIME)%'

current_date_format: 'Y-m-d'
today_dir_path: '/path/to/today/dir/%env(date_time_format:current_date_format:date_time:DATE_TIME)%'

.. code-block:: xml

<!-- config/packages/framework.yaml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://symfony.com/schema/dic/security"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/security
https://symfony.com/schema/dic/security/security-1.0.xsd">

<parameters>
<parameter key="env(DATE_TIME)">now</parameter>

<parameter key="date_time_format">Y-m-d H:i:s</parameter>
<parameter key="formatted_date_time_string">%env(date_time_format:date_time_format:date_time:DATE_TIME)%</parameter>

<parameter key="current_date_format">Y-m-d</parameter>
<parameter key="today_dir_path">/path/to/today/dir/%env(date_time_format:current_date_format:date_time:DATE_TIME)%</parameter>
</parameters>
</container>

.. code-block:: php

// config/packages/framework.php
use Symfony\Component\DependencyInjection\ContainerBuilder;

return static function (ContainerBuilder $container): void {
$container->setParameter('env(DATE_TIME)', 'now');

$container->setParameter('date_time_format', 'Y-m-d H:i:s');
$container->setParameter('formatted_date_time_string', '%env(date_time_format:date_time_format:date_time:DATE_TIME)%');

$container->setParameter('current_date_format', 'Y-m-d');
$container->setParameter('today_dir_path', '/path/to/today/dir/%env(date_time_format:current_date_format:date_time:DATE_TIME)%');
};

.. versionadded:: 6.4

The ``env(date_time_format:...)`` env var processor was introduced in Symfony 6.4.

``env(const:FOO)``
Finds the const value named in ``FOO``:

Expand Down