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;
|
13
|
|
|
|
14
|
|
|
use Rocket\ORM\Config\Exception\ConfigurationKeyNotFoundException;
|
15
|
|
|
use Rocket\ORM\Connection\ConnectionFactory;
|
16
|
|
|
use Rocket\ORM\Connection\ConnectionInterface;
|
17
|
|
|
use Rocket\ORM\Model\Map\TableMapInterface;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @author Sylvain Lorinet <[email protected]>
|
21
|
|
|
*/
|
22
|
|
|
class Rocket
|
23
|
|
|
{
|
24
|
|
|
const VERSION = '0.1.0';
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* @var array
|
29
|
|
|
*/
|
30
|
|
|
protected static $config;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* @var array
|
34
|
|
|
*/
|
35
|
|
|
protected static $configCache = [];
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @var array
|
39
|
|
|
*/
|
40
|
|
|
protected static $cons = [];
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* @var array
|
44
|
|
|
*/
|
45
|
|
|
protected static $tableMaps = [];
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* @param string $name The connection name
|
|
|
|
|
50
|
|
|
* @param string $mode The connection mode, a Rocket class constant
|
51
|
|
|
*
|
52
|
|
|
* @return ConnectionInterface|\PDO
|
53
|
|
|
*
|
54
|
|
|
* @throws Connection\Exception\ConnectionNotFoundException
|
55
|
|
|
*/
|
56
|
1 |
|
public static function getConnection($name = null, $mode = ConnectionInterface::CONNECTION_MODE_WRITE)
|
57
|
|
|
{
|
58
|
1 |
|
if (null == $name) {
|
|
|
|
|
59
|
1 |
|
$name = self::getConfiguration('default_connection');
|
60
|
1 |
|
}
|
61
|
|
|
|
62
|
1 |
|
if (!isset(self::$cons[$name])) {
|
63
|
1 |
|
self::$cons[$name] = ConnectionFactory::create(self::$config, $name, $mode);
|
64
|
1 |
|
}
|
65
|
|
|
|
66
|
1 |
|
return self::$cons[$name];
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* @param null|string $name
|
71
|
|
|
*
|
72
|
|
|
* @return string
|
73
|
|
|
*/
|
74
|
1 |
|
public static function getConnectionDriver($name = null)
|
75
|
|
|
{
|
76
|
1 |
|
if (null == $name) {
|
|
|
|
|
77
|
1 |
|
$name = self::getConfiguration('default_connection');
|
78
|
1 |
|
}
|
79
|
|
|
|
80
|
|
|
/** @var ConnectionInterface $class */
|
81
|
1 |
|
$class = ConnectionFactory::getClassNamespace(self::$config, $name);
|
82
|
|
|
|
83
|
1 |
|
return $class::getDriver();
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
/**
|
87
|
|
|
* @param array $config
|
88
|
|
|
*/
|
89
|
7 |
|
public static function setConfiguration(array $config)
|
90
|
|
|
{
|
91
|
7 |
|
self::$config = $config;
|
92
|
7 |
|
self::$configCache = [];
|
93
|
7 |
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* @param string $key
|
97
|
|
|
*
|
98
|
|
|
* @return mixed
|
99
|
|
|
*
|
100
|
|
|
* @throws ConfigurationKeyNotFoundException
|
101
|
|
|
*/
|
102
|
4 |
|
public static function getConfiguration($key)
|
103
|
|
|
{
|
104
|
4 |
|
if (isset(self::$configCache[$key])) {
|
105
|
1 |
|
return self::$configCache[$key];
|
106
|
|
|
}
|
107
|
|
|
|
108
|
4 |
|
$parts = explode('.', $key);
|
109
|
4 |
|
$config = self::$config;
|
110
|
|
|
|
111
|
4 |
|
foreach ($parts as $part) {
|
112
|
4 |
|
if (!array_key_exists($part, $config)) {
|
113
|
1 |
|
throw new ConfigurationKeyNotFoundException('No configuration found for the key "' . $key . '"');
|
114
|
|
|
}
|
115
|
|
|
|
116
|
3 |
|
$config = $config[$part];
|
117
|
3 |
|
}
|
118
|
|
|
|
119
|
|
|
// Save to avoid next iteration
|
120
|
3 |
|
self::$configCache[$key] = $config;
|
121
|
|
|
|
122
|
3 |
|
return $config;
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
/**
|
126
|
|
|
* @param string $classNamespace
|
127
|
|
|
*
|
128
|
|
|
* @return TableMapInterface
|
129
|
|
|
*
|
130
|
|
|
* @throws \RuntimeException
|
131
|
|
|
*/
|
132
|
3 |
|
public static function getTableMap($classNamespace)
|
133
|
|
|
{
|
134
|
3 |
|
if (!isset(self::$tableMaps[$classNamespace])) {
|
135
|
3 |
|
$namespaceParts = explode('\\', $classNamespace);
|
136
|
3 |
|
$size = sizeof($namespaceParts);
|
137
|
3 |
|
$className = $namespaceParts[$size - 1];
|
138
|
|
|
|
139
|
3 |
|
unset($namespaceParts[$size - 1]);
|
140
|
3 |
|
$tableMapNamespace = join('\\', $namespaceParts) . '\\TableMap\\' . $className . 'TableMap';
|
141
|
|
|
|
142
|
3 |
|
if (!class_exists($tableMapNamespace)) {
|
143
|
1 |
|
throw new \InvalidArgumentException('The table map class "' . $tableMapNamespace . '" does not exist');
|
144
|
|
|
}
|
145
|
|
|
|
146
|
2 |
|
$tableMap = new $tableMapNamespace();
|
147
|
2 |
|
if (!$tableMap instanceof TableMapInterface) {
|
148
|
1 |
|
throw new \InvalidArgumentException(
|
149
|
1 |
|
'The "' . $tableMapNamespace . '" table map must implement '
|
150
|
1 |
|
. '"\Rocket\Model\TableMap\TableMapInterface"'
|
151
|
1 |
|
);
|
152
|
|
|
}
|
153
|
|
|
|
154
|
1 |
|
$tableMap->configure();
|
155
|
1 |
|
self::$tableMaps[$classNamespace] = $tableMap;
|
156
|
1 |
|
}
|
157
|
|
|
|
158
|
1 |
|
return self::$tableMaps[$classNamespace];
|
159
|
|
|
}
|
160
|
|
|
}
|
161
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.