Completed
Push — master ( 4630f9...56ee91 )
by Tom
04:14
created

RunCommand::printException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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