1 | <?php |
||
13 | class GetCommand extends AbstractConfigCommand |
||
14 | { |
||
15 | /** |
||
16 | * @var Collection |
||
17 | */ |
||
18 | private $collection; |
||
19 | |||
20 | protected function configure() |
||
21 | { |
||
22 | $this |
||
23 | ->setName('config:get') |
||
24 | ->setDescription('Get a core config item') |
||
25 | ->setHelp( |
||
26 | <<<EOT |
||
27 | If <info>path</info> is not set, all available config items will be listed. |
||
28 | The <info>path</info> may contain wildcards (*). |
||
29 | If <info>path</info> ends with a trailing slash, all child items will be listed. E.g. |
||
30 | |||
31 | config:get web/ |
||
32 | is the same as |
||
33 | config:get web/* |
||
34 | EOT |
||
35 | ) |
||
36 | ->addArgument('path', InputArgument::OPTIONAL, 'The config path') |
||
37 | ->addOption('scope', null, InputOption::VALUE_REQUIRED, 'The config value\'s scope') |
||
38 | ->addOption('scope-id', null, InputOption::VALUE_REQUIRED, 'The config value\'s scope ID') |
||
39 | ->addOption( |
||
40 | 'decrypt', |
||
41 | null, |
||
42 | InputOption::VALUE_NONE, |
||
43 | 'Decrypt the config value using local.xml\'s crypt key' |
||
44 | ) |
||
45 | ->addOption('update-script', null, InputOption::VALUE_NONE, 'Output as update script lines') |
||
46 | ->addOption('magerun-script', null, InputOption::VALUE_NONE, 'Output for usage with config:set') |
||
47 | ->addOption( |
||
48 | 'format', |
||
49 | null, |
||
50 | InputOption::VALUE_OPTIONAL, |
||
51 | 'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
||
52 | ); |
||
53 | |||
54 | $help = <<<HELP |
||
55 | If path is not set, all available config items will be listed. path may contain wildcards (*) |
||
56 | HELP; |
||
57 | $this->setHelp($help); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param Collection $collection |
||
62 | */ |
||
63 | public function inject(Collection $collection) |
||
67 | |||
68 | /** |
||
69 | * @param InputInterface $input |
||
70 | * @param OutputInterface $output |
||
71 | * @return int|void |
||
72 | */ |
||
73 | protected function execute(InputInterface $input, OutputInterface $output) |
||
74 | { |
||
75 | $collection = $this->collection; |
||
76 | |||
77 | $searchPath = $input->getArgument('path'); |
||
78 | |||
79 | if (substr($input->getArgument('path'), -1, 1) === '/') { |
||
80 | $searchPath .= '*'; |
||
81 | } |
||
82 | |||
83 | $collection->addFieldToFilter('path', array( |
||
84 | 'like' => str_replace('*', '%', $searchPath) |
||
85 | )); |
||
86 | |||
87 | if ($scopeId = $input->getOption('scope')) { |
||
88 | $collection->addFieldToFilter('scope', array('eq' => $scopeId)); |
||
89 | } |
||
90 | |||
91 | if ($scopeId = $input->getOption('scope-id')) { |
||
92 | $collection->addFieldToFilter( |
||
93 | 'scope_id', |
||
94 | array('eq' => $scopeId) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | $collection->addOrder('path', 'ASC'); |
||
99 | |||
100 | // sort according to the config overwrite order |
||
101 | // trick to force order default -> (f)website -> store , because f comes after d and before s |
||
102 | $collection->addOrder('REPLACE(scope, "website", "fwebsite")', 'ASC'); |
||
103 | |||
104 | $collection->addOrder('scope_id', 'ASC'); |
||
105 | |||
106 | if ($collection->count() == 0) { |
||
107 | $output->writeln(sprintf("Couldn't find a config value for \"%s\"", $input->getArgument('path'))); |
||
108 | |||
109 | return; |
||
110 | } |
||
111 | |||
112 | foreach ($collection as $item) { |
||
113 | $table[] = array( |
||
|
|||
114 | 'path' => $item->getPath(), |
||
115 | 'scope' => $item->getScope(), |
||
116 | 'scope_id' => $item->getScopeId(), |
||
117 | 'value' => $this->_formatValue( |
||
118 | $item->getValue(), |
||
119 | $input->getOption('decrypt') ? 'decrypt' : false |
||
120 | ), |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | ksort($table); |
||
125 | |||
126 | if ($input->getOption('update-script')) { |
||
127 | $this->renderAsUpdateScript($output, $table); |
||
128 | } elseif ($input->getOption('magerun-script')) { |
||
129 | $this->renderAsMagerunScript($output, $table); |
||
130 | } else { |
||
131 | $this->renderAsTable($output, $table, $input->getOption('format')); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param OutputInterface $output |
||
137 | * @param array $table |
||
138 | * @param string $format |
||
139 | */ |
||
140 | protected function renderAsTable(OutputInterface $output, $table, $format) |
||
156 | |||
157 | /** |
||
158 | * @param OutputInterface $output |
||
159 | * @param array $table |
||
160 | */ |
||
161 | protected function renderAsUpdateScript(OutputInterface $output, $table) |
||
189 | |||
190 | /** |
||
191 | * @param OutputInterface $output |
||
192 | * @param array $table |
||
193 | */ |
||
194 | protected function renderAsMagerunScript(OutputInterface $output, $table) |
||
208 | } |
||
209 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.