mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-11 03:53:13 +00:00
64 lines
1.4 KiB
PHP
64 lines
1.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Phraseanet
|
|
*
|
|
* (c) 2005-2014 Alchemy
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Alchemy\Phrasea\Core\Connection;
|
|
|
|
use Doctrine\Common\EventManager;
|
|
use Doctrine\DBAL\Configuration;
|
|
use Doctrine\DBAL\Connection;
|
|
use Doctrine\DBAL\DriverManager;
|
|
|
|
class ConnectionProvider
|
|
{
|
|
private $config;
|
|
/**
|
|
* @var Connection[]
|
|
*/
|
|
private $connections = [];
|
|
private $eventManager;
|
|
|
|
public function __construct(Configuration $config, EventManager $eventManager)
|
|
{
|
|
$this->config = $config;
|
|
$this->eventManager = $eventManager;
|
|
}
|
|
|
|
public function __destruct()
|
|
{
|
|
foreach ($this->connections as $conn) {
|
|
$conn->close();
|
|
}
|
|
|
|
$this->connections = [];
|
|
}
|
|
|
|
/**
|
|
* @param $params
|
|
*
|
|
* @return Connection
|
|
*/
|
|
public function get(array $params)
|
|
{
|
|
$params = array_replace([
|
|
'driver' => 'pdo_mysql',
|
|
'charset' => 'UTF8',
|
|
], $params);
|
|
|
|
$key = md5(serialize($params));
|
|
|
|
if (isset($this->connections[$key])) {
|
|
return $this->connections[$key];
|
|
}
|
|
|
|
return $this->connections[$key] = DriverManager::getConnection($params, $this->config, $this->eventManager);;
|
|
}
|
|
}
|