55namespace Loki \Components \Controller \Index ;
66
77use Exception ;
8+ use Loki \Components \Component \ComponentInterface ;
9+ use Loki \Components \Component \ComponentRegistry ;
810use Magento \Framework \App \Action \HttpGetActionInterface ;
911use Magento \Framework \App \Action \HttpPostActionInterface ;
1012use Magento \Framework \App \ResponseInterface ;
@@ -38,7 +40,8 @@ public function __construct(
3840 private readonly MessageManager $ messageManager ,
3941 private readonly Config $ config ,
4042 private readonly AppState $ appState ,
41- private readonly LoggerInterface $ logger
43+ private readonly LoggerInterface $ logger ,
44+ private readonly ComponentRegistry $ componentRegistry
4245 ) {
4346 }
4447
@@ -48,10 +51,14 @@ public function execute(): ResultInterface|ResponseInterface
4851 $ this ->requestDataLoader ->mergeRequestParams ();
4952 $ layout = $ this ->layoutLoader ->load ($ data ['handles ' ]);
5053
51- foreach ($ data ['updates ' ] as $ update ) {
54+ $ updates = $ this ->enrichUpdates ($ data ['updates ' ], $ layout );
55+ $ updates = $ this ->sortUpdates ($ updates );
56+
57+ foreach ($ updates as $ update ) {
58+ echo "UPDATE: " .$ update ['blockName ' ]."\n" ;
5259 try {
5360 $ this ->repositoryDispatcher ->dispatch (
54- $ this -> getBlock ( $ layout , $ update ['blockName ' ]) ,
61+ $ update ['component ' ] ,
5562 $ update ['update ' ]
5663 );
5764 } catch (NoBlockFoundException $ exception ) {
@@ -81,6 +88,31 @@ public function execute(): ResultInterface|ResponseInterface
8188 return $ this ->getHtmlResult ($ htmlParts );
8289 }
8390
91+ private function enrichUpdates (array $ updates , LayoutInterface $ layout ): array
92+ {
93+ foreach ($ updates as $ updateIndex => $ update ) {
94+ $ update ['block ' ] = $ this ->getBlock ($ layout , $ update ['blockName ' ]);
95+ $ update ['component ' ] = $ this ->componentRegistry ->getComponentFromBlock ($ update ['block ' ]);
96+ $ updates [$ updateIndex ] = $ update ;
97+ }
98+
99+ return $ updates ;
100+ }
101+
102+ private function sortUpdates (array $ updates ): array
103+ {
104+ usort ($ updates , function (array $ a , array $ b ) {
105+ /** @var ComponentInterface $componentA */
106+ /** @var ComponentInterface $componentB */
107+ $ componentA = $ a ['component ' ];
108+ $ componentB = $ b ['component ' ];
109+
110+ return $ componentA ->getRepository ()?->getPriority() <=> $ componentB ->getRepository ()?->getPriority();
111+ });
112+
113+ return $ updates ;
114+ }
115+
84116 private function getBlock (LayoutInterface $ layout , string $ blockName ): AbstractBlock
85117 {
86118 if ($ blockName === '' || $ blockName === '0 ' ) {
@@ -116,6 +148,7 @@ private function getJsonRedirect(string $redirectUrl): JsonResult
116148 ]);
117149
118150 $ json ->setHeader ('X-Loki-Redirect ' , $ redirectUrl );
151+
119152 // @todo: Make sure messages sent to frontend are remembered
120153
121154 return $ json ;
0 commit comments