|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\Database; |
|
3
|
|
|
|
|
4
|
|
|
use WebStream\Container\Container; |
|
|
|
|
|
|
5
|
|
|
use WebStream\Exception\Extend\ClassNotFoundException; |
|
|
|
|
|
|
6
|
|
|
use WebStream\Exception\Extend\DatabaseException; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* ConnectionManager |
|
10
|
|
|
* @author Ryuichi TANAKA. |
|
11
|
|
|
* @since 2014/06/13 |
|
12
|
|
|
* @version 0.4 |
|
13
|
|
|
*/ |
|
14
|
|
|
class ConnectionManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array<string> クラスパス-DSNハッシュマップ |
|
18
|
|
|
*/ |
|
19
|
|
|
private $classpathMap; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var AnnotationContainer データベース接続項目コンテナ |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
private $connectionContainer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* constructor |
|
28
|
|
|
* @param Container 依存コンテナ |
|
29
|
|
|
*/ |
|
|
|
|
|
|
30
|
|
|
public function __construct(Container $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->initialize($container); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* destructor |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __destruct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->connectionContainer = null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* DBコネクションを返却する |
|
45
|
|
|
* @param string Modelクラスファイルパス |
|
46
|
|
|
* @return DatabaseDriver データベースドライバインスタンス |
|
47
|
|
|
*/ |
|
|
|
|
|
|
48
|
|
|
public function getConnection($filepath) |
|
49
|
|
|
{ |
|
50
|
|
|
$dsnHash = $this->classpathMap[$filepath]; |
|
51
|
|
|
|
|
52
|
|
|
return $dsnHash !== null ? $this->connectionContainer->{$dsnHash} : null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* 初期処理 |
|
57
|
|
|
* @param Container 依存コンテナ |
|
58
|
|
|
*/ |
|
|
|
|
|
|
59
|
|
|
private function initialize(Container $container) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->classpathMap = []; |
|
62
|
|
|
$this->connectionContainer = new Container(); |
|
63
|
|
|
$logger = $container->logger; |
|
64
|
|
|
$innerException = null; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
foreach ($container->connectionContainerList as $container) { |
|
67
|
|
|
$config = null; |
|
68
|
|
|
$ext = pathinfo($container->configPath, PATHINFO_EXTENSION); |
|
69
|
|
|
if ($ext === 'ini') { |
|
70
|
|
|
$config = parse_ini_file($container->configPath); |
|
71
|
|
|
} elseif ($ext === 'yml' || $ext === 'yaml') { |
|
72
|
|
|
$config = \Spyc::YAMLLoad($container->configPath); |
|
73
|
|
|
} else { |
|
74
|
|
|
throw new DatabaseException("Yaml or ini file only available database configuration file."); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$driverClassPath = $container->driverClassPath; |
|
78
|
|
|
|
|
79
|
|
|
if (!class_exists($driverClassPath)) { |
|
80
|
|
|
throw new ClassNotFoundException("$driverClassPath is not defined."); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$dsnHash = ""; |
|
84
|
|
|
$databaseConfigContainer = new Container(false); |
|
|
|
|
|
|
85
|
|
|
foreach ($config as $key => $value) { |
|
86
|
|
|
$dsnHash .= $key . $value; |
|
87
|
|
|
$databaseConfigContainer->set($key, $value); |
|
88
|
|
|
} |
|
89
|
|
|
$dsnHash = md5($dsnHash); |
|
90
|
|
|
|
|
91
|
|
|
$this->classpathMap[$container->filepath] = $dsnHash; |
|
92
|
|
|
|
|
93
|
|
|
$this->connectionContainer->{$dsnHash} = function () use ($driverClassPath, $databaseConfigContainer, $logger) { |
|
94
|
|
|
$driver = new $driverClassPath($databaseConfigContainer); |
|
95
|
|
|
$driver->inject('logger', $logger); |
|
96
|
|
|
|
|
97
|
|
|
return $driver; |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths