多くのサイトをホストする Symfony2 サイトに取り組んでおり、各サイトには独自のデータベースがあります。リフレクションを使用して「クライアント」エンティティ マネージャーの接続パラメーター (ユーザー名、パスワード、データベース名) を変更するサービスを実装しました。FOSUserBundle がその認証サービスを呼び出す前に、このサービスをトリガーする方法がわかりません。Symfony2 Request イベントリスナーを作成しようとしましたが、うまくいかないようです:
class RequestListener {
private $clientSiteContext;
function __construct($clientSiteContext) {
$this->clientSiteContext = $clientSiteContext;
}
public function onKernelRequest(GetResponseEvent $event) {
if ($event->getRequestType() == HttpKernel::MASTER_REQUEST) {
$this->clientSiteContext->resetClientEntityManager();
}
}
}
resetClientEntityManager() の実装
public function resetClientEntityManager() {
/** @var $doctrine \Doctrine\Bundle\DoctrineBundle\Registry */
$doctrine = $this->container->get('doctrine');
$dbConfig = $this->getConnectionParams();
$dbalServiceName = sprintf('doctrine.dbal.%s_connection', 'client');
$clientEmName = 'client';
$connection = $this->container->get($dbalServiceName);
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public');
$params = $refParams->getValue($connection);
$params['dbname'] = $dbConfig['dbname'];
$params['user'] = $dbConfig['user'];
$params['host'] = $dbConfig['host'];
$params['password'] = $dbConfig['password'];
$params['driver'] = $dbConfig['driver'];
$params['charset'] = 'UTF8';
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$doctrine->resetEntityManager($clientEmName);
}
このリスナーがページ要求ごとに 1 回呼び出され、FOSUserBundle が使用するエンティティ マネージャーに影響を与える方法を教えてもらえますか?