1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
/*
|
4
|
|
|
* This file is part of the "RocketORM" package.
|
5
|
|
|
*
|
6
|
|
|
* https://github.com/RocketORM/ORM
|
7
|
|
|
*
|
8
|
|
|
* For the full license information, please view the LICENSE
|
9
|
|
|
* file that was distributed with this source code.
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Connection;
|
13
|
|
|
|
14
|
|
|
use Rocket\ORM\Connection\Exception\ConnectionModeException;
|
15
|
|
|
use Rocket\ORM\Connection\Exception\ConnectionNotFoundException;
|
16
|
|
|
use Rocket\ORM\Rocket;
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* @author Sylvain Lorinet <[email protected]>
|
20
|
|
|
*/
|
21
|
|
|
class ConnectionFactory
|
22
|
|
|
{
|
23
|
|
|
const WRAPPER_DEFAULT_CLASS = '\\Rocket\\ORM\\Connection\\PDO\\PDO';
|
24
|
|
|
const WRAPPER_DEFAULT_SQLITE_CLASS = '\\Rocket\\ORM\\Connection\\PDO\\SQLitePDO';
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* @param array $config
|
28
|
|
|
* @param string $name
|
29
|
|
|
* @param int $mode
|
30
|
|
|
*
|
31
|
|
|
* @return ConnectionInterface
|
32
|
|
|
*
|
33
|
|
|
* @throws ConnectionNotFoundException
|
34
|
|
|
* @throws ConnectionModeException
|
35
|
|
|
*/
|
36
|
3 |
|
public static function create(array $config, $name, $mode)
|
37
|
|
|
{
|
38
|
3 |
|
if (!isset($config['connections'][$name])) {
|
39
|
1 |
|
throw new ConnectionNotFoundException('The connection with name "' . $name . '" is not found in the configuration');
|
40
|
2 |
|
} elseif (isset($config['connections'][$name]['mode']) && null != $config['connections'][$name]['mode'] && $mode != $config['connections'][$name]) {
|
41
|
1 |
|
throw new ConnectionModeException(
|
42
|
1 |
|
'Trying to use connection named "' . $name . '" with the mode "'
|
43
|
1 |
|
. $mode . '", but got "' . $config['connections'][$name]['mode'] . '"'
|
44
|
1 |
|
);
|
45
|
|
|
}
|
46
|
|
|
|
47
|
1 |
|
$class = self::getClassNamespace($config, $name);
|
48
|
1 |
|
$connection = $class::create($config['connections'][$name]['params']);
|
49
|
|
|
|
50
|
1 |
|
return $connection;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* @param array $config
|
55
|
|
|
* @param string $name
|
56
|
|
|
*
|
57
|
|
|
* @return string
|
58
|
|
|
*/
|
59
|
2 |
|
public static function getClassNamespace(array $config, $name)
|
60
|
|
|
{
|
61
|
2 |
|
if (isset($config['connections'][$name]['class']) && null != $config['connections'][$name]['class']) {
|
62
|
1 |
|
$class = $config['connections'][$name]['class'];
|
63
|
2 |
|
} elseif (isset($config['connection_class']) && null != $config['connection_class']) {
|
64
|
1 |
|
$class = $config['connection_class'];
|
65
|
1 |
|
} else {
|
66
|
2 |
|
$class = self::WRAPPER_DEFAULT_CLASS;
|
67
|
2 |
|
$dsn = $config['connections'][$name]['params']['dsn'];
|
68
|
2 |
|
$driver = substr($dsn, 0, strpos($dsn, ':'));
|
69
|
|
|
|
70
|
2 |
|
if ('sqlite' == $driver) {
|
71
|
2 |
|
$class = self::WRAPPER_DEFAULT_SQLITE_CLASS;
|
72
|
2 |
|
}
|
73
|
|
|
}
|
74
|
|
|
|
75
|
2 |
|
return $class;
|
76
|
|
|
}
|
77
|
|
|
}
|
78
|
|
|
|