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