7
7
namespace Magento \Developer \Console \Command ;
8
8
9
9
use Magento \Developer \Model \Di \Information ;
10
+ use Magento \Framework \App \ObjectManager ;
10
11
use Symfony \Component \Console \Command \Command ;
11
12
use Symfony \Component \Console \Exception \InvalidArgumentException ;
12
13
use Symfony \Component \Console \Input \InputInterface ;
13
14
use Symfony \Component \Console \Output \OutputInterface ;
14
15
use Symfony \Component \Console \Input \InputArgument ;
15
16
use Symfony \Component \Console \Helper \Table ;
17
+ use Magento \Framework \App \AreaList ;
16
18
17
19
class DiInfoCommand extends Command
18
20
{
19
- /**
20
- * Command name
21
- */
22
- const COMMAND_NAME = 'dev:di:info ' ;
21
+ public const COMMAND_NAME = 'dev:di:info ' ;
22
+
23
+ public const CLASS_NAME = 'class ' ;
24
+
25
+ public const AREA_CODE = 'area ' ;
23
26
24
27
/**
25
- * input name
28
+ * @var Information
26
29
*/
27
- const CLASS_NAME = ' class ' ;
30
+ private Information $ diInformation ;
28
31
29
32
/**
30
- * @var Information
33
+ * @var AreaList
31
34
*/
32
- private $ diInformation ;
35
+ private AreaList $ areaList ;
33
36
34
37
/**
35
38
* @param Information $diInformation
39
+ * @param AreaList $areaList
36
40
*/
37
41
public function __construct (
38
- Information $ diInformation
42
+ Information $ diInformation ,
43
+ AreaList $ areaList
39
44
) {
45
+ $ this ->areaList = $ areaList ;
40
46
$ this ->diInformation = $ diInformation ;
41
47
parent ::__construct ();
42
48
}
43
49
44
50
/**
45
- * {@inheritdoc}
51
+ * @inheritdoc
52
+ *
46
53
* @throws InvalidArgumentException
47
54
*/
48
55
protected function configure ()
49
56
{
50
57
$ this ->setName (self ::COMMAND_NAME )
51
- ->setDescription ('Provides information on Dependency Injection configuration for the Command. ' )
52
- ->setDefinition ([
53
- new InputArgument (self ::CLASS_NAME , InputArgument::REQUIRED , 'Class name ' )
54
- ]);
58
+ ->setDescription ('Provides information on Dependency Injection configuration for the Command. ' )
59
+ ->setDefinition ([
60
+ new InputArgument (self ::CLASS_NAME , InputArgument::REQUIRED , 'Class name ' ),
61
+ new InputArgument (self ::AREA_CODE , InputArgument::OPTIONAL , 'Area Code ' )
62
+ ]);
55
63
56
64
parent ::configure ();
57
65
}
@@ -146,15 +154,19 @@ private function printPlugins($className, $output, $label)
146
154
}
147
155
148
156
/**
149
- * { @inheritdoc}
157
+ * @inheritdoc
150
158
* @throws \InvalidArgumentException
151
159
*/
152
160
protected function execute (InputInterface $ input , OutputInterface $ output )
153
161
{
162
+ $ area = 'GLOBAL ' ;
163
+ if ($ area = $ input ->getArgument (self ::AREA_CODE )) {
164
+ $ this ->setDiArea ($ area );
165
+ }
154
166
$ className = $ input ->getArgument (self ::CLASS_NAME );
155
167
$ output ->setDecorated (true );
156
168
$ output ->writeln ('' );
157
- $ output ->writeln (sprintf ('DI configuration for the class %s in the GLOBAL area ' , $ className ));
169
+ $ output ->writeln (sprintf ('DI configuration for the class %s in the %s area ' , $ className, strtoupper ( $ area ) ));
158
170
159
171
if ($ this ->diInformation ->isVirtualType ($ className )) {
160
172
$ output ->writeln (
@@ -170,4 +182,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
170
182
171
183
return \Magento \Framework \Console \Cli::RETURN_SUCCESS ;
172
184
}
185
+
186
+ /**
187
+ * Set Area for DI Configuration
188
+ *
189
+ * @param string $area
190
+ * @return void
191
+ * @throws \InvalidArgumentException
192
+ */
193
+ private function setDiArea ($ area )
194
+ {
195
+ if ($ this ->validateAreaCodeFromInput ($ area )) {
196
+ $ objectManager = ObjectManager::getInstance ();
197
+
198
+ $ objectManager ->configure (
199
+ $ objectManager
200
+ ->get (\Magento \Framework \App \ObjectManager \ConfigLoader::class)
201
+ ->load ($ area )
202
+ );
203
+ $ objectManager ->get (\Magento \Framework \Config \ScopeInterface::class)
204
+ ->setCurrentScope ($ area );
205
+ } else {
206
+ throw new InvalidArgumentException (sprintf ('The "%s" area code does not exist ' , $ area ));
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Validate Input
212
+ *
213
+ * @param string $area
214
+ * @return bool
215
+ */
216
+ private function validateAreaCodeFromInput ($ area )
217
+ {
218
+ $ availableAreaCodes = $ this ->areaList ->getCodes ();
219
+ return in_array ($ area , $ availableAreaCodes , true );
220
+ }
173
221
}
0 commit comments