|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains class SqlWiring. |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7.0+ |
|
7
|
|
|
* |
|
8
|
|
|
* LICENSE: |
|
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
11
|
|
|
* database. |
|
12
|
|
|
* Copyright (C) 2016-2017 Michael Cummings |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
17
|
|
|
* option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
22
|
|
|
* for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
25
|
|
|
* along with this program. If not, see |
|
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
|
27
|
|
|
* |
|
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
|
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2016-2017 Michael Cummings |
|
32
|
|
|
* @license LGPL-3.0+ |
|
33
|
|
|
* @author Michael Cummings <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
namespace Yapeal\Configuration; |
|
36
|
|
|
|
|
37
|
|
|
use Yapeal\Container\ContainerInterface; |
|
38
|
|
|
use Yapeal\Sql\CommonSqlQueries; |
|
39
|
|
|
use Yapeal\Sql\ConnectionInterface; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Class SqlWiring. |
|
43
|
|
|
*/ |
|
44
|
|
|
class SqlWiring implements WiringInterface |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @param ContainerInterface $dic |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
* @throws \LogicException |
|
51
|
|
|
* @throws \Yapeal\Exception\YapealDatabaseException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function wire(ContainerInterface $dic) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->wireMergedSubsCallable($dic) |
|
56
|
|
|
->wireCommonQueries($dic) |
|
57
|
|
|
->wireConnection($dic) |
|
58
|
|
|
->wireCreator($dic); |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* @param ContainerInterface $dic |
|
62
|
|
|
* |
|
63
|
|
|
* @return self Fluent interface. |
|
64
|
|
|
*/ |
|
65
|
|
|
private function wireCommonQueries(ContainerInterface $dic): self |
|
66
|
|
|
{ |
|
67
|
|
|
if (empty($dic['Yapeal.Sql.Callable.CommonQueries'])) { |
|
68
|
|
|
/** |
|
69
|
|
|
* @param ContainerInterface $dic |
|
70
|
|
|
* |
|
71
|
|
|
* @return CommonSqlQueries |
|
72
|
|
|
*/ |
|
73
|
|
|
$dic['Yapeal.Sql.Callable.CommonQueries'] = function (ContainerInterface $dic): CommonSqlQueries { |
|
74
|
|
|
return new $dic['Yapeal.Sql.Classes.queries']($dic['Yapeal.Sql.Callable.GetSqlMergedSubs']); |
|
75
|
|
|
}; |
|
76
|
|
|
} |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* @param ContainerInterface $dic |
|
81
|
|
|
* |
|
82
|
|
|
* @return self Fluent interface. |
|
83
|
|
|
*/ |
|
84
|
|
|
private function wireConnection(ContainerInterface $dic): self |
|
85
|
|
|
{ |
|
86
|
|
|
if (empty($dic['Yapeal.Sql.Callable.Connection'])) { |
|
87
|
|
|
/** |
|
88
|
|
|
* @param ContainerInterface $dic |
|
89
|
|
|
* |
|
90
|
|
|
* @return ConnectionInterface |
|
91
|
|
|
* @throws \PDOException |
|
92
|
|
|
*/ |
|
93
|
|
|
$dic['Yapeal.Sql.Callable.Connection'] = function (ContainerInterface $dic): ConnectionInterface { |
|
94
|
|
|
/** |
|
95
|
|
|
* @var \Yapeal\Sql\Connection $conn |
|
96
|
|
|
*/ |
|
97
|
|
|
$sqlSubs = $dic['Yapeal.Sql.Callable.GetSqlMergedSubs']; |
|
98
|
|
|
$dsn = $sqlSubs['{dsn}']; |
|
99
|
|
|
$dsn = str_replace(array_keys($sqlSubs), array_values($sqlSubs), $dsn); |
|
100
|
|
|
$conn = new $dic['Yapeal.Sql.Classes.connection']($dsn, $sqlSubs['{userName}'], $sqlSubs['{password}']); |
|
101
|
|
|
$conn->setExposingPdo($dic['Yapeal.Sql.Parameters.connection.exposingPdo']) |
|
102
|
|
|
->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
103
|
|
|
$csq = $dic['Yapeal.Sql.Callable.CommonQueries']; |
|
104
|
|
|
$conn->exec($csq->getInitialization()); |
|
105
|
|
|
$conn->setSql92Mode(); |
|
106
|
|
|
return $conn; |
|
107
|
|
|
}; |
|
108
|
|
|
} |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
/** |
|
112
|
|
|
* @param \Yapeal\Container\ContainerInterface $dic |
|
113
|
|
|
* |
|
114
|
|
|
* @return self Fluent interface. |
|
115
|
|
|
* @throws \LogicException |
|
116
|
|
|
*/ |
|
117
|
|
|
private function wireCreator(ContainerInterface $dic): self |
|
118
|
|
|
{ |
|
119
|
|
|
if (empty($dic['Yapeal.Sql.Callable.Creator'])) { |
|
120
|
|
|
/** |
|
121
|
|
|
* @param ContainerInterface $dic |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Yapeal\Sql\Creator |
|
124
|
|
|
* @throws \LogicException |
|
125
|
|
|
*/ |
|
126
|
|
|
$dic['Yapeal.Sql.Callable.Creator'] = function (ContainerInterface $dic) { |
|
127
|
|
|
$loader = new \Twig_Loader_Filesystem($dic['Yapeal.Sql.dir']); |
|
128
|
|
|
$twig = new \Twig_Environment($loader, |
|
129
|
|
|
['debug' => true, 'strict_variables' => true, 'autoescape' => false]); |
|
130
|
|
|
$filter = new \Twig_SimpleFilter('ucFirst', function ($value) { |
|
131
|
|
|
return ucfirst($value); |
|
132
|
|
|
}); |
|
133
|
|
|
$twig->addFilter($filter); |
|
134
|
|
|
$filter = new \Twig_SimpleFilter('lcFirst', function ($value) { |
|
135
|
|
|
return lcfirst($value); |
|
136
|
|
|
}); |
|
137
|
|
|
$twig->addFilter($filter); |
|
138
|
|
|
/** |
|
139
|
|
|
* @var \Yapeal\Sql\Creator $create |
|
140
|
|
|
*/ |
|
141
|
|
|
$create = new $dic['Yapeal.Sql.Classes.create']($twig, |
|
142
|
|
|
$dic['Yapeal.Sql.dir'], |
|
143
|
|
|
$dic['Yapeal.Sql.platform']); |
|
144
|
|
|
if (!empty($dic['Yapeal.Create.overwrite'])) { |
|
145
|
|
|
$create->setOverwrite($dic['Yapeal.Create.overwrite']); |
|
146
|
|
|
} |
|
147
|
|
|
return $create; |
|
148
|
|
|
}; |
|
149
|
|
|
} |
|
150
|
|
|
/** |
|
151
|
|
|
* @var \Yapeal\Event\MediatorInterface $mediator |
|
152
|
|
|
*/ |
|
153
|
|
|
$mediator = $dic['Yapeal.Event.Callable.Mediator']; |
|
154
|
|
|
$mediator->addServiceListener('Yapeal.EveApi.create', ['Yapeal.Sql.Callable.Creator', 'createSql'], 'last'); |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
/** |
|
158
|
|
|
* @param ContainerInterface $dic |
|
159
|
|
|
* |
|
160
|
|
|
* @return self Fluent interface. |
|
161
|
|
|
*/ |
|
162
|
|
|
private function wireMergedSubsCallable(ContainerInterface $dic): self |
|
163
|
|
|
{ |
|
164
|
|
|
if (empty($dic['Yapeal.Sql.Callable.GetSqlMergedSubs'])) { |
|
165
|
|
|
/** |
|
166
|
|
|
* @param ContainerInterface $dic |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
$dic['Yapeal.Sql.Callable.GetSqlMergedSubs'] = function (ContainerInterface $dic): array { |
|
171
|
|
|
$getScalars = $dic['Yapeal.Config.Callable.ExtractScalarsByKeyPrefix']; |
|
172
|
|
|
$base = []; |
|
173
|
|
|
foreach ($getScalars($dic, 'Yapeal.Sql.') as $index => $item) { |
|
174
|
|
|
$base['{' . $index . '}'] = $item; |
|
175
|
|
|
} |
|
176
|
|
|
$perPlatform = []; |
|
177
|
|
|
if (in_array('Yapeal.Sql.platform', $dic->keys())) { |
|
178
|
|
|
$platformParameters = 'Yapeal.Sql.Parameters.' . $dic['Yapeal.Sql.platform']; |
|
179
|
|
|
foreach ($getScalars($dic, $platformParameters) as $index => $item) { |
|
180
|
|
|
$perPlatform['{' . $index . '}'] = $item; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
return array_merge($base, $perPlatform); |
|
184
|
|
|
}; |
|
185
|
|
|
} |
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|