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