Skip to content

Commit 2ff5b99

Browse files
committed
minor #17750 [DependencyInjection] Enable deprecating parameters (alexandre-daubois)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Enable deprecating parameters #17741 has been closed involuntarily I guess but an unrelated merge 😄 But it's OK as this new iteration is totally different from the other one. Pretty makes sense to have a new PR! I took into account `@HeahDude`'s remarks (#17741 (review)). I think it's better know. Please let me know if you see something else to update! Commits ------- 978d0e8 [DependencyInjection] Enable deprecating parameters
2 parents 7eea193 + 978d0e8 commit 2ff5b99

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

components/dependency_injection/compilation.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,37 @@ file but also load a secondary one only if a certain parameter is set::
254254
}
255255
}
256256

257+
You are also able to deprecate container parameters in your extension to warn
258+
to not use a specific parameter anymore. This is particularly useful to help
259+
developers with the migration process across major versions of an extension.
260+
The deprecation can be done thanks to the ``ContainerBuilder::deprecateParameter``
261+
method. This can only be achieved when configuring an extension with PHP, as this is
262+
not possible to do it with other configuration formats (YAML, XML). Let's define a
263+
parameter in our extension that will be declared as deprecated::
264+
265+
public function load(array $configs, ContainerBuilder $containerBuilder)
266+
{
267+
// ...
268+
269+
$containerBuilder->setParameter('acme_demo.database_user', $configs['db_user']);
270+
271+
$containerBuilder->deprecateParameter(
272+
'acme_demo.database_user',
273+
'acme/database-package',
274+
'1.3',
275+
// optionally you can set a custom deprecation message
276+
'"acme_demo.database_user" is deprecated, you should configure database credentials with the "acme_demo.database_dsn" parameter instead.'
277+
);
278+
}
279+
280+
The parameter being deprecated must be set before being declared as deprecated. Otherwise
281+
a :class:`Symfony\\Component\\DependencyInjection\\Exception\\ParameterNotFoundException` exception
282+
will be thrown.
283+
284+
.. versionadded:: 6.3
285+
286+
The ``ContainerBuilder::deprecateParameter`` method was introduced in Symfony 6.3.
287+
257288
.. note::
258289

259290
Just registering an extension with the container is not enough to get
@@ -478,7 +509,7 @@ serves at dumping the compiled container::
478509
the :ref:`dumpFile() method <filesystem-dumpfile>` from Symfony Filesystem
479510
component or other methods provided by Symfony (e.g. ``$containerConfigCache->write()``)
480511
which are atomic.
481-
512+
482513
``ProjectServiceContainer`` is the default name given to the dumped container
483514
class. However, you can change this with the ``class`` option when you
484515
dump it::

0 commit comments

Comments
 (0)