|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains class SqlWiring. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.5 |
|
6
|
|
|
* |
|
7
|
|
|
* LICENSE: |
|
8
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
9
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
10
|
|
|
* database. |
|
11
|
|
|
* Copyright (C) 2016 Michael Cummings |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
14
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
15
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
16
|
|
|
* option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
19
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
21
|
|
|
* for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
24
|
|
|
* along with this program. If not, see |
|
25
|
|
|
* <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
* You should be able to find a copy of this license in the LICENSE.md file. A |
|
28
|
|
|
* copy of the GNU GPL should also be available in the GNU-GPL.md file. |
|
29
|
|
|
* |
|
30
|
|
|
* @copyright 2016 Michael Cummings |
|
31
|
|
|
* @license LGPL-3.0+ |
|
32
|
|
|
* @author Michael Cummings <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Yapeal\Configuration; |
|
35
|
|
|
|
|
36
|
|
|
use PDO; |
|
37
|
|
|
use Twig_Environment; |
|
38
|
|
|
use Twig_Loader_Filesystem; |
|
39
|
|
|
use Twig_SimpleFilter; |
|
40
|
|
|
use Yapeal\Container\ContainerInterface; |
|
41
|
|
|
use Yapeal\Exception\YapealDatabaseException; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class SqlWiring. |
|
45
|
|
|
*/ |
|
46
|
|
|
class SqlWiring implements WiringInterface |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @param ContainerInterface $dic |
|
50
|
|
|
* |
|
51
|
|
|
* @return self Fluent interface. |
|
52
|
|
|
* @throws \InvalidArgumentException |
|
53
|
|
|
* @throws \Yapeal\Exception\YapealDatabaseException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function wire(ContainerInterface $dic) |
|
56
|
|
|
{ |
|
57
|
|
|
if (empty($dic['Yapeal.Sql.CommonQueries'])) { |
|
58
|
|
|
$dic['Yapeal.Sql.CommonQueries'] = function ($dic) { |
|
59
|
|
|
return new $dic['Yapeal.Sql.sharedSql']( |
|
60
|
|
|
$dic['Yapeal.Sql.database'], $dic['Yapeal.Sql.tablePrefix'] |
|
61
|
|
|
); |
|
62
|
|
|
}; |
|
63
|
|
|
} |
|
64
|
|
|
if (!empty($dic['Yapeal.Sql.Connection'])) { |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
if ('mysql' !== $dic['Yapeal.Sql.platform']) { |
|
68
|
|
|
$mess = 'Unknown platform, was given ' . $dic['Yapeal.Sql.platform']; |
|
69
|
|
|
throw new YapealDatabaseException($mess); |
|
70
|
|
|
} |
|
71
|
|
|
$dic['Yapeal.Sql.Connection'] = function ($dic) { |
|
72
|
|
|
$dsn = $dic['Yapeal.Sql.platform'] . ':host=' . $dic['Yapeal.Sql.hostName'] . ';charset=utf8'; |
|
73
|
|
|
if (!empty($dic['Yapeal.Sql.port'])) { |
|
74
|
|
|
$dsn .= ';port=' . $dic['Yapeal.Sql.port']; |
|
75
|
|
|
} |
|
76
|
|
|
/** |
|
77
|
|
|
* @var PDO $database |
|
78
|
|
|
*/ |
|
79
|
|
|
$database = new $dic['Yapeal.Sql.class']( |
|
80
|
|
|
$dsn, $dic['Yapeal.Sql.userName'], $dic['Yapeal.Sql.password'] |
|
81
|
|
|
); |
|
82
|
|
|
$database->setAttribute( |
|
83
|
|
|
PDO::ATTR_ERRMODE, |
|
84
|
|
|
PDO::ERRMODE_EXCEPTION |
|
85
|
|
|
); |
|
86
|
|
|
$database->exec('SET SESSION SQL_MODE=\'ANSI,TRADITIONAL\''); |
|
87
|
|
|
$database->exec('SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE'); |
|
88
|
|
|
$database->exec('SET SESSION TIME_ZONE=\'+00:00\''); |
|
89
|
|
|
$database->exec('SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci'); |
|
90
|
|
|
$database->exec('SET COLLATION_CONNECTION=utf8mb4_unicode_520_ci'); |
|
91
|
|
|
$database->exec('SET DEFAULT_STORAGE_ENGINE=' . $dic['Yapeal.Sql.engine']); |
|
92
|
|
|
return $database; |
|
93
|
|
|
}; |
|
94
|
|
|
if (empty($dic['Yapeal.Sql.Creator'])) { |
|
95
|
|
|
$dic['Yapeal.Sql.Creator'] = $dic->factory( |
|
96
|
|
|
function ($dic) { |
|
97
|
|
|
$loader = new Twig_Loader_Filesystem($dic['Yapeal.Sql.dir']); |
|
98
|
|
|
$twig = new Twig_Environment( |
|
99
|
|
|
$loader, ['debug' => true, 'strict_variables' => true, 'autoescape' => false] |
|
100
|
|
|
); |
|
101
|
|
|
$filter = new Twig_SimpleFilter( |
|
102
|
|
|
'ucFirst', function ($value) { |
|
103
|
|
|
return ucfirst($value); |
|
104
|
|
|
} |
|
105
|
|
|
); |
|
106
|
|
|
$twig->addFilter($filter); |
|
107
|
|
|
$filter = new Twig_SimpleFilter( |
|
108
|
|
|
'lcFirst', function ($value) { |
|
109
|
|
|
return lcfirst($value); |
|
110
|
|
|
} |
|
111
|
|
|
); |
|
112
|
|
|
$twig->addFilter($filter); |
|
113
|
|
|
/** |
|
114
|
|
|
* @var \Yapeal\Sql\Creator $create |
|
115
|
|
|
*/ |
|
116
|
|
|
$create = new $dic['Yapeal.Sql.create']($twig, $dic['Yapeal.Sql.dir'], $dic['Yapeal.Sql.platform']); |
|
117
|
|
|
if (array_key_exists('Yapeal.Create.overwrite', $dic)) { |
|
118
|
|
|
$create->setOverwrite($dic['Yapeal.Create.overwrite']); |
|
119
|
|
|
} |
|
120
|
|
|
return $create; |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
/** |
|
125
|
|
|
* @var \Yapeal\Event\MediatorInterface $mediator |
|
126
|
|
|
*/ |
|
127
|
|
|
$mediator = $dic['Yapeal.Event.Mediator']; |
|
128
|
|
|
$mediator->addServiceSubscriberByEventList( |
|
129
|
|
|
'Yapeal.Sql.Creator', |
|
130
|
|
|
['Yapeal.EveApi.create' => ['createSql', 'last']] |
|
131
|
|
|
); |
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|