|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cli\ConfigurationLoader |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Log\LogLevel; |
|
24
|
|
|
use Ramsey\Uuid\Uuid; |
|
25
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
26
|
|
|
use TechDivision\Import\Configuration\Jms\Configuration\Database; |
|
27
|
|
|
use TechDivision\Import\Cli\Command\InputArgumentKeys; |
|
28
|
|
|
use TechDivision\Import\Cli\Command\InputOptionKeys; |
|
29
|
|
|
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys; |
|
30
|
|
|
use TechDivision\Import\Cli\Utils\MagentoConfigurationKeys; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The configuration loader implementation. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Tim Wagner <[email protected]> |
|
36
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
38
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
39
|
|
|
* @link http://www.techdivision.com |
|
40
|
|
|
*/ |
|
41
|
|
|
class ConfigurationLoader extends SimpleConfigurationLoader |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Factory implementation to create a new initialized configuration instance. |
|
46
|
|
|
* |
|
47
|
|
|
* If command line options are specified, they will always override the |
|
48
|
|
|
* values found in the configuration file. |
|
49
|
|
|
* |
|
50
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The configuration instance |
|
51
|
|
|
* @throws \Exception Is thrown, if the specified configuration file doesn't exist or the mandatory arguments/options to run the requested operation are not available |
|
52
|
|
|
*/ |
|
53
|
|
|
public function load() |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
// load the configuration instance |
|
57
|
|
|
$instance = parent::load(); |
|
58
|
|
|
|
|
59
|
|
|
// set the serial that has been specified as command line option (or the default value) |
|
60
|
|
|
$instance->setSerial($this->input->getOption(InputOptionKeys::SERIAL)); |
|
61
|
|
|
|
|
62
|
|
|
// query whether or not an operation name has been specified as command line |
|
63
|
|
|
// option, if yes override the value from the configuration file |
|
64
|
|
|
if ($operationName = $this->input->getArgument(InputArgumentKeys::OPERATION_NAME)) { |
|
65
|
|
|
$instance->setOperationName($operationName); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// query whether or not a Magento version has been specified as command line |
|
69
|
|
|
// option, if yes override the value from the configuration file |
|
70
|
|
|
if ($magentoVersion = $this->input->getOption(InputOptionKeys::MAGENTO_VERSION)) { |
|
71
|
|
|
$instance->setMagentoVersion($magentoVersion); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// query whether or not a directory containing the imported files has been specified as command line |
|
75
|
|
|
// option, if yes override the value from the configuration file |
|
76
|
|
|
if ($targetDir = $this->input->getOption(InputOptionKeys::TARGET_DIR)) { |
|
77
|
|
|
$instance->setTargetDir($targetDir); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// query whether or not a directory containing the archived imported files has been specified as command line |
|
81
|
|
|
// option, if yes override the value from the configuration file |
|
82
|
|
|
if ($archiveDir = $this->input->getOption(InputOptionKeys::ARCHIVE_DIR)) { |
|
83
|
|
|
$instance->setArchiveDir($archiveDir); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// query whether or not the debug mode has been specified as command line |
|
87
|
|
|
// option, if yes override the value from the configuration file |
|
88
|
|
|
if ($archiveArtefacts = $this->input->getOption(InputOptionKeys::ARCHIVE_ARTEFACTS)) { |
|
89
|
|
|
$instance->setArchiveArtefacts($instance->mapBoolean($archiveArtefacts)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// query whether or not a source date format has been specified as command |
|
93
|
|
|
// line option, if yes override the value from the configuration file |
|
94
|
|
|
if ($sourceDateFormat = $this->input->getOption(InputOptionKeys::SOURCE_DATE_FORMAT)) { |
|
95
|
|
|
$instance->setSourceDateFormat($sourceDateFormat); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// query whether or not the debug mode has been specified as command line |
|
99
|
|
|
// option, if yes override the value from the configuration file |
|
100
|
|
|
if ($debugMode = $this->input->getOption(InputOptionKeys::DEBUG_MODE)) { |
|
101
|
|
|
$instance->setDebugMode($instance->mapBoolean($debugMode)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// query whether or not the log level has been specified as command line |
|
105
|
|
|
// option, if yes override the value from the configuration file |
|
106
|
|
|
if ($logLevel = $this->input->getOption(InputOptionKeys::LOG_LEVEL)) { |
|
107
|
|
|
$instance->setLogLevel($logLevel); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// query whether or not the single transaction flag has been specified as command line |
|
111
|
|
|
// option, if yes override the value from the configuration file |
|
112
|
|
|
if ($singleTransaction = $this->input->getOption(InputOptionKeys::SINGLE_TRANSACTION)) { |
|
113
|
|
|
$instance->setSingleTransaction($instance->mapBoolean($singleTransaction)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// query whether or not the cache flag has been specified as command line |
|
117
|
|
|
// option, if yes override the value from the configuration file |
|
118
|
|
|
if ($cacheEnabled = $this->input->getOption(InputOptionKeys::CACHE_ENABLED)) { |
|
119
|
|
|
$instance->setCacheEnabled($instance->mapBoolean($cacheEnabled)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// query whether or not we've an valid Magento root directory specified |
|
123
|
|
|
if ($this->isMagentoRootDir($installationDir = $instance->getInstallationDir())) { |
|
124
|
|
|
// if yes, add the database configuration |
|
125
|
|
|
$instance->addDatabase($this->getMagentoDbConnection($installationDir)); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// add the source directory if NOT specified in the configuration file |
|
128
|
|
|
if (($sourceDir = $instance->getSourceDir()) === null) { |
|
|
|
|
|
|
129
|
|
|
$instance->setSourceDir($sourceDir = sprintf('%s/var/importexport', $installationDir)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// add the target directory if NOT specified in the configuration file |
|
133
|
|
|
if ($instance->getTargetDir() === null) { |
|
|
|
|
|
|
134
|
|
|
$instance->setTargetDir($sourceDir); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// query whether or not a DB ID has been specified as command line |
|
139
|
|
|
// option, if yes override the value from the configuration file |
|
140
|
|
|
if ($useDbId = $this->input->getOption(InputOptionKeys::USE_DB_ID)) { |
|
141
|
|
|
$instance->setUseDbId($useDbId); |
|
142
|
|
|
} else { |
|
143
|
|
|
// query whether or not a PDO DSN has been specified as command line |
|
144
|
|
|
// option, if yes override the value from the configuration file |
|
145
|
|
|
if ($dsn = $this->input->getOption(InputOptionKeys::DB_PDO_DSN)) { |
|
146
|
|
|
// first REMOVE all other database configurations |
|
147
|
|
|
$instance->clearDatabases(); |
|
148
|
|
|
|
|
149
|
|
|
// add the database configuration |
|
150
|
|
|
$instance->addDatabase( |
|
151
|
|
|
$this->newDatabaseConfiguration( |
|
152
|
|
|
$dsn, |
|
153
|
|
|
$this->input->getOption(InputOptionKeys::DB_USERNAME), |
|
154
|
|
|
$this->input->getOption(InputOptionKeys::DB_PASSWORD) |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// query whether or not a DB ID has been specified as command line |
|
161
|
|
|
// option, if yes override the value from the configuration file |
|
162
|
|
|
if ($tablePrefix = $this->input->getOption(InputOptionKeys::DB_TABLE_PREFIX)) { |
|
163
|
|
|
$instance->getDatabase()->setTablePrefix($tablePrefix); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// extend the plugins with the main configuration instance |
|
167
|
|
|
/** @var \TechDivision\Import\Cli\Configuration\Subject $subject */ |
|
168
|
|
|
foreach ($instance->getPlugins() as $plugin) { |
|
169
|
|
|
// set the configuration instance on the plugin |
|
170
|
|
|
$plugin->setConfiguration($instance); |
|
171
|
|
|
|
|
172
|
|
|
// query whether or not the plugin has subjects configured |
|
173
|
|
|
if ($subjects = $plugin->getSubjects()) { |
|
174
|
|
|
// extend the plugin's subjects with the main configuration instance |
|
175
|
|
|
/** @var \TechDivision\Import\Cli\Configuration\Subject $subject */ |
|
176
|
|
|
foreach ($subjects as $subject) { |
|
177
|
|
|
// set the configuration instance on the subject |
|
178
|
|
|
$subject->setConfiguration($instance); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// query whether or not the debug mode is enabled and log level |
|
184
|
|
|
// has NOT been overwritten with a commandline option |
|
185
|
|
|
if ($instance->isDebugMode() && !$this->input->getOption(InputOptionKeys::LOG_LEVEL)) { |
|
186
|
|
|
// set debug log level, if log level has NOT been overwritten on command line |
|
187
|
|
|
$instance->setLogLevel(LogLevel::DEBUG); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// prepend the array with the Magento Edition specific core libraries |
|
191
|
|
|
$instance->setExtensionLibraries( |
|
192
|
|
|
array_merge( |
|
193
|
|
|
$this->getDefaultLibraries($instance->getMagentoEdition()), |
|
194
|
|
|
$instance->getExtensionLibraries() |
|
195
|
|
|
) |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
// load the extension libraries, if configured |
|
199
|
|
|
$this->libraryLoader->load($instance); |
|
200
|
|
|
|
|
201
|
|
|
// register the configured aliases in the DI container, this MUST |
|
202
|
|
|
// happen after the libraries have been loaded, else it would not |
|
203
|
|
|
// be possible to override existing aliases |
|
204
|
|
|
$this->initializeAliases($instance); |
|
205
|
|
|
|
|
206
|
|
|
// return the initialized configuration instance |
|
207
|
|
|
return $instance; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Return's the requested Magento DB connction data. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $dir The path to the Magento root directory |
|
214
|
|
|
* @param string $connectionName The connection name to return the data for |
|
215
|
|
|
* |
|
216
|
|
|
* @return array The connection data |
|
217
|
|
|
* @throws \Exception Is thrown, if the requested DB connection is not available |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function getMagentoDbConnection($dir, $connectionName = 'default') |
|
220
|
|
|
{ |
|
221
|
|
|
|
|
222
|
|
|
// load the magento environment |
|
223
|
|
|
$env = require $this->getMagentoEnv($dir); |
|
224
|
|
|
|
|
225
|
|
|
// query whether or not, the requested connection is available |
|
226
|
|
|
if (isset($env[MagentoConfigurationKeys::DB][MagentoConfigurationKeys::CONNECTION][$connectionName])) { |
|
227
|
|
|
// load the databaase connection |
|
228
|
|
|
$db = $env[MagentoConfigurationKeys::DB]; |
|
229
|
|
|
// load the connection data |
|
230
|
|
|
$connection = $db[MagentoConfigurationKeys::CONNECTION][$connectionName]; |
|
231
|
|
|
|
|
232
|
|
|
// create and return a new database configuration |
|
233
|
|
|
return $this->newDatabaseConfiguration( |
|
|
|
|
|
|
234
|
|
|
$this->newDsn($connection[MagentoConfigurationKeys::HOST], $connection[MagentoConfigurationKeys::DBNAME]), |
|
235
|
|
|
$connection[MagentoConfigurationKeys::USERNAME], |
|
236
|
|
|
$connection[MagentoConfigurationKeys::PASSWORD], |
|
237
|
|
|
false, |
|
238
|
|
|
null, |
|
239
|
|
|
isset($db[MagentoConfigurationKeys::TABLE_PREFIX]) ? $db[MagentoConfigurationKeys::TABLE_PREFIX] : null |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// throw an execption if not |
|
244
|
|
|
throw new \Exception(sprintf('Requested Magento DB connection "%s" not found in Magento "%s"', $connectionName, $dir)); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Create's and return's a new database configuration instance, initialized with |
|
249
|
|
|
* the passed values. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $dsn The DSN to use |
|
252
|
|
|
* @param string $username The username to use |
|
253
|
|
|
* @param string|null $password The passed to use |
|
254
|
|
|
* @param boolean $default TRUE if this should be the default connection |
|
255
|
|
|
* @param string $id The ID to use |
|
256
|
|
|
* @param string $tablePrefix The table prefix to use |
|
257
|
|
|
* |
|
258
|
|
|
* @return \TechDivision\Import\Configuration\Jms\Configuration\Database The database configuration instance |
|
259
|
|
|
*/ |
|
260
|
|
|
protected function newDatabaseConfiguration($dsn, $username = 'root', $password = null, $default = true, $id = null, $tablePrefix = null) |
|
261
|
|
|
{ |
|
262
|
|
|
|
|
263
|
|
|
// initialize a new database configuration |
|
264
|
|
|
$database = new Database(); |
|
265
|
|
|
$database->setDsn($dsn); |
|
266
|
|
|
$database->setDefault($default); |
|
267
|
|
|
$database->setUsername($username); |
|
268
|
|
|
|
|
269
|
|
|
// query whether or not an ID has been passed |
|
270
|
|
|
if ($id === null) { |
|
271
|
|
|
$id = Uuid::uuid4()->__toString(); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
// set the ID |
|
275
|
|
|
$database->setId($id); |
|
276
|
|
|
|
|
277
|
|
|
// query whether or not a password has been passed |
|
278
|
|
|
if ($password) { |
|
279
|
|
|
$database->setPassword($password); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
// query whether or not a table prefix has been passed |
|
283
|
|
|
if ($tablePrefix) { |
|
284
|
|
|
$database->setTablePrefix($tablePrefix); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
// return the database configuration |
|
288
|
|
|
return $database; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Create's and return's a new DSN from the passed values. |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $host The host to use |
|
295
|
|
|
* @param string $dbName The database name to use |
|
296
|
|
|
* @param string $charset The charset to use |
|
297
|
|
|
* |
|
298
|
|
|
* @return string The DSN |
|
299
|
|
|
*/ |
|
300
|
|
|
protected function newDsn($host, $dbName, $charset = 'utf8') |
|
301
|
|
|
{ |
|
302
|
|
|
return sprintf('mysql:host=%s;dbname=%s;charset=%s', $host, $dbName, $charset); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Return's the Magento Edition specific default libraries. Supported Magento Editions are CE or EE. |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $magentoEdition The Magento Edition to return the libraries for |
|
309
|
|
|
* |
|
310
|
|
|
* @return array The Magento Edition specific default libraries |
|
311
|
|
|
* @throws \Exception Is thrown, if the passed Magento Edition is NOT supported |
|
312
|
|
|
*/ |
|
313
|
|
|
protected function getDefaultLibraries($magentoEdition) |
|
314
|
|
|
{ |
|
315
|
|
|
|
|
316
|
|
|
// load the default libraries from the configuration |
|
317
|
|
|
$defaultLibraries = $this->getContainer()->getParameter(DependencyInjectionKeys::APPLICATION_DEFAULT_LIBRARIES); |
|
318
|
|
|
|
|
319
|
|
|
// query whether or not, default libraries for the passed edition are available |
|
320
|
|
|
if (isset($defaultLibraries[$edition = strtolower($magentoEdition)])) { |
|
321
|
|
|
return $defaultLibraries[$edition]; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
// throw an exception, if the passed edition is not supported |
|
325
|
|
|
throw new \Exception( |
|
326
|
|
|
sprintf( |
|
327
|
|
|
'Default libraries for Magento \'%s\' not supported (MUST be one of CE or EE)', |
|
328
|
|
|
$magentoEdition |
|
329
|
|
|
) |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Registers the configured aliases in the DI container. |
|
335
|
|
|
* |
|
336
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration with the aliases to register |
|
337
|
|
|
* |
|
338
|
|
|
* @return void |
|
339
|
|
|
*/ |
|
340
|
|
|
protected function initializeAliases(ConfigurationInterface $configuration) |
|
341
|
|
|
{ |
|
342
|
|
|
|
|
343
|
|
|
// load the DI aliases |
|
344
|
|
|
$aliases = $configuration->getAliases(); |
|
345
|
|
|
|
|
346
|
|
|
// register the DI aliases |
|
347
|
|
|
foreach ($aliases as $alias) { |
|
348
|
|
|
$this->getContainer()->setAlias($alias->getId(), $alias->getTarget()); |
|
|
|
|
|
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.