|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains DatabaseInitializer class. |
|
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) 2014-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 2014-2016 Michael Cummings |
|
31
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
|
32
|
|
|
* @author Michael Cummings <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Yapeal\Console\Command; |
|
35
|
|
|
|
|
36
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
37
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
38
|
|
|
use Yapeal\Container\ContainerInterface; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Class DatabaseInitializer |
|
42
|
|
|
*/ |
|
43
|
|
|
class DatabaseInitializer extends AbstractDatabaseCommon |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|null $name |
|
47
|
|
|
* @param ContainerInterface $dic |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
|
50
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($name, ContainerInterface $dic) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setDescription('Retrieves SQL from files and initializes database'); |
|
55
|
|
|
$this->setName($name); |
|
56
|
|
|
$this->setDic($dic); |
|
57
|
|
|
parent::__construct($name); |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Configures the current command. |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function configure() |
|
63
|
|
|
{ |
|
64
|
|
|
$help = <<<'HELP' |
|
65
|
|
|
The <info>%command.full_name%</info> command is used to initialize (create) a new |
|
66
|
|
|
database and tables to be used by Yapeal. If you already have a |
|
67
|
|
|
config/yapeal.yaml file setup you can use the following: |
|
68
|
|
|
|
|
69
|
|
|
<info>php %command.full_name%</info> |
|
70
|
|
|
|
|
71
|
|
|
EXAMPLES: |
|
72
|
|
|
To use a configuration file in a different location: |
|
73
|
|
|
<info>%command.name% -c /my/very/special/config.yaml</info> |
|
74
|
|
|
|
|
75
|
|
|
<info>NOTE:</info> |
|
76
|
|
|
Only the Database section of the configuration file will be used. |
|
77
|
|
|
|
|
78
|
|
|
You can also use the command before setting up a configuration file like so: |
|
79
|
|
|
<info>%command.name% -o "localhost" -d "yapeal" -u "YapealUser" -p "secret" |
|
80
|
|
|
|
|
81
|
|
|
HELP; |
|
82
|
|
|
$this->addOptions($help); |
|
83
|
|
|
$desc = 'Drop existing database before re-creating. <comment>Warning all the tables will be dropped as well!</comment>'; |
|
|
|
|
|
|
84
|
|
|
$this->addOption('dropDatabase', null, InputOption::VALUE_NONE, $desc); |
|
85
|
|
|
} |
|
86
|
|
|
/** |
|
87
|
|
|
* @param OutputInterface $output |
|
88
|
|
|
* |
|
89
|
|
|
* @return string[] |
|
90
|
|
|
* @throws \LogicException |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getCreateFileList(OutputInterface $output) |
|
93
|
|
|
{ |
|
94
|
|
|
$dic = $this->getDic(); |
|
95
|
|
|
$sections = ['Database', 'Util', 'Account', 'Api', 'Char', 'Corp', 'Eve', 'Map', 'Server']; |
|
96
|
|
|
$path = $dic['Yapeal.Sql.dir']; |
|
97
|
|
|
if (!is_readable($path)) { |
|
98
|
|
|
if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) { |
|
99
|
|
|
$mess = sprintf('<comment>Could NOT access Sql directory %1$s</comment>', $path); |
|
100
|
|
|
$output->writeln($mess); |
|
101
|
|
|
} |
|
102
|
|
|
return []; |
|
103
|
|
|
} |
|
104
|
|
|
$fileList = []; |
|
105
|
|
|
foreach ($sections as $dir) { |
|
106
|
|
|
if ('Database' === $dir && $this->dropDatabase) { |
|
107
|
|
|
// Add drop database file if requested. |
|
108
|
|
|
$fileList[] = $this->getFpn() |
|
109
|
|
|
->normalizeFile($path . $dir . '/DropDatabase.sql'); |
|
110
|
|
|
} |
|
111
|
|
|
foreach (new \DirectoryIterator($path . $dir . '/') as $fileInfo) { |
|
112
|
|
|
// Add file path if it's a sql create file. |
|
113
|
|
|
if ($fileInfo->isFile() |
|
114
|
|
|
&& 'sql' === $fileInfo->getExtension() |
|
115
|
|
|
&& 0 === strpos($fileInfo->getBasename(), 'Create') |
|
116
|
|
|
) { |
|
117
|
|
|
$fileList[] = $this->getFpn() |
|
118
|
|
|
->normalizeFile($fileInfo->getPathname()); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$fileNames = '%1$sCreateCustomTables.sql,%2$sconfig/CreateCustomTables.sql'; |
|
123
|
|
|
$vendorPath = ''; |
|
124
|
|
|
if (!empty($dic['Yapeal.vendorParentDir'])) { |
|
125
|
|
|
$fileNames .= ',%3$sconfig/CreateCustomTables.sql'; |
|
126
|
|
|
$vendorPath = $dic['Yapeal.vendorParentDir']; |
|
127
|
|
|
} |
|
128
|
|
|
/** |
|
129
|
|
|
* @var array $customFiles |
|
130
|
|
|
*/ |
|
131
|
|
|
$customFiles = explode(',', sprintf($fileNames, $path, $dic['Yapeal.baseDir'], $vendorPath)); |
|
132
|
|
|
foreach ($customFiles as $fileName) { |
|
133
|
|
|
if (!is_readable($fileName) || !is_file($fileName)) { |
|
134
|
|
|
continue; |
|
135
|
|
|
} |
|
136
|
|
|
$fileList[] = $fileName; |
|
137
|
|
|
} |
|
138
|
|
|
return $fileList; |
|
139
|
|
|
} |
|
140
|
|
|
/** |
|
141
|
|
|
* @param array $options |
|
142
|
|
|
* |
|
143
|
|
|
* @return self Fluent interface. |
|
144
|
|
|
* @throws \LogicException |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function processCliOptions(array $options) |
|
147
|
|
|
{ |
|
148
|
|
|
if (array_key_exists('dropDatabase', $options)) { |
|
149
|
|
|
$this->dropDatabase = $options['dropDatabase']; |
|
150
|
|
|
} |
|
151
|
|
|
return parent::processCliOptions($options); |
|
152
|
|
|
} |
|
153
|
|
|
/** |
|
154
|
|
|
* @param OutputInterface $output |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \InvalidArgumentException |
|
157
|
|
|
* @throws \LogicException |
|
158
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
|
159
|
|
|
* @throws \Yapeal\Exception\YapealDatabaseException |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function processSql(OutputInterface $output) |
|
162
|
|
|
{ |
|
163
|
|
|
foreach ($this->getCreateFileList($output) as $fileName) { |
|
164
|
|
|
$sqlStatements = file_get_contents($fileName); |
|
165
|
|
|
if (false === $sqlStatements) { |
|
166
|
|
|
if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) { |
|
167
|
|
|
$mess = sprintf('<error>Could NOT get contents of SQL file %1$s</error>', $fileName); |
|
168
|
|
|
$output->writeln($mess); |
|
169
|
|
|
} |
|
170
|
|
|
continue; |
|
171
|
|
|
} |
|
172
|
|
|
$this->executeSqlStatements($sqlStatements, $fileName, $output); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
/** |
|
176
|
|
|
* @var bool $dropDatabase |
|
177
|
|
|
*/ |
|
178
|
|
|
private $dropDatabase = false; |
|
179
|
|
|
} |
|
180
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.