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