symfony2 - Making a dependency configurable at runtime in Symfony 2? -
in reusable bundle, can specify connection name (doctrine dbal connection):
# config.yml my_bundle: connection: ~ # or "default", or "my_connection"
in extension, i'm aliasing it:
// mybundleextension.php $container->setalias( 'my_bundle.connection', 'doctrine.dbal.'.$config['connection'].'_connection' );
and injecting needed (4-5 services in bundle). works fine.
unfortunatly, turns out connection should (may) changed @ runtime, i.e. after user been authenticated (i.e http basic authentication). when username foo
, utilize foo_database
, when bar
bar_database
.
my solution (workaround):
right i'm changing using event system: when in bundle uses connection
object, emit event, i.e mybundle::bar
event. listener can alter connection using setconnection(connection $connection)
. in bundle, utilize updated connection calling getconnection()
.
this solution forces me hear every event need alter connection. if forgot hear mybundle::foo
event? application not work expected , bugs hard understand , track.
is there way solve problem?
symfony2 dependency-injection doctrine2 inversion-of-control dbal
No comments:
Post a Comment