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 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 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\SqlSubsTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class SqlWiring. |
43
|
|
|
*/ |
44
|
|
|
class SqlWiring implements WiringInterface |
45
|
|
|
{ |
46
|
|
|
use SqlSubsTrait; |
47
|
|
|
/** |
48
|
|
|
* @param ContainerInterface $dic |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
* @throws \LogicException |
52
|
|
|
* @throws \Yapeal\Exception\YapealDatabaseException |
53
|
|
|
*/ |
54
|
|
|
public function wire(ContainerInterface $dic) |
55
|
|
|
{ |
56
|
|
|
if (empty($dic['Yapeal.Sql.CommonQueries'])) { |
57
|
|
|
$dic['Yapeal.Sql.CommonQueries'] = function ($dic) { |
58
|
|
|
/** |
59
|
|
|
* @var CommonSqlQueries $csq |
60
|
|
|
*/ |
61
|
|
|
$csq = new $dic['Yapeal.Sql.Handlers.queries']($dic['Yapeal.Sql.database'], |
62
|
|
|
$dic['Yapeal.Sql.tablePrefix']); |
63
|
|
|
$csq->setDic($dic) |
64
|
|
|
->setYem($dic['Yapeal.Event.Mediator']); |
65
|
|
|
return $csq; |
66
|
|
|
}; |
67
|
|
|
} |
68
|
|
|
$this->wireConnection($dic); |
69
|
|
|
$this->wireCreator($dic); |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* @param \Yapeal\Container\ContainerInterface $dic |
73
|
|
|
* |
74
|
|
|
* @throws \Yapeal\Exception\YapealDatabaseException |
75
|
|
|
*/ |
76
|
|
|
private function wireConnection(ContainerInterface $dic) |
77
|
|
|
{ |
78
|
|
|
if (empty($dic['Yapeal.Sql.Connection'])) { |
79
|
|
|
$replacements = $this->getSqlSubs($dic); |
80
|
|
|
$dic['Yapeal.Sql.Connection'] = function () use ($dic, $replacements) { |
81
|
|
|
$dsn = $replacements['{dsn}']; |
82
|
|
|
$dsn = str_replace(array_keys($replacements), array_values($replacements), $dsn); |
83
|
|
|
/** |
84
|
|
|
* @var \PDO $database |
85
|
|
|
* @var CommonSqlQueries $csq |
86
|
|
|
*/ |
87
|
|
|
$database = new $dic['Yapeal.Sql.Handlers.connection']($dsn, |
88
|
|
|
$replacements['{userName}'], |
89
|
|
|
$replacements['{password}']); |
90
|
|
|
$database->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
91
|
|
|
$csq = $dic['Yapeal.Sql.CommonQueries']; |
92
|
|
|
$sql = $csq->initialization(); |
93
|
|
|
$database->exec($sql); |
94
|
|
|
return $database; |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* @param \Yapeal\Container\ContainerInterface $dic |
100
|
|
|
* |
101
|
|
|
* @throws \LogicException |
102
|
|
|
*/ |
103
|
|
|
private function wireCreator(ContainerInterface $dic) |
104
|
|
|
{ |
105
|
|
|
if (empty($dic['Yapeal.Sql.Creator'])) { |
106
|
|
|
$dic['Yapeal.Sql.Creator'] = function () use ($dic) { |
107
|
|
|
$loader = new \Twig_Loader_Filesystem($dic['Yapeal.Sql.dir']); |
108
|
|
|
$twig = new \Twig_Environment($loader, |
109
|
|
|
['debug' => true, 'strict_variables' => true, 'autoescape' => false]); |
110
|
|
|
$filter = new \Twig_SimpleFilter('ucFirst', function ($value) { |
111
|
|
|
return ucfirst($value); |
112
|
|
|
}); |
113
|
|
|
$twig->addFilter($filter); |
114
|
|
|
$filter = new \Twig_SimpleFilter('lcFirst', function ($value) { |
115
|
|
|
return lcfirst($value); |
116
|
|
|
}); |
117
|
|
|
$twig->addFilter($filter); |
118
|
|
|
/** |
119
|
|
|
* @var \Yapeal\Sql\Creator $create |
120
|
|
|
*/ |
121
|
|
|
$create = new $dic['Yapeal.Sql.Handlers.create']($twig, |
122
|
|
|
$dic['Yapeal.Sql.dir'], |
123
|
|
|
$dic['Yapeal.Sql.platform']); |
124
|
|
|
if (!empty($dic['Yapeal.Create.overwrite'])) { |
125
|
|
|
$create->setOverwrite($dic['Yapeal.Create.overwrite']); |
126
|
|
|
} |
127
|
|
|
return $create; |
128
|
|
|
}; |
129
|
|
|
} |
130
|
|
|
if (empty($dic['Yapeal.Event.Mediator'])) { |
131
|
|
|
$mess = 'Tried to call Mediator before it has been added'; |
132
|
|
|
throw new \LogicException($mess); |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* @var \Yapeal\Event\MediatorInterface $mediator |
136
|
|
|
*/ |
137
|
|
|
$mediator = $dic['Yapeal.Event.Mediator']; |
138
|
|
|
$mediator->addServiceListener('Yapeal.EveApi.create', ['Yapeal.Sql.Creator', 'createSql'], 'last'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|