|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Developer\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Framework\Module\ModuleListInterface; |
|
6
|
|
|
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure; |
|
7
|
|
|
use N98\Util\BinaryString; |
|
8
|
|
|
use Psy\VarDumper\Presenter; |
|
9
|
|
|
use Psy\VarDumper\PresenterAware; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ModuleCommand extends AbstractGeneratorCommand implements PresenterAware |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Presenter |
|
18
|
|
|
*/ |
|
19
|
|
|
private $presenter; |
|
20
|
|
|
|
|
21
|
|
|
protected function configure() |
|
22
|
|
|
{ |
|
23
|
|
|
$this |
|
24
|
|
|
->setName('module') |
|
25
|
|
|
->addArgument('module', InputArgument::OPTIONAL) |
|
26
|
|
|
->setAliases(['mod']) |
|
27
|
|
|
->setDescription('Set current module context'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* PresenterAware interface. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Presenter $presenter |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setPresenter(Presenter $presenter) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->presenter = $presenter; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param InputInterface $input |
|
42
|
|
|
* @param OutputInterface $output |
|
43
|
|
|
* |
|
44
|
|
|
* @return int|void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
$module = $input->getArgument('module'); |
|
49
|
|
|
|
|
50
|
|
|
if (!empty($module)) { |
|
51
|
|
|
$moduleName = new ModuleNameStructure($module); |
|
52
|
|
|
$this->setCurrentModuleContext($output, $moduleName); |
|
53
|
|
|
} else { |
|
54
|
|
|
try { |
|
55
|
|
|
$module = $this->getCurrentModuleName(); |
|
56
|
|
|
$output->writeln('<info>Current module </info><comment>' . $module . '</comment>'); |
|
57
|
|
|
} catch (\InvalidArgumentException $e) { |
|
58
|
|
|
$output->writeln('<info>No module context defined</info>'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param OutputInterface $output |
|
65
|
|
|
* @param ModuleNameStructure $moduleName |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function setCurrentModuleContext(OutputInterface $output, ModuleNameStructure $moduleName) |
|
68
|
|
|
{ |
|
69
|
|
|
$moduleList = $this->create(ModuleListInterface::class); |
|
70
|
|
|
/** @var $moduleList ModuleListInterface */ |
|
71
|
|
|
|
|
72
|
|
|
$detectedModule = $moduleList->getOne($moduleName->getFullModuleName()); |
|
73
|
|
|
|
|
74
|
|
|
if (is_array($detectedModule)) { |
|
75
|
|
|
$detectedModule = $detectedModule['name']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!$detectedModule) { |
|
79
|
|
|
// Try to load first matching module |
|
80
|
|
|
foreach ($moduleList->getAll() as $moduleListItem) { |
|
81
|
|
|
if (BinaryString::startsWith($moduleListItem['name'], $moduleName->getFullModuleName())) { |
|
82
|
|
|
$detectedModule = $moduleListItem['name']; |
|
83
|
|
|
break; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($detectedModule) { |
|
89
|
|
|
$this->setCurrentModuleName($detectedModule); |
|
90
|
|
|
$output->writeln('<info>Use module </info><comment>' . $detectedModule . '</comment>'); |
|
91
|
|
|
$this->getApplication()->setPrompt('Module: ' . $detectedModule . ' >>> '); |
|
|
|
|
|
|
92
|
|
|
} else { |
|
93
|
|
|
$output->writeln('<error>Invalid module</error>'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: