1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace N98\Magento\Application\Console\EventSubscriber; |
7
|
|
|
|
8
|
|
|
use Magento\Developer\Console\Command\XmlCatalogGenerateCommand; |
9
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
10
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
11
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DevUrnCatalogAutoPath |
16
|
|
|
* |
17
|
|
|
* Comfort option to automatically set path to ".idea/misc.xml |
18
|
|
|
* |
19
|
|
|
* @package N98\Magento\Application\Console\EventSubscriber |
20
|
|
|
*/ |
21
|
|
|
class DevUrnCatalogAutoPath implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
25
|
|
|
* |
26
|
|
|
* @return array The event names to listen to |
27
|
|
|
* |
28
|
|
|
* @api |
29
|
|
|
*/ |
30
|
|
|
public static function getSubscribedEvents() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
ConsoleEvents::COMMAND => 'autosetIdeaMiscXmlPath', |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Display a warning if a running n98-magerun as root user |
39
|
|
|
* |
40
|
|
|
* @param ConsoleCommandEvent $event |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function autosetIdeaMiscXmlPath(ConsoleCommandEvent $event) |
45
|
|
|
{ |
46
|
|
|
if (!$event->getCommand() instanceof XmlCatalogGenerateCommand) { |
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$input = clone $event->getInput(); |
51
|
|
|
if (!$input instanceof ArgvInput) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$command = clone $event->getCommand(); |
56
|
|
|
|
57
|
|
|
$input->bind($command->getDefinition()); |
58
|
|
|
|
59
|
|
|
$path = null; |
60
|
|
|
try { |
61
|
|
|
$path = $input->getArgument('path'); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ('dev:urn-catalog:generate' !== $path) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$file = $this->detectFile($event); |
71
|
|
|
if (null === $file) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$event->getOutput()->writeln("<info>automatically setting path to <comment>'$file'</comment></info>"); |
76
|
|
|
$this->addToken($event->getInput(), $file); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function detectFile(ConsoleCommandEvent $event) |
80
|
|
|
{ |
81
|
|
|
/** @var \N98\Magento\Application $app */ |
82
|
|
|
$app = $event->getCommand()->getApplication(); |
83
|
|
|
|
84
|
|
|
$root = $app->getMagentoRootFolder(); |
85
|
|
|
$down = 1; |
86
|
|
|
do { |
87
|
|
|
if (is_dir($root . '/.idea')) { |
88
|
|
|
return $root . '/.idea/misc.xml'; |
89
|
|
|
} |
90
|
|
|
$root .= '/..'; |
91
|
|
|
} while (is_dir($root) && $down--); |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ArgvInput $arg |
98
|
|
|
* @param string $file |
99
|
|
|
*/ |
100
|
|
|
private function addToken(ArgvInput $arg, $file) |
101
|
|
|
{ |
102
|
|
|
$refl = new \ReflectionObject($arg); |
103
|
|
|
$prop = $refl->getProperty('tokens'); |
104
|
|
|
$prop->setAccessible(true); |
105
|
|
|
$tokens = $prop->getValue($arg); |
106
|
|
|
$tokens[] = $file; |
107
|
|
|
$prop->setValue($arg, $tokens); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.