77namespace Magento \Developer \Console \Command ;
88
99use Magento \Developer \Model \Di \Information ;
10+ use Magento \Framework \ObjectManagerInterface ;
1011use Symfony \Component \Console \Command \Command ;
1112use Symfony \Component \Console \Exception \InvalidArgumentException ;
1213use Symfony \Component \Console \Helper \Table ;
1314use Symfony \Component \Console \Input \InputArgument ;
1415use Symfony \Component \Console \Input \InputInterface ;
1516use Symfony \Component \Console \Output \OutputInterface ;
17+ use Magento \Framework \App \AreaList ;
18+ use Magento \Framework \App \Area ;
1619
20+ /**
21+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+ */
1723class DiInfoCommand extends Command
1824{
25+ /**
26+ * @var ObjectManagerInterface
27+ */
28+ private ObjectManagerInterface $ objectManager ;
29+
1930 /**
2031 * Command name
2132 */
@@ -26,18 +37,34 @@ class DiInfoCommand extends Command
2637 */
2738 public const CLASS_NAME = 'class ' ;
2839
40+ /**
41+ * Area name
42+ */
43+ public const AREA_CODE = 'area ' ;
44+
2945 /**
3046 * @var Information
3147 */
32- private $ diInformation ;
48+ private Information $ diInformation ;
49+
50+ /**
51+ * @var AreaList
52+ */
53+ private AreaList $ areaList ;
3354
3455 /**
3556 * @param Information $diInformation
57+ * @param ObjectManagerInterface $objectManager
58+ * @param AreaList|null $areaList
3659 */
3760 public function __construct (
38- Information $ diInformation
61+ Information $ diInformation ,
62+ ObjectManagerInterface $ objectManager ,
63+ ?AreaList $ areaList = null
3964 ) {
4065 $ this ->diInformation = $ diInformation ;
66+ $ this ->objectManager = $ objectManager ;
67+ $ this ->areaList = $ areaList ?? \Magento \Framework \App \ObjectManager::getInstance ()->get (AreaList::class);
4168 parent ::__construct ();
4269 }
4370
@@ -49,10 +76,11 @@ public function __construct(
4976 protected function configure ()
5077 {
5178 $ 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+ ]);
5684
5785 parent ::configure ();
5886 }
@@ -154,10 +182,14 @@ private function printPlugins($className, $output, $label)
154182 */
155183 protected function execute (InputInterface $ input , OutputInterface $ output )
156184 {
185+ $ area = $ input ->getArgument (self ::AREA_CODE ) ?? Area::AREA_GLOBAL ;
186+ if ($ area !== Area::AREA_GLOBAL ) {
187+ $ this ->setDiArea ($ area );
188+ }
157189 $ className = $ input ->getArgument (self ::CLASS_NAME );
158190 $ output ->setDecorated (true );
159191 $ 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 ) ));
161193
162194 if ($ this ->diInformation ->isVirtualType ($ className )) {
163195 $ output ->writeln (
@@ -173,4 +205,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
173205
174206 return \Magento \Framework \Console \Cli::RETURN_SUCCESS ;
175207 }
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+ }
176243}
0 commit comments