1 | <?php |
||
14 | class ConflictsCommand extends AbstractRewriteCommand |
||
15 | { |
||
16 | protected function configure() |
||
17 | { |
||
18 | $this |
||
19 | ->setName('dev:module:rewrite:conflicts') |
||
20 | ->addOption('log-junit', null, InputOption::VALUE_REQUIRED, |
||
21 | 'Log conflicts in JUnit XML format to defined file.') |
||
22 | ->setDescription('Lists all magento rewrite conflicts'); |
||
23 | |||
24 | $help = <<<HELP |
||
25 | Lists all duplicated rewrites and tells you which class is loaded by Magento. |
||
26 | The command checks class inheritance in order of your module dependencies. |
||
27 | |||
28 | * If a filename with `--log-junit` option is set the tool generates an XML file and no output to *stdout*. |
||
29 | |||
30 | Exit status is 0 if no conflicts were found, 1 if conflicts were found and 2 if there was a problem to |
||
31 | initialize Magento. |
||
32 | HELP; |
||
33 | $this->setHelp($help); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param InputInterface $input |
||
38 | * @param OutputInterface $output |
||
39 | * |
||
40 | * @return int exit code: 0 no conflicts found, 1 conflicts found, 2 magento could not be initialized |
||
41 | */ |
||
42 | protected function execute(InputInterface $input, OutputInterface $output) |
||
43 | { |
||
44 | $this->detectMagento($output, true); |
||
45 | if (!$this->initMagento()) { |
||
46 | return 2; |
||
47 | } |
||
48 | |||
49 | $conflicts = array(); |
||
50 | $time = microtime(true); |
||
51 | $rewrites = $this->loadRewrites(); |
||
52 | |||
53 | foreach ($rewrites as $type => $data) { |
||
54 | if (!is_array($data)) { |
||
55 | continue; |
||
56 | } |
||
57 | foreach ($data as $class => $rewriteClasses) { |
||
58 | if (!$this->_isInheritanceConflict($rewriteClasses)) { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | $conflicts[] = array( |
||
63 | 'Type' => $type, |
||
64 | 'Class' => $class, |
||
65 | 'Rewrites' => implode(', ', $rewriteClasses), |
||
66 | 'Loaded Class' => $this->_getLoadedClass($type, $class), |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if ($input->getOption('log-junit')) { |
||
72 | $duration = microtime($time) - $time; |
||
73 | $this->logJUnit($conflicts, $input->getOption('log-junit'), $duration); |
||
74 | } else { |
||
75 | $this->writeOutput($output, $conflicts); |
||
76 | } |
||
77 | |||
78 | return (int) (bool) $conflicts; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns loaded class by type like models or blocks |
||
83 | * |
||
84 | * @param string $type |
||
85 | * @param string $class |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function _getLoadedClass($type, $class) |
||
103 | |||
104 | /** |
||
105 | * @param array $conflicts |
||
106 | * @param string $filename |
||
107 | * @param float $duration |
||
108 | */ |
||
109 | protected function logJUnit(array $conflicts, $filename, $duration) |
||
130 | |||
131 | /** |
||
132 | * Check if rewritten class has inherited the parent class. |
||
133 | * If yes we have no conflict. The top class can extend every core class. |
||
134 | * So we cannot check this. |
||
135 | * |
||
136 | * @var array $classes |
||
137 | * @return bool |
||
138 | */ |
||
139 | protected function _isInheritanceConflict($classes) |
||
161 | |||
162 | /** |
||
163 | * @param OutputInterface $output |
||
164 | * @param array $conflicts |
||
165 | * @return int |
||
166 | */ |
||
167 | private function writeOutput(OutputInterface $output, array $conflicts) |
||
185 | } |
||
186 |