55namespace Loki \Components \Controller \Adminhtml \Index ;
66
77use Exception ;
8+ use Loki \Components \Component \ComponentInterface ;
9+ use Loki \Components \Component \ComponentRegistry ;
810use Magento \Backend \App \Action ;
911use Magento \Backend \App \Action \Context ;
10- use Magento \Framework \App \Action \HttpGetActionInterface ;
11- use Magento \Framework \App \Action \HttpPostActionInterface ;
1212use Magento \Framework \App \ResponseInterface ;
1313use Magento \Framework \App \State as AppState ;
1414use Magento \Framework \Controller \Result \Json as JsonResult ;
@@ -43,6 +43,7 @@ public function __construct(
4343 private readonly Config $ config ,
4444 private readonly AppState $ appState ,
4545 private readonly LoggerInterface $ logger ,
46+ private readonly ComponentRegistry $ componentRegistry ,
4647 Context $ context ,
4748 ) {
4849 parent ::__construct ($ context );
@@ -54,10 +55,13 @@ public function execute(): ResultInterface|ResponseInterface
5455 $ this ->requestDataLoader ->mergeRequestParams ();
5556 $ layout = $ this ->layoutLoader ->load ($ data ['handles ' ]);
5657
58+ $ updates = $ this ->enrichUpdates ($ data ['updates ' ], $ layout );
59+ $ updates = $ this ->sortUpdates ($ updates );
60+
5761 foreach ($ data ['updates ' ] as $ update ) {
5862 try {
5963 $ this ->repositoryDispatcher ->dispatch (
60- $ this -> getBlock ( $ layout , $ update ['blockName ' ]) ,
64+ $ update ['component ' ] ,
6165 $ update ['update ' ]
6266 );
6367 } catch (NoBlockFoundException $ exception ) {
@@ -75,7 +79,8 @@ public function execute(): ResultInterface|ResponseInterface
7579 if ($ this ->allowRendering ($ data )) {
7680 try {
7781 $ htmlParts = $ this ->targetRenderer ->render (
78- $ layout , $ data ['targets ' ]
82+ $ layout ,
83+ $ data ['targets ' ]
7984 );
8085 } catch (RedirectException $ redirectException ) {
8186 return $ this ->getJsonRedirect ($ redirectException ->getMessage ());
@@ -88,8 +93,33 @@ public function execute(): ResultInterface|ResponseInterface
8893 return $ this ->getHtmlResult ($ htmlParts );
8994 }
9095
91- private function getBlock (LayoutInterface $ layout , string $ blockName
92- ): AbstractBlock {
96+ private function enrichUpdates (array $ updates , LayoutInterface $ layout ): array
97+ {
98+ foreach ($ updates as $ updateIndex => $ update ) {
99+ $ update ['block ' ] = $ this ->getBlock ($ layout , $ update ['blockName ' ]);
100+ $ update ['component ' ] = $ this ->componentRegistry ->getComponentFromBlock ($ update ['block ' ]);
101+ $ updates [$ updateIndex ] = $ update ;
102+ }
103+
104+ return $ updates ;
105+ }
106+
107+ private function sortUpdates (array $ updates ): array
108+ {
109+ usort ($ updates , function (array $ a , array $ b ) {
110+ /** @var ComponentInterface $componentA */
111+ $ componentA = $ a ['component ' ];
112+ /** @var ComponentInterface $componentB */
113+ $ componentB = $ b ['component ' ];
114+
115+ return $ componentA ->getRepository ()?->getPriority() <=> $ componentB ->getRepository ()?->getPriority();
116+ });
117+
118+ return $ updates ;
119+ }
120+
121+ private function getBlock (LayoutInterface $ layout , string $ blockName ): AbstractBlock
122+ {
93123 if ($ blockName === '' || $ blockName === '0 ' ) {
94124 throw new NoBlockFoundException ('Block name not specified ' );
95125 }
0 commit comments