1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Dedipanel project |
5
|
|
|
* |
6
|
|
|
* (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DP\Core\DistributionBundle\Configurator\Step; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use DP\Core\DistributionBundle\Configurator\Form\DoctrineStepType; |
16
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
17
|
|
|
use Doctrine\DBAL\DriverManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Doctrine Step. |
21
|
|
|
* |
22
|
|
|
* @author Fabien Potencier <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DoctrineStep implements StepInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @Assert\NotBlank(message="configurator.db.host.blank") |
28
|
|
|
*/ |
29
|
|
|
public $host; |
30
|
|
|
|
31
|
|
|
public $port; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @Assert\NotBlank(message="configurator.db.name.blank") |
35
|
|
|
*/ |
36
|
|
|
public $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Assert\NotBlank(message="configurator.db.user.blank") |
40
|
|
|
*/ |
41
|
|
|
public $user; |
42
|
|
|
|
43
|
|
|
public $password; |
44
|
|
|
|
45
|
|
|
private $container; |
46
|
|
|
|
47
|
|
|
public function __construct(ContainerInterface $container) |
48
|
|
|
{ |
49
|
|
|
$this->container = $container; |
50
|
|
|
|
51
|
|
|
$installer = $container->get('dp.webinstaller'); |
52
|
|
|
$parameters = $installer->getConfigParameters(); |
53
|
|
|
|
54
|
|
|
foreach ($parameters as $key => $value) { |
55
|
|
|
if (0 === strpos($key, 'database_')) { |
56
|
|
|
$parameters[substr($key, 9)] = $value; |
57
|
|
|
$key = substr($key, 9); |
58
|
|
|
|
59
|
|
|
switch ($key) { |
60
|
|
|
case 'host': |
61
|
|
|
case 'port': |
62
|
|
|
case 'name': |
63
|
|
|
case 'user': |
64
|
|
|
case 'password': |
65
|
|
|
$this->$key = $value; |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @see StepInterface |
74
|
|
|
*/ |
75
|
|
|
public function getFormType() |
76
|
|
|
{ |
77
|
|
|
return new DoctrineStepType(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @see StepInterface |
82
|
|
|
*/ |
83
|
|
|
public function checkRequirements() |
84
|
|
|
{ |
85
|
|
|
$messages = array(); |
86
|
|
|
|
87
|
|
|
$messages['configurator.pdo_mandatory'] = true; |
88
|
|
|
$messages['configurator.mysql_extension_mandatory'] = true; |
89
|
|
|
|
90
|
|
|
if (!class_exists('\PDO')) { |
91
|
|
|
$messages['configurator.pdo_mandatory'] = false; |
92
|
|
|
$messages['configurator.mysql_extension_mandatory'] = false; |
93
|
|
|
} else { |
94
|
|
|
$drivers = \PDO::getAvailableDrivers(); |
95
|
|
|
|
96
|
|
|
if (!in_array('mysql', $drivers)) { |
97
|
|
|
$messages['configurator.mysql_extension_mandatory'] = false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $messages; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @see StepInterface |
106
|
|
|
*/ |
107
|
|
|
public function checkOptionalSettings() |
108
|
|
|
{ |
109
|
|
|
return array(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function run(StepInterface $data, $configType) |
113
|
|
|
{ |
114
|
|
|
if (!$data instanceof DoctrineStep) { |
115
|
|
|
throw new \RuntimeException("Can't manage another step that itself."); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$errors = array(); |
119
|
|
|
$configurator = $this->container->get('dp.webinstaller'); |
120
|
|
|
$parameters = array(); |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* @var array $data |
124
|
|
|
*/ |
125
|
|
|
foreach ($data as $key => $value) { |
126
|
|
|
$parameters['database_' . $key] = $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!$configurator->isFileWritable()) { |
130
|
|
|
$errors[] = 'Your app/config/parameters.yml is not writable.'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$this->testConnection($data->host, $data->user, $data->password, $data->port, $data->name)) { |
134
|
|
|
return array_merge($errors, array('configurator.db.connectionTest')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$configurator->mergeParameters($parameters); |
138
|
|
|
|
139
|
|
|
if (!$configurator->write()) { |
140
|
|
|
return array_merge($errors, array('An error occured while writing the app/config/parameters.yml file.')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Suppression "hard" du cache (sinon les nouveaux paramètres ne sont pas pris en compte) |
144
|
|
|
$cacheFile = $configurator->getKernelDir() . '/cache/installer/appInstallerProjectContainer.php'; |
145
|
|
|
if (file_exists($cacheFile)) { |
146
|
|
|
unlink($cacheFile); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Trying to connect to the database |
152
|
|
|
* |
153
|
|
|
* @param string $dbname |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
private function testConnection($host, $user, $password, $port, $dbname) |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
|
|
$conn = DriverManager::getConnection(array( |
160
|
|
|
'driver' => 'pdo_mysql', |
161
|
|
|
'user' => $user, |
162
|
|
|
'password' => $password, |
163
|
|
|
'host' => $host, |
164
|
|
|
'port' => $port, |
165
|
|
|
'dbname' => $dbname, |
166
|
|
|
)); |
167
|
|
|
|
168
|
|
|
$conn->connect(); |
169
|
|
|
$conn->close(); |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
catch (\Exception $e) { |
174
|
|
|
// var_dump($e->getMessage()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @see StepInterface |
182
|
|
|
*/ |
183
|
|
|
public function getTemplate() |
184
|
|
|
{ |
185
|
|
|
return 'DPDistributionBundle:Configurator/Step:doctrine.html.twig'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getTitle() |
189
|
|
|
{ |
190
|
|
|
return 'configurator.db.title'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
static public function getDriverKeys() |
197
|
|
|
{ |
198
|
|
|
return array_keys(static::getDrivers()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
static public function getDrivers() |
205
|
|
|
{ |
206
|
|
|
return array( |
207
|
|
|
'pdo_mysql' => 'MySQL (PDO)', |
208
|
|
|
'pdo_sqlite' => 'SQLite (PDO)', |
209
|
|
|
'pdo_pgsql' => 'PosgreSQL (PDO)', |
210
|
|
|
'oci8' => 'Oracle (native)', |
211
|
|
|
'ibm_db2' => 'IBM DB2 (native)', |
212
|
|
|
'pdo_oci' => 'Oracle (PDO)', |
213
|
|
|
'pdo_ibm' => 'IBM DB2 (PDO)', |
214
|
|
|
'pdo_sqlsrv' => 'SQLServer (PDO)', |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function isInstallStep() |
219
|
|
|
{ |
220
|
|
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function isUpdateStep() |
224
|
|
|
{ |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.