|
1
|
|
|
<?php |
|
2
|
|
|
namespace SymphonyCms\Installer\Steps; |
|
3
|
|
|
|
|
4
|
|
|
use Author; |
|
5
|
|
|
use Configuration; |
|
6
|
|
|
use Cryptography; |
|
7
|
|
|
use DatabaseException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Symphony; |
|
10
|
|
|
|
|
11
|
|
|
class CreateDatabase extends DefaultStep |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
public function handle(Configuration $config, array $data) |
|
17
|
|
|
{ |
|
18
|
|
|
// MySQL: Establishing connection |
|
19
|
|
|
$this->logger->info('MYSQL: Establishing Connection'); |
|
20
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
Symphony::Database()->connect( |
|
23
|
|
|
$config->get('host', 'database'), |
|
|
|
|
|
|
24
|
|
|
$config->get('user', 'database'), |
|
|
|
|
|
|
25
|
|
|
$config->get('password', 'database'), |
|
|
|
|
|
|
26
|
|
|
$config->get('port', 'database'), |
|
|
|
|
|
|
27
|
|
|
$config->get('db', 'database') |
|
28
|
|
|
); |
|
29
|
|
|
} catch (DatabaseException $e) { |
|
30
|
|
|
throw new Exception( |
|
31
|
|
|
'There was a problem while trying to establish a connection to the MySQL server. Please check your settings.' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (Symphony::Database()->tableExists($config->get('tbl_prefix', 'database') . '%') && !$this->override) { |
|
36
|
|
|
$this->logger->error('MYSQL: Database table prefix is already in use. Change prefix or run installation with the `--override` flag.', [ |
|
37
|
|
|
'prefix' => $config->get('tbl_prefix', 'database'), |
|
38
|
|
|
'db' => $config->get('db', 'database') |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// MySQL: Setting prefix & importing schema |
|
45
|
|
|
Symphony::Database()->setPrefix($config->get('tbl_prefix', 'database')); |
|
|
|
|
|
|
46
|
|
|
$this->logger->info('MYSQL: Importing Table Schema'); |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
Symphony::Database()->import(file_get_contents(INSTALL . '/includes/install.sql')); |
|
50
|
|
|
} catch (DatabaseException $e) { |
|
51
|
|
|
throw new Exception(sprintf( |
|
52
|
|
|
'There was an error while trying to import data to the database. MySQL returned: %s:%s', |
|
53
|
|
|
$e->getDatabaseErrorCode(), |
|
54
|
|
|
$e->getDatabaseErrorMessage() |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// MySQL: Creating default author |
|
59
|
|
|
if (isset($data['user'])) { |
|
60
|
|
|
$this->logger->info('MYSQL: Creating Default Author'); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
// Clean all the user data. |
|
64
|
|
|
$userData = array_map([Symphony::Database(), 'cleanValue'], $data['user']); |
|
65
|
|
|
|
|
66
|
|
|
$author = new Author; |
|
67
|
|
|
$author->set('user_type', 'developer'); |
|
68
|
|
|
$author->set('primary', 'yes'); |
|
69
|
|
|
$author->set('username', $userData['username']); |
|
70
|
|
|
$author->set('password', Cryptography::hash($userData['password'])); |
|
71
|
|
|
$author->set('first_name', $userData['firstname']); |
|
72
|
|
|
$author->set('last_name', $userData['lastname']); |
|
73
|
|
|
$author->set('email', $userData['email']); |
|
74
|
|
|
$author->commit(); |
|
75
|
|
|
} catch (DatabaseException $e) { |
|
76
|
|
|
throw new Exception(sprintf( |
|
77
|
|
|
'There was an error while trying create the default author. MySQL returned: %s:%s', |
|
78
|
|
|
$e->getDatabaseErrorCode(), |
|
79
|
|
|
$e->getDatabaseErrorMessage() |
|
80
|
|
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
$this->logger->info('MYSQL: Skipping Default Author creation'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.