1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Setup; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\StringInput; |
10
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class RunCommand extends AbstractMagentoCommand |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this |
18
|
|
|
->setName('sys:setup:run') |
19
|
|
|
->addOption( |
20
|
|
|
'--no-implicit-cache-flush', |
21
|
|
|
null, |
22
|
|
|
InputOption::VALUE_NONE, |
23
|
|
|
'Do not flush the cache' |
24
|
|
|
) |
25
|
|
|
->setDescription('Runs all new setup scripts.'); |
26
|
|
|
$help = <<<HELP |
27
|
|
|
Runs all setup scripts (no need to call frontend). |
28
|
|
|
This command is useful if you update your system with enabled maintenance mode. |
29
|
|
|
HELP; |
30
|
|
|
$this->setHelp($help); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* @return int|null |
37
|
|
|
*/ |
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
$this->getApplication()->setAutoExit(false); |
41
|
|
|
$this->detectMagento($output); |
42
|
|
|
if (!$this->initMagento()) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
|
48
|
|
|
if (false === $input->getOption('no-implicit-cache-flush')) { |
49
|
|
|
$this->flushCache(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Put output in buffer. \Mage_Core_Model_Resource_Setup::_modifyResourceDb should print any error |
54
|
|
|
* directly to stdout. Use execption which will be thrown to show error |
55
|
|
|
*/ |
56
|
|
|
\ob_start(); |
57
|
|
|
\Mage_Core_Model_Resource_Setup::applyAllUpdates(); |
58
|
|
|
if (is_callable(array('\Mage_Core_Model_Resource_Setup', 'applyAllDataUpdates'))) { |
59
|
|
|
\Mage_Core_Model_Resource_Setup::applyAllDataUpdates(); |
60
|
|
|
} |
61
|
|
|
\ob_end_clean(); |
62
|
|
|
$output->writeln('<info>done</info>'); |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
\ob_end_clean(); |
65
|
|
|
$this->printException($output, $e); |
66
|
|
|
$this->printStackTrace($output, $e->getTrace()); |
67
|
|
|
$this->printFile($output, $e); |
68
|
|
|
|
69
|
|
|
return 1; // exit with error status |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param OutputInterface $output |
75
|
|
|
* @param array $trace |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
protected function printStackTrace(OutputInterface $output, array $trace) |
80
|
|
|
{ |
81
|
|
|
$rootFolder = $this->getApplication()->getMagentoRootFolder(); |
82
|
|
|
$trace = array_filter($trace, function (&$row) use ($rootFolder) { |
83
|
|
|
if (!strstr($row['file'], $rootFolder)) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$row['file'] = ltrim(str_replace($rootFolder, '', $row['file']), '/'); |
88
|
|
|
|
89
|
|
|
return $row; |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$table = $this->getHelper('table'); |
93
|
|
|
$rows = array(); |
94
|
|
|
$i = 1; |
95
|
|
|
foreach ($trace as $row) { |
96
|
|
|
$rows[] = array( |
97
|
|
|
$i++, |
98
|
|
|
$row['file'] . ':' . $row['line'], |
99
|
|
|
$row['class'] . '::' . $row['function'], |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
$table->setHeaders(array('#', 'File/Line', 'Method')); |
103
|
|
|
$table->setRows($rows); |
104
|
|
|
|
105
|
|
|
$table->render($output); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param OutputInterface $output |
110
|
|
|
* @param $e |
111
|
|
|
*/ |
112
|
|
|
protected function printException(OutputInterface $output, $e) |
113
|
|
|
{ |
114
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param OutputInterface $output |
119
|
|
|
* @param $e |
120
|
|
|
*/ |
121
|
|
|
protected function printFile(OutputInterface $output, $e) |
122
|
|
|
{ |
123
|
|
|
if (preg_match('/Error\sin\sfile\:\s"(.+)\"\s-/', $e->getMessage(), $matches)) { |
124
|
|
|
$table = $this->getHelper('table'); |
125
|
|
|
$lines = \file($matches[1]); |
126
|
|
|
$rows = array(); |
127
|
|
|
$i = 0; |
128
|
|
|
foreach ($lines as $line) { |
129
|
|
|
$rows[] = array(++$i, rtrim($line)); |
130
|
|
|
} |
131
|
|
|
$table->setHeaders(array('Line', 'Code')); |
132
|
|
|
$table->setRows($rows); |
133
|
|
|
$table->render($output); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function flushCache() |
138
|
|
|
{ |
139
|
|
|
/** |
140
|
|
|
* Get events before cache flush command is called. |
141
|
|
|
*/ |
142
|
|
|
$reflectionApp = new \ReflectionObject(\Mage::app()); |
143
|
|
|
$appEventReflectionProperty = $reflectionApp->getProperty('_events'); |
144
|
|
|
$appEventReflectionProperty->setAccessible(true); |
145
|
|
|
$eventsBeforeCacheFlush = $appEventReflectionProperty->getValue(\Mage::app()); |
146
|
|
|
|
147
|
|
|
$this->getApplication()->run(new StringInput('cache:flush'), new NullOutput()); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Restore initially loaded events which was reset during setup script run |
151
|
|
|
*/ |
152
|
|
|
$appEventReflectionProperty->setValue(\Mage::app(), $eventsBeforeCacheFlush); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|