symfony - Avoid duplicated services in dependecy injection -


i need advices avoid duplicating services in symfony dependecy injection project.

the project goal create symfony console commands manage objects of remote service. service exposes api manage objects.

the remote service gives me 2 different instance work with: production instance , sandbox instance.

examples of commands execute are:

  • create user on instance x
  • create group on instance x
  • show difference between users of 2 instances
  • sync users between 2 instances

i started creating class wrap service api , creating 2 services using same class different authentication data 2 instances:

services:      sandbox_instance:         class: apiclient         arguments: ["sandbox_account", "sandbox_user", "sandbox_password"]      prod_instance:         class: apiclient         arguments: ["prod_account", "prod_user", "prod_password"] 

but then, created "managers" have logic of different operations. example, created usermanager , groupmanager. these services depends on "apiclient" services, ended duplicating managers, 1 production, 1 sandbox:

services:    ....    user_manager_sandbox:       class: usermanager       arguments: ['@sandbox_instance']    user_manager_prod:       class: usermanager       arguments: ['@prod_instance']     group_manager_sandbox:       class: groupmanager       arguments: ['@sandbox_instance']    group_manager_prod:       class: groupmanager       arguments: ['@prod_instance'] 

i'm sure not correct way use service container, can't figure out how define , "parametric" services.

my goal, in command code call:

$produsermanager = $container->get('user_manager', 'prod'); $sandboxusermanager = $container->get('user_manager', 'sandbox'); 

and avoid duplicating definition of service.

of course cannot put authentication data in config parameters , load different configurations according instance want work because there commands need work both instances (migration , diff commands, example).

how can in correct way? best practice?

thank in advance. cheers matteo

i ended using service switch environment , using expression language in service definition:

environment switch class:

<?php  class envswitch {    private $currentenv;    public function getcurrentenv(){     if($this->currentenv == null)       throw new \exception('no environment selected yet.');     return $this->currentenv;   }    public function setcurrentenv(string $env){     $this->currentenv = $env;   }    public function getcurrentenvservicename(){     return $this->getcurrentenv().'_instance';   }  } 

the services definition:

services:      sandbox_instance:         class: apiclient         arguments: ["sandbox_account", "sandbox_user", "sandbox_password"]      prod_instance:         class: apiclient         arguments: ["prod_account", "prod_user", "prod_password"]       env_switch:          class: envswitch       user_manager:          class: usermanager          arguments: ["@=service(service('env_switch').getcurrentenvservicename())"]          shared: false       group_manager:          class: groupmanager          arguments: ["@=service(service('env_switch').getcurrentenvservicename())"]          shared: false 

usage in console command:

... $this->get('env_switch')->setcurrentenv('sandbox'); $usermanager = $this->get('user_manager'); ... 

what think solution? matteo


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -