|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\SearchEngine; |
|
4
|
|
|
|
|
5
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
|
6
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class ListCommand extends AbstractMagentoCommand |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Magento\Search\Model\Adminhtml\System\Config\Source\Engine |
|
15
|
|
|
*/ |
|
16
|
|
|
private $searchEngineConfig; |
|
17
|
|
|
|
|
18
|
|
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
$this |
|
21
|
|
|
->setName('search:engine:list') |
|
22
|
|
|
->setDescription('Lists all registered search engines') |
|
23
|
|
|
->addOption( |
|
24
|
|
|
'format', |
|
25
|
|
|
null, |
|
26
|
|
|
InputOption::VALUE_OPTIONAL, |
|
27
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
|
28
|
|
|
) |
|
29
|
|
|
; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Magento\Search\Model\Adminhtml\System\Config\Source\Engine $searchEngineConfig |
|
34
|
|
|
*/ |
|
35
|
|
|
public function inject(\Magento\Search\Model\Adminhtml\System\Config\Source\Engine $searchEngineConfig) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->searchEngineConfig = $searchEngineConfig; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
42
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
43
|
|
|
* @return int|void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->detectMagento($output, true); |
|
48
|
|
|
if (!$this->initMagento()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$searchEngines = $this->searchEngineConfig->toOptionArray(); |
|
53
|
|
|
|
|
54
|
|
|
$table = array(); |
|
55
|
|
|
foreach ($searchEngines as $searchEngine) { |
|
56
|
|
|
$table[] = [ |
|
57
|
|
|
$searchEngine['value'], |
|
58
|
|
|
$searchEngine['label'], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
$this->getHelper('table') |
|
|
|
|
|
|
62
|
|
|
->setHeaders(array('code', 'label')) |
|
63
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
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 implementation 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 interface: