Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameter dependency for a service

Tags:

symfony

I know that i is possible to add optional service dependency for a service. The syntax is

arguments: [@?my_mailer]

But how do i add optional parameter dependency for a service?

arguments: [%my_parameter%]

I tried

arguments: [%?my_parameter%]
arguments: [?%my_parameter%]

But neither of them work, is this feature implemented in sf2?

like image 278
Vlad Avatar asked Sep 25 '12 10:09

Vlad


People also ask

How do you specify an optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.

Are optional parameters bad practice?

The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.

How do you make a function parameter optional?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

What is the use of @optional in angular?

Angular has an @Optional decorator, which when applied to a constructor argument instructs the Angular injector to inject null if the dependency is not found.

What if my service does not have an optional dependency?

Sometimes, one of your services may have an optional dependency, meaning that the dependency is not required for your service to work properly. You can configure the container to not throw an error in this case. You can use the null strategy to explicitly set the argument to null if the service does not exist:

When to make some of the dependencies injected optional?

There are quite a few use cases where it's needed to make some of the dependencies that are injected optional. Here are some example use cases: Provide a default implementation whenever a required infrastructure dependency is not provided, such as DataSource s.

How to define the optional parameters of a method?

The optional parameters are always defined at the end of the parameter list. Or in other words, the last parameter of the method, constructor, etc. is the optional parameter.

How do I remove all dependencies from a service?

To remove all dependencies use the following command. Open regedit and locate the following key. There will be a subkey listed for each installed service, click the subkey for the service you wish to configure. Click Edit, and New Multi-String Value.


1 Answers

From Symfony 2.4 you can use expression for this:

arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]

More at http://symfony.com/doc/current/book/service_container.html#using-the-expression-language

like image 138
ewgra Avatar answered Oct 02 '22 14:10

ewgra