|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Config\Env; |
|
4
|
|
|
|
|
5
|
|
|
use Adbar\Dot; |
|
6
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
|
7
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
|
13
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ShowCommand |
|
17
|
|
|
* @package N98\Magento\Command\Config\Env |
|
18
|
|
|
*/ |
|
19
|
|
|
class ShowCommand extends AbstractMagentoCommand |
|
20
|
|
|
{ |
|
21
|
|
|
protected function configure() |
|
22
|
|
|
{ |
|
23
|
|
|
$this |
|
24
|
|
|
->setName('config:env:show') |
|
25
|
|
|
->setDescription('List env.php file') |
|
26
|
|
|
->addArgument('key', InputArgument::OPTIONAL, 'Key to show.') |
|
27
|
|
|
->addOption( |
|
28
|
|
|
'format', |
|
29
|
|
|
null, |
|
30
|
|
|
InputOption::VALUE_OPTIONAL, |
|
31
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
37
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
38
|
|
|
* @return int|void |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->detectMagento($output); |
|
44
|
|
|
|
|
45
|
|
|
$envFilePath = $this->getApplication()->getMagentoRootFolder() . '/app/etc/env.php'; |
|
46
|
|
|
|
|
47
|
|
|
if (!file_exists($envFilePath)) { |
|
48
|
|
|
throw new \RuntimeException('env.php file does not exist.'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$keyToShow = $input->getArgument('key'); |
|
52
|
|
|
|
|
53
|
|
|
$envConfig = include $envFilePath; |
|
54
|
|
|
$env = new Dot($envConfig); |
|
55
|
|
|
|
|
56
|
|
|
$cloner = new VarCloner(); |
|
57
|
|
|
$cloner->setMaxItems(-1); |
|
58
|
|
|
$cloner->setMaxString(-1); |
|
59
|
|
|
$dumper = new CliDumper(); |
|
60
|
|
|
$dumper->setColors(true); |
|
61
|
|
|
|
|
62
|
|
|
$flattenArray = $env->flatten(); |
|
63
|
|
|
|
|
64
|
|
|
ksort($flattenArray); |
|
65
|
|
|
|
|
66
|
|
|
if ($keyToShow !== null) { |
|
67
|
|
|
if (!isset($flattenArray[$keyToShow])) { |
|
68
|
|
|
throw new \InvalidArgumentException('Unknown key: ' . $keyToShow); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$output->writeln($flattenArray[$keyToShow]); |
|
72
|
|
|
} else { |
|
73
|
|
|
$table =[]; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($flattenArray as $configKey => $configValue) { |
|
76
|
|
|
$table[] = [ |
|
77
|
|
|
'key' => $configKey, |
|
78
|
|
|
'value' => $configValue, |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->getHelper('table') |
|
|
|
|
|
|
83
|
|
|
->setHeaders(['key', 'value']) |
|
84
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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: