1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento; |
4
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
use Exception; |
7
|
|
|
use Mage; |
8
|
|
|
use Magento\Mtf\EntryPoint\EntryPoint; |
9
|
|
|
use N98\Magento\Application\Config; |
10
|
|
|
use N98\Magento\Application\ConfigurationLoader; |
11
|
|
|
use N98\Magento\Application\Console\Events; |
12
|
|
|
use N98\Util\AutoloadRestorer; |
13
|
|
|
use N98\Util\Console\Helper\MagentoHelper; |
14
|
|
|
use N98\Util\Console\Helper\TwigHelper; |
15
|
|
|
use N98\Util\OperatingSystem; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Event\ConsoleEvent; |
20
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
21
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
22
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
23
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
25
|
|
|
use Symfony\Component\Console\Input\InputOption; |
26
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
29
|
|
|
use UnexpectedValueException; |
30
|
|
|
|
31
|
|
|
class Application extends BaseApplication |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
const APP_NAME = 'n98-magerun'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const APP_VERSION = '1.97.28'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
const MAGENTO_MAJOR_VERSION_1 = 1; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
const MAGENTO_MAJOR_VERSION_2 = 2; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private static $logo = " |
57
|
|
|
___ ___ |
58
|
|
|
_ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |
59
|
|
|
| ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ |
60
|
|
|
|_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_| |
61
|
|
|
|___/ |
62
|
|
|
"; |
63
|
|
|
/** |
64
|
|
|
* @var ClassLoader |
65
|
|
|
*/ |
66
|
|
|
protected $autoloader; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Config |
70
|
|
|
*/ |
71
|
|
|
protected $config; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see \N98\Magento\Application::setConfigurationLoader() |
75
|
|
|
* @var ConfigurationLoader |
76
|
|
|
*/ |
77
|
|
|
private $configurationLoaderInjected; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $_magentoRootFolder = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
protected $_magentoEnterprise = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var int |
91
|
|
|
*/ |
92
|
|
|
protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var EntryPoint |
96
|
|
|
*/ |
97
|
|
|
protected $_magento2EntryPoint = null; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
protected $_isPharMode = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var bool |
106
|
|
|
*/ |
107
|
|
|
protected $_magerunStopFileFound = false; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
protected $_magerunStopFileFolder = null; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var bool |
116
|
|
|
*/ |
117
|
|
|
protected $_isInitialized = false; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var EventDispatcher |
121
|
|
|
*/ |
122
|
|
|
protected $dispatcher; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* If root dir is set by root-dir option this flag is true |
126
|
|
|
* |
127
|
|
|
* @var bool |
128
|
|
|
*/ |
129
|
|
|
protected $_directRootDir = false; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var bool |
133
|
|
|
*/ |
134
|
|
|
protected $_magentoDetected = false; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var bool |
138
|
|
|
*/ |
139
|
|
|
private static $_isScriptEnvironment = false; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ClassLoader $autoloader |
143
|
|
|
*/ |
144
|
|
|
public function __construct($autoloader = null) |
145
|
|
|
{ |
146
|
|
|
$this->autoloader = $autoloader; |
147
|
|
|
parent::__construct(self::APP_NAME, self::APP_VERSION); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return InputDefinition |
152
|
|
|
*/ |
153
|
|
|
protected function getDefaultInputDefinition() |
154
|
|
|
{ |
155
|
|
|
$inputDefinition = parent::getDefaultInputDefinition(); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Root dir |
159
|
|
|
*/ |
160
|
|
|
$rootDirOption = new InputOption( |
161
|
|
|
'--root-dir', |
162
|
|
|
'', |
163
|
|
|
InputOption::VALUE_OPTIONAL, |
164
|
|
|
'Force magento root dir. No auto detection' |
165
|
|
|
); |
166
|
|
|
$inputDefinition->addOption($rootDirOption); |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Skip config |
170
|
|
|
*/ |
171
|
|
|
$skipExternalConfig = new InputOption( |
172
|
|
|
'--skip-config', |
173
|
|
|
'', |
174
|
|
|
InputOption::VALUE_NONE, |
175
|
|
|
'Do not load any custom config.' |
176
|
|
|
); |
177
|
|
|
$inputDefinition->addOption($skipExternalConfig); |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Skip root check |
181
|
|
|
*/ |
182
|
|
|
$skipExternalConfig = new InputOption( |
183
|
|
|
'--skip-root-check', |
184
|
|
|
'', |
185
|
|
|
InputOption::VALUE_NONE, |
186
|
|
|
'Do not check if n98-magerun runs as root' |
187
|
|
|
); |
188
|
|
|
$inputDefinition->addOption($skipExternalConfig); |
189
|
|
|
|
190
|
|
|
return $inputDefinition; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Search for magento root folder |
195
|
|
|
* |
196
|
|
|
* @param InputInterface $input [optional] |
197
|
|
|
* @param OutputInterface $output [optional] |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
201
|
|
|
{ |
202
|
|
|
// do not detect magento twice |
203
|
|
|
if ($this->_magentoDetected) { |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (null === $input) { |
208
|
|
|
$input = new ArgvInput(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (null === $output) { |
212
|
|
|
$output = new ConsoleOutput(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($this->getMagentoRootFolder() === null) { |
216
|
|
|
$this->_checkRootDirOption($input); |
217
|
|
|
$folder = OperatingSystem::getCwd(); |
218
|
|
|
} else { |
219
|
|
|
$folder = $this->getMagentoRootFolder(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$this->getHelperSet()->set(new MagentoHelper($input, $output), 'magento'); |
223
|
|
|
$magentoHelper = $this->getHelperSet()->get('magento'); |
224
|
|
|
/* @var $magentoHelper MagentoHelper */ |
225
|
|
|
if (!$this->_directRootDir) { |
226
|
|
|
$subFolders = $this->config->getDetectSubFolders(); |
227
|
|
|
} else { |
228
|
|
|
$subFolders = array(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->_magentoDetected = $magentoHelper->detect($folder, $subFolders); |
232
|
|
|
$this->_magentoRootFolder = $magentoHelper->getRootFolder(); |
233
|
|
|
$this->_magentoEnterprise = $magentoHelper->isEnterpriseEdition(); |
234
|
|
|
$this->_magentoMajorVersion = $magentoHelper->getMajorVersion(); |
235
|
|
|
$this->_magerunStopFileFound = $magentoHelper->isMagerunStopFileFound(); |
236
|
|
|
$this->_magerunStopFileFolder = $magentoHelper->getMagerunStopFileFolder(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Add own helpers to helperset. |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
protected function registerHelpers() |
245
|
|
|
{ |
246
|
|
|
$helperSet = $this->getHelperSet(); |
247
|
|
|
$config = $this->config->getConfig(); |
248
|
|
|
|
249
|
|
|
// Twig |
250
|
|
|
$twigBaseDirs = array( |
251
|
|
|
__DIR__ . '/../../../res/twig', |
252
|
|
|
); |
253
|
|
|
if (isset($config['twig']['baseDirs']) && is_array($config['twig']['baseDirs'])) { |
254
|
|
|
$twigBaseDirs = array_merge(array_reverse($config['twig']['baseDirs']), $twigBaseDirs); |
255
|
|
|
} |
256
|
|
|
$helperSet->set(new TwigHelper($twigBaseDirs), 'twig'); |
257
|
|
|
|
258
|
|
|
foreach ($config['helpers'] as $helperName => $helperClass) { |
259
|
|
|
if (class_exists($helperClass)) { |
260
|
|
|
$helperSet->set(new $helperClass(), $helperName); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param InputInterface $input |
267
|
|
|
* |
268
|
|
|
* @return ArgvInput|InputInterface |
269
|
|
|
*/ |
270
|
|
|
protected function checkConfigCommandAlias(InputInterface $input) |
271
|
|
|
{ |
272
|
|
|
trigger_error(__METHOD__ . ' moved, use getConfig()->checkConfigCommandAlias()', E_USER_DEPRECATED); |
273
|
|
|
|
274
|
|
|
return $this->config->checkConfigCommandAlias($input); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param Command $command |
279
|
|
|
*/ |
280
|
|
|
protected function registerConfigCommandAlias(Command $command) |
281
|
|
|
{ |
282
|
|
|
trigger_error(__METHOD__ . ' moved, use getConfig()->registerConfigCommandAlias() instead', E_USER_DEPRECATED); |
283
|
|
|
|
284
|
|
|
return $this->config->registerConfigCommandAlias($command); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Adds autoloader prefixes from user's config |
289
|
|
|
*/ |
290
|
|
|
protected function registerCustomAutoloaders() |
291
|
|
|
{ |
292
|
|
|
trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomAutoloaders() instead', E_USER_DEPRECATED); |
293
|
|
|
|
294
|
|
|
$this->config->registerCustomAutoloaders($this->autoloader); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return bool |
299
|
|
|
*/ |
300
|
|
|
protected function hasCustomCommands() |
301
|
|
|
{ |
302
|
|
|
trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED); |
303
|
|
|
|
304
|
|
|
$config = $this->config->getConfig(); |
305
|
|
|
|
306
|
|
|
return isset($config['commands']['customCommands']) && is_array($config['commands']['customCommands']); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
protected function registerCustomCommands() |
313
|
|
|
{ |
314
|
|
|
trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomCommands() instead', E_USER_DEPRECATED); |
315
|
|
|
|
316
|
|
|
$this->config->registerCustomCommands($this); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $class |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
|
|
protected function isCommandDisabled($class) |
324
|
|
|
{ |
325
|
|
|
trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED); |
326
|
|
|
|
327
|
|
|
$config = $this->config->getConfig(); |
328
|
|
|
|
329
|
|
|
return in_array($class, $config['commands']['disabled']); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Override standard command registration. We want alias support. |
334
|
|
|
* |
335
|
|
|
* @param Command $command |
336
|
|
|
* |
337
|
|
|
* @return Command |
338
|
|
|
*/ |
339
|
|
|
public function add(Command $command) |
340
|
|
|
{ |
341
|
|
|
if ($this->config) { |
342
|
|
|
$this->config->registerConfigCommandAlias($command); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return parent::add($command); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param bool $mode |
350
|
|
|
*/ |
351
|
|
|
public function setPharMode($mode) |
352
|
|
|
{ |
353
|
|
|
$this->_isPharMode = $mode; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return bool |
358
|
|
|
*/ |
359
|
|
|
public function isPharMode() |
360
|
|
|
{ |
361
|
|
|
return $this->_isPharMode; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @TODO Move logic into "EventSubscriber" |
366
|
|
|
* |
367
|
|
|
* @param OutputInterface $output |
368
|
|
|
* @return null|false |
369
|
|
|
*/ |
370
|
|
|
public function checkVarDir(OutputInterface $output) |
371
|
|
|
{ |
372
|
|
|
$tempVarDir = sys_get_temp_dir() . '/magento/var'; |
373
|
|
|
if (!OutputInterface::VERBOSITY_NORMAL <= $output->getVerbosity() && !is_dir($tempVarDir)) { |
374
|
|
|
return; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$this->detectMagento(null, $output); |
378
|
|
|
/* If magento is not installed yet, don't check */ |
379
|
|
|
if ($this->_magentoRootFolder === null |
380
|
|
|
|| !file_exists($this->_magentoRootFolder . '/app/etc/local.xml') |
381
|
|
|
) { |
382
|
|
|
return; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
try { |
386
|
|
|
$this->initMagento(); |
387
|
|
|
} catch (Exception $e) { |
388
|
|
|
$message = 'Cannot initialize Magento. Please check your configuration. ' |
389
|
|
|
. 'Some n98-magerun command will not work. Got message: '; |
390
|
|
|
if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $output->getVerbosity()) { |
391
|
|
|
$message .= $e->getTraceAsString(); |
392
|
|
|
} else { |
393
|
|
|
$message .= $e->getMessage(); |
394
|
|
|
} |
395
|
|
|
$output->writeln($message); |
396
|
|
|
|
397
|
|
|
return; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
$configOptions = new \Mage_Core_Model_Config_Options(); |
401
|
|
|
$currentVarDir = $configOptions->getVarDir(); |
402
|
|
|
|
403
|
|
|
if ($currentVarDir == $tempVarDir) { |
404
|
|
|
$output->writeln(array( |
405
|
|
|
sprintf('<warning>Fallback folder %s is used in n98-magerun</warning>', $tempVarDir), |
406
|
|
|
'', |
407
|
|
|
'n98-magerun is using the fallback folder. If there is another folder configured for Magento, this ' . |
408
|
|
|
'can cause serious problems.', |
409
|
|
|
'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' . |
410
|
|
|
'for more information.', |
411
|
|
|
'', |
412
|
|
|
)); |
413
|
|
|
} else { |
414
|
|
|
$output->writeln(array( |
415
|
|
|
sprintf('<warning>Folder %s found, but not used in n98-magerun</warning>', $tempVarDir), |
416
|
|
|
'', |
417
|
|
|
"This might cause serious problems. n98-magerun is using the configured var-folder " . |
418
|
|
|
"<comment>$currentVarDir</comment>", |
419
|
|
|
'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' . |
420
|
|
|
'for more information.', |
421
|
|
|
'', |
422
|
|
|
)); |
423
|
|
|
|
424
|
|
|
return false; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Loads and initializes the Magento application |
430
|
|
|
* |
431
|
|
|
* @param bool $soft |
432
|
|
|
* |
433
|
|
|
* @return bool false if magento root folder is not set, true otherwise |
434
|
|
|
*/ |
435
|
|
|
public function initMagento($soft = false) |
436
|
|
|
{ |
437
|
|
|
if ($this->getMagentoRootFolder() === null) { |
438
|
|
|
return false; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$isMagento2 = $this->_magentoMajorVersion === self::MAGENTO_MAJOR_VERSION_2; |
442
|
|
|
if ($isMagento2) { |
443
|
|
|
$this->_initMagento2(); |
444
|
|
|
} else { |
445
|
|
|
$this->_initMagento1($soft); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
return true; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @return string |
453
|
|
|
*/ |
454
|
|
|
public function getHelp() |
455
|
|
|
{ |
456
|
|
|
return self::$logo . parent::getHelp(); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
public function getLongVersion() |
460
|
|
|
{ |
461
|
|
|
return parent::getLongVersion() . ' by <info>netz98 GmbH</info>'; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @return boolean |
466
|
|
|
*/ |
467
|
|
|
public function isMagentoEnterprise() |
468
|
|
|
{ |
469
|
|
|
return $this->_magentoEnterprise; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @return string |
474
|
|
|
*/ |
475
|
|
|
public function getMagentoRootFolder() |
476
|
|
|
{ |
477
|
|
|
return $this->_magentoRootFolder; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param string $magentoRootFolder |
482
|
|
|
*/ |
483
|
|
|
public function setMagentoRootFolder($magentoRootFolder) |
484
|
|
|
{ |
485
|
|
|
$this->_magentoRootFolder = $magentoRootFolder; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @return int |
490
|
|
|
*/ |
491
|
|
|
public function getMagentoMajorVersion() |
492
|
|
|
{ |
493
|
|
|
return $this->_magentoMajorVersion; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @return ClassLoader |
498
|
|
|
*/ |
499
|
|
|
public function getAutoloader() |
500
|
|
|
{ |
501
|
|
|
return $this->autoloader; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @param ClassLoader $autoloader |
506
|
|
|
*/ |
507
|
|
|
public function setAutoloader(ClassLoader $autoloader) |
508
|
|
|
{ |
509
|
|
|
$this->autoloader = $autoloader; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Get config array |
514
|
|
|
* |
515
|
|
|
* Specify one key per parameter to traverse the config. Then returns null |
516
|
|
|
* if the path of the key(s) can not be obtained. |
517
|
|
|
* |
518
|
|
|
* @param string|int $key ... (optional) |
519
|
|
|
* |
520
|
|
|
* @return array|null |
521
|
|
|
*/ |
522
|
|
|
public function getConfig($key = null) |
523
|
|
|
{ |
524
|
|
|
$array = $this->config->getConfig(); |
525
|
|
|
|
526
|
|
|
$keys = func_get_args(); |
527
|
|
|
foreach ($keys as $key) { |
528
|
|
|
if (null === $key) { |
529
|
|
|
continue; |
530
|
|
|
} |
531
|
|
|
if (!isset($array[$key])) { |
532
|
|
|
return null; |
533
|
|
|
} |
534
|
|
|
$array = $array[$key]; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
return $array; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @param array $config |
542
|
|
|
*/ |
543
|
|
|
public function setConfig($config) |
544
|
|
|
{ |
545
|
|
|
$this->config->setConfig($config); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* @return boolean |
550
|
|
|
*/ |
551
|
|
|
public function isMagerunStopFileFound() |
552
|
|
|
{ |
553
|
|
|
return $this->_magerunStopFileFound; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Runs the current application with possible command aliases |
558
|
|
|
* |
559
|
|
|
* @param InputInterface $input An Input instance |
560
|
|
|
* @param OutputInterface $output An Output instance |
561
|
|
|
* |
562
|
|
|
* @return integer 0 if everything went fine, or an error code |
563
|
|
|
*/ |
564
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
565
|
|
|
{ |
566
|
|
|
$event = new Application\Console\Event($this, $input, $output); |
567
|
|
|
$this->dispatcher->dispatch(Events::RUN_BEFORE, $event); |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* only for compatibility to old versions. |
571
|
|
|
*/ |
572
|
|
|
$event = new ConsoleEvent(new Command('dummy'), $input, $output); |
573
|
|
|
$this->dispatcher->dispatch('console.run.before', $event); |
574
|
|
|
|
575
|
|
|
$input = $this->config->checkConfigCommandAlias($input); |
576
|
|
|
if ($output instanceof ConsoleOutput) { |
577
|
|
|
$this->checkVarDir($output->getErrorOutput()); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
return parent::doRun($input, $output); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* @param InputInterface $input [optional] |
585
|
|
|
* @param OutputInterface $output [optional] |
586
|
|
|
* |
587
|
|
|
* @return int |
588
|
|
|
*/ |
589
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
590
|
|
|
{ |
591
|
|
|
if (null === $input) { |
592
|
|
|
$input = new ArgvInput(); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
if (null === $output) { |
596
|
|
|
$output = new ConsoleOutput(); |
597
|
|
|
} |
598
|
|
|
$this->_addOutputStyles($output); |
599
|
|
|
if ($output instanceof ConsoleOutput) { |
600
|
|
|
$this->_addOutputStyles($output->getErrorOutput()); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
$this->configureIO($input, $output); |
604
|
|
|
|
605
|
|
|
try { |
606
|
|
|
$this->init(array(), $input, $output); |
607
|
|
|
} catch (Exception $e) { |
608
|
|
|
$output = new ConsoleOutput(); |
609
|
|
|
$this->renderException($e, $output->getErrorOutput()); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
$return = parent::run($input, $output); |
613
|
|
|
|
614
|
|
|
// Fix for no return values -> used in interactive shell to prevent error output |
615
|
|
|
if ($return === null) { |
616
|
|
|
return 0; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
return $return; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* @param array $initConfig [optional] |
624
|
|
|
* @param InputInterface $input [optional] |
625
|
|
|
* @param OutputInterface $output [optional] |
626
|
|
|
* |
627
|
|
|
* @return void |
628
|
|
|
*/ |
629
|
|
|
public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
630
|
|
|
{ |
631
|
|
|
if ($this->_isInitialized) { |
632
|
|
|
return; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
// Suppress DateTime warnings |
636
|
|
|
date_default_timezone_set(@date_default_timezone_get()); |
637
|
|
|
|
638
|
|
|
// Initialize EventDispatcher early |
639
|
|
|
$this->dispatcher = new EventDispatcher(); |
640
|
|
|
$this->setDispatcher($this->dispatcher); |
641
|
|
|
|
642
|
|
|
$input = $input ?: new ArgvInput(); |
643
|
|
|
$output = $output ?: new ConsoleOutput(); |
644
|
|
|
|
645
|
|
|
if (null !== $this->config) { |
646
|
|
|
throw new UnexpectedValueException(sprintf('Config already initialized')); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
$loadExternalConfig = !$input->hasParameterOption('--skip-config'); |
650
|
|
|
|
651
|
|
|
$this->config = $config = new Config($initConfig, $this->isPharMode(), $output); |
652
|
|
|
if ($this->configurationLoaderInjected) { |
653
|
|
|
$config->setLoader($this->configurationLoaderInjected); |
654
|
|
|
} |
655
|
|
|
$config->loadPartialConfig($loadExternalConfig); |
656
|
|
|
$this->detectMagento($input, $output); |
657
|
|
|
$configLoader = $config->getLoader(); |
658
|
|
|
$configLoader->loadStageTwo($this->_magentoRootFolder, $loadExternalConfig, $this->_magerunStopFileFolder); |
659
|
|
|
$config->load(); |
660
|
|
|
|
661
|
|
|
if ($autoloader = $this->autoloader) { |
662
|
|
|
$config->registerCustomAutoloaders($autoloader); |
663
|
|
|
$this->registerEventSubscribers(); |
664
|
|
|
$config->registerCustomCommands($this); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
$this->registerHelpers(); |
668
|
|
|
|
669
|
|
|
$this->_isInitialized = true; |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* @param array $initConfig [optional] |
674
|
|
|
* @param InputInterface $input [optional] |
675
|
|
|
* @param OutputInterface $output [optional] |
676
|
|
|
*/ |
677
|
|
|
public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
678
|
|
|
{ |
679
|
|
|
$this->_isInitialized = false; |
680
|
|
|
$this->_magentoDetected = false; |
681
|
|
|
$this->_magentoRootFolder = null; |
682
|
|
|
$this->config = null; |
683
|
|
|
$this->init($initConfig, $input, $output); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @return void |
688
|
|
|
*/ |
689
|
|
|
protected function registerEventSubscribers() |
690
|
|
|
{ |
691
|
|
|
$config = $this->config->getConfig(); |
692
|
|
|
$subscriberClasses = $config['event']['subscriber']; |
693
|
|
|
foreach ($subscriberClasses as $subscriberClass) { |
694
|
|
|
$subscriber = new $subscriberClass(); |
695
|
|
|
$this->dispatcher->addSubscriber($subscriber); |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* @param InputInterface $input |
701
|
|
|
* @return bool |
702
|
|
|
* @deprecated 1.97.27 |
703
|
|
|
*/ |
704
|
|
|
protected function _checkSkipConfigOption(InputInterface $input) |
705
|
|
|
{ |
706
|
|
|
trigger_error( |
707
|
|
|
__METHOD__ . ' removed, use $input->hasParameterOption(\'--skip-config\') instead', |
708
|
|
|
E_USER_DEPRECATED |
709
|
|
|
); |
710
|
|
|
|
711
|
|
|
return $input->hasParameterOption('--skip-config'); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* @param InputInterface $input |
716
|
|
|
* @return string |
717
|
|
|
*/ |
718
|
|
|
protected function _checkRootDirOption(InputInterface $input) |
719
|
|
|
{ |
720
|
|
|
$rootDir = $input->getParameterOption('--root-dir'); |
721
|
|
|
if (is_string($rootDir)) { |
722
|
|
|
$this->setRootDir($rootDir); |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Set root dir (chdir()) of magento directory |
728
|
|
|
* |
729
|
|
|
* @param string $path to Magento directory |
730
|
|
|
*/ |
731
|
|
|
private function setRootDir($path) |
732
|
|
|
{ |
733
|
|
|
if (isset($path[0]) && '~' === $path[0]) { |
734
|
|
|
$path = OperatingSystem::getHomeDir() . substr($path, 1); |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
$folder = realpath($path); |
738
|
|
|
$this->_directRootDir = true; |
739
|
|
|
if (is_dir($folder)) { |
740
|
|
|
chdir($folder); |
741
|
|
|
} |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* use require-once inside a function with it's own variable scope w/o any other variables |
746
|
|
|
* and $this unbound. |
747
|
|
|
* |
748
|
|
|
* @param string $path |
749
|
|
|
*/ |
750
|
|
|
private function requireOnce($path) |
751
|
|
|
{ |
752
|
|
|
$requireOnce = function () { |
753
|
|
|
require_once func_get_arg(0); |
754
|
|
|
}; |
755
|
|
|
if (50400 <= PHP_VERSION_ID) { |
756
|
|
|
$requireOnce->bindTo(null); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
$requireOnce($path); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* @param bool $soft |
764
|
|
|
* |
765
|
|
|
* @return void |
766
|
|
|
*/ |
767
|
|
|
protected function _initMagento1($soft = false) |
768
|
|
|
{ |
769
|
|
|
if (!class_exists('Mage', false)) { |
770
|
|
|
// Create a new AutoloadRestorer to capture currenjt auto-öpaders |
771
|
|
|
$restorer = new AutoloadRestorer(); |
772
|
|
|
// require app/Mage.php from Magento in a function of it's own to have it's own variable scope |
773
|
|
|
$this->requireOnce($this->_magentoRootFolder . '/app/Mage.php'); |
774
|
|
|
// Restore auto-loaders that might be removed by extensions that overwrite Varien/Autoload |
775
|
|
|
$restorer->restore(); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
// skip Mage::app init routine and return |
779
|
|
|
if ($soft === true) { |
780
|
|
|
return; |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
$config = $this->config->getConfig(); |
784
|
|
|
$initSettings = $config['init']; |
785
|
|
|
|
786
|
|
|
Mage::app($initSettings['code'], $initSettings['type'], $initSettings['options']); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* @return void |
791
|
|
|
*/ |
792
|
|
|
protected function _initMagento2() |
793
|
|
|
{ |
794
|
|
|
$this->outputMagerunCompatibilityNotice('2'); |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
/** |
798
|
|
|
* Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
799
|
|
|
* |
800
|
|
|
* @param string $version of Magento, "1" or "2", that is incompatible |
801
|
|
|
*/ |
802
|
|
|
private function outputMagerunCompatibilityNotice($version) |
803
|
|
|
{ |
804
|
|
|
$file = $version === '2' ? $version : ''; |
805
|
|
|
$magentoHint = <<<MAGENTOHINT |
806
|
|
|
You are running a Magento $version.x instance. This version of n98-magerun is not compatible |
807
|
|
|
with Magento $version.x. Please use n98-magerun$version (version $version) for this shop. |
808
|
|
|
|
809
|
|
|
A current version of the software can be downloaded on github. |
810
|
|
|
|
811
|
|
|
<info>Download with curl |
812
|
|
|
------------------</info> |
813
|
|
|
|
814
|
|
|
<comment>curl -O https://files.magerun.net/n98-magerun$file.phar</comment> |
815
|
|
|
|
816
|
|
|
<info>Download with wget |
817
|
|
|
------------------</info> |
818
|
|
|
|
819
|
|
|
<comment>wget https://files.magerun.net/n98-magerun$file.phar</comment> |
820
|
|
|
|
821
|
|
|
MAGENTOHINT; |
822
|
|
|
|
823
|
|
|
$output = new ConsoleOutput(); |
824
|
|
|
|
825
|
|
|
/** @var $formatter FormatterHelper */ |
826
|
|
|
$formatter = $this->getHelperSet()->get('formatter'); |
827
|
|
|
|
828
|
|
|
$output->writeln(array( |
829
|
|
|
'', |
830
|
|
|
$formatter->formatBlock('Compatibility Notice', 'bg=blue;fg=white', true), |
831
|
|
|
'', |
832
|
|
|
$magentoHint, |
833
|
|
|
)); |
834
|
|
|
|
835
|
|
|
throw new RuntimeException('This version of n98-magerun is not compatible with Magento ' . $version); |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* @return EventDispatcher |
840
|
|
|
*/ |
841
|
|
|
public function getDispatcher() |
842
|
|
|
{ |
843
|
|
|
return $this->dispatcher; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* @param array $initConfig |
848
|
|
|
* @param OutputInterface $output |
849
|
|
|
* @return ConfigurationLoader |
850
|
|
|
*/ |
851
|
|
|
public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
852
|
|
|
{ |
853
|
|
|
trigger_error(__METHOD__ . ' moved, use getConfig()->getLoader()', E_USER_DEPRECATED); |
854
|
|
|
|
855
|
|
|
unset($initConfig, $output); |
856
|
|
|
|
857
|
|
|
$loader = $this->config ? $this->config->getLoader() : $this->configurationLoaderInjected; |
858
|
|
|
|
859
|
|
|
if (!$loader) { |
860
|
|
|
throw new RuntimeException('ConfigurationLoader is not yet available, initialize it or Config first'); |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
return $loader; |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
/** |
867
|
|
|
* @param ConfigurationLoader $configurationLoader |
868
|
|
|
* |
869
|
|
|
* @return $this |
870
|
|
|
*/ |
871
|
|
|
public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
872
|
|
|
{ |
873
|
|
|
if ($this->config) { |
874
|
|
|
$this->config->setLoader($configurationLoader); |
875
|
|
|
} else { |
876
|
|
|
/* inject loader to be used later when config is created in */ |
877
|
|
|
/* @see N98\Magento\Application::init */ |
878
|
|
|
$this->configurationLoaderInjected = $configurationLoader; |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
return $this; |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
/** |
885
|
|
|
* @param OutputInterface $output |
886
|
|
|
*/ |
887
|
|
|
protected function _addOutputStyles(OutputInterface $output) |
888
|
|
|
{ |
889
|
|
|
$output->getFormatter()->setStyle('debug', new OutputFormatterStyle('magenta', 'white')); |
890
|
|
|
$output->getFormatter()->setStyle('warning', new OutputFormatterStyle('red', 'yellow', array('bold'))); |
891
|
|
|
} |
892
|
|
|
/** |
893
|
|
|
* @return bool |
894
|
|
|
*/ |
895
|
|
|
public function isScriptEnvironment() |
896
|
|
|
{ |
897
|
|
|
return self::$_isScriptEnvironment; |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
/** |
901
|
|
|
* @param $state bool |
902
|
|
|
*/ |
903
|
|
|
public function setScriptEnvironment($state) |
904
|
|
|
{ |
905
|
|
|
self::$_isScriptEnvironment = $state; |
906
|
|
|
} |
907
|
|
|
} |
908
|
|
|
|