symfony - Method return value as service parameter -
i'm trying use method-return-value di-parameter. concrete use-case have service has logic of calculating users-country-code like:
$user->getcountrycode();
on other hand have many services depend on country like:
$foo = new foo('uk'); $bar = new bar('usa');
i know can inject user service foo / bar, creates odd dependency these services don't need user need country-code.
is there way me write service-description along lines of:
my_services.foo: class: foo arguments: - @user.getcountrycode
edit: i'm using symfony 2.4 yml configuration.
service factory method (compatible symfony2 versions)
service definitions:
# services.yml foofactory: class: foo\foobundle\service\foofactory arguments: [@user_country_code_calc_service] fooservice: class: foo\foobundle\service\fooservice factory_service: foofactory factory_method: create
and factory:
// foofactory.php class foofactory { private $countrycodecalculator; public function __construct(usercountrycodecalculator $countrycodecalculator) { $this->countrycodecalculator = $countrycodecalculator; } public function create() { $countrycode = $this->countrycodecalculator->calculate(...); return new fooservice($countrycode); } }
now when inject @foofactory
in of services, symfony call foofactory::create
, inject method's result it.
Comments
Post a Comment