7
7
namespace Magento \Developer \Console \Command ;
8
8
9
9
use Magento \Developer \Model \Di \Information ;
10
+ use Magento \Framework \ObjectManagerInterface ;
10
11
use Symfony \Component \Console \Command \Command ;
11
12
use Symfony \Component \Console \Exception \InvalidArgumentException ;
12
13
use Symfony \Component \Console \Helper \Table ;
13
14
use Symfony \Component \Console \Input \InputArgument ;
14
15
use Symfony \Component \Console \Input \InputInterface ;
15
16
use Symfony \Component \Console \Output \OutputInterface ;
17
+ use Magento \Framework \App \AreaList ;
18
+ use Magento \Framework \App \Area ;
16
19
20
+ /**
21
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
+ */
17
23
class DiInfoCommand extends Command
18
24
{
25
+ /**
26
+ * @var ObjectManagerInterface
27
+ */
28
+ private ObjectManagerInterface $ objectManager ;
29
+
19
30
/**
20
31
* Command name
21
32
*/
@@ -26,18 +37,34 @@ class DiInfoCommand extends Command
26
37
*/
27
38
public const CLASS_NAME = 'class ' ;
28
39
40
+ /**
41
+ * Area name
42
+ */
43
+ public const AREA_CODE = 'area ' ;
44
+
29
45
/**
30
46
* @var Information
31
47
*/
32
- private $ diInformation ;
48
+ private Information $ diInformation ;
49
+
50
+ /**
51
+ * @var AreaList
52
+ */
53
+ private AreaList $ areaList ;
33
54
34
55
/**
35
56
* @param Information $diInformation
57
+ * @param ObjectManagerInterface $objectManager
58
+ * @param AreaList|null $areaList
36
59
*/
37
60
public function __construct (
38
- Information $ diInformation
61
+ Information $ diInformation ,
62
+ ObjectManagerInterface $ objectManager ,
63
+ ?AreaList $ areaList = null
39
64
) {
40
65
$ this ->diInformation = $ diInformation ;
66
+ $ this ->objectManager = $ objectManager ;
67
+ $ this ->areaList = $ areaList ?? \Magento \Framework \App \ObjectManager::getInstance ()->get (AreaList::class);
41
68
parent ::__construct ();
42
69
}
43
70
@@ -49,10 +76,11 @@ public function __construct(
49
76
protected function configure ()
50
77
{
51
78
$ this ->setName (self ::COMMAND_NAME )
52
- ->setDescription ('Provides information on Dependency Injection configuration for the Command. ' )
53
- ->setDefinition ([
54
- new InputArgument (self ::CLASS_NAME , InputArgument::REQUIRED , 'Class name ' )
55
- ]);
79
+ ->setDescription ('Provides information on Dependency Injection configuration for the Command. ' )
80
+ ->setDefinition ([
81
+ new InputArgument (self ::CLASS_NAME , InputArgument::REQUIRED , 'Class name ' ),
82
+ new InputArgument (self ::AREA_CODE , InputArgument::OPTIONAL , 'Area Code ' )
83
+ ]);
56
84
57
85
parent ::configure ();
58
86
}
@@ -154,10 +182,14 @@ private function printPlugins($className, $output, $label)
154
182
*/
155
183
protected function execute (InputInterface $ input , OutputInterface $ output )
156
184
{
185
+ $ area = $ input ->getArgument (self ::AREA_CODE ) ?? Area::AREA_GLOBAL ;
186
+ if ($ area !== Area::AREA_GLOBAL ) {
187
+ $ this ->setDiArea ($ area );
188
+ }
157
189
$ className = $ input ->getArgument (self ::CLASS_NAME );
158
190
$ output ->setDecorated (true );
159
191
$ output ->writeln ('' );
160
- $ output ->writeln (sprintf ('DI configuration for the class %s in the GLOBAL area ' , $ className ));
192
+ $ output ->writeln (sprintf ('DI configuration for the class %s in the %s area ' , $ className, strtoupper ( $ area ) ));
161
193
162
194
if ($ this ->diInformation ->isVirtualType ($ className )) {
163
195
$ output ->writeln (
@@ -173,4 +205,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
173
205
174
206
return \Magento \Framework \Console \Cli::RETURN_SUCCESS ;
175
207
}
208
+
209
+ /**
210
+ * Set Area for DI Configuration
211
+ *
212
+ * @param string $area
213
+ * @return void
214
+ * @throws \InvalidArgumentException
215
+ */
216
+ private function setDiArea (string $ area ): void
217
+ {
218
+ if ($ this ->validateAreaCodeFromInput ($ area )) {
219
+ $ areaOmConfiguration = $ this ->objectManager
220
+ ->get (\Magento \Framework \App \ObjectManager \ConfigLoader::class)
221
+ ->load ($ area );
222
+
223
+ $ this ->objectManager ->configure ($ areaOmConfiguration );
224
+
225
+ $ this ->objectManager ->get (\Magento \Framework \Config \ScopeInterface::class)
226
+ ->setCurrentScope ($ area );
227
+ } else {
228
+ throw new InvalidArgumentException (sprintf ('The "%s" area code does not exist ' , $ area ));
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Validate Input
234
+ *
235
+ * @param string $area
236
+ * @return bool
237
+ */
238
+ private function validateAreaCodeFromInput ($ area ): bool
239
+ {
240
+ $ availableAreaCodes = $ this ->areaList ->getCodes ();
241
+ return in_array ($ area , $ availableAreaCodes , true );
242
+ }
176
243
}
0 commit comments