Skip to content

Commit a4f919b

Browse files
committed
Add component repository repository so when bulking, country is saved first
1 parent edce62e commit a4f919b

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

Component/ComponentRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ protected function getContext(): ComponentContextInterface
4949
{
5050
return $this->component->getContext();
5151
}
52+
53+
public function getPriority(): int
54+
{
55+
return 0;
56+
}
5257
}

Component/ComponentRepositoryInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public function saveValue(mixed $value): void;
1111

1212
public function getValue(): mixed;
1313
public function getDefaultValue(): mixed;
14+
public function getPriority(): int;
1415

1516
}

Controller/Index/Html.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Loki\Components\Controller\Index;
66

77
use Exception;
8+
use Loki\Components\Component\ComponentInterface;
9+
use Loki\Components\Component\ComponentRegistry;
810
use Magento\Framework\App\Action\HttpGetActionInterface;
911
use Magento\Framework\App\Action\HttpPostActionInterface;
1012
use 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;

Util/Controller/RepositoryDispatcher.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
namespace Loki\Components\Util\Controller;
44

5+
use Loki\Components\Component\ComponentInterface;
56
use Magento\Framework\Event\Manager as EventManager;
6-
use Magento\Framework\View\Element\AbstractBlock;
7-
use Loki\Components\Component\ComponentRegistry;
87
use Loki\Components\Component\ComponentRepositoryInterface;
98
use Loki\Components\Filter\Filter;
109
use Loki\Components\Validator\Validator;
1110

1211
class RepositoryDispatcher
1312
{
1413
public function __construct(
15-
private readonly ComponentRegistry $componentRegistry,
1614
private readonly EventManager $eventManager,
1715
private readonly Filter $filter,
1816
private readonly Validator $validator,
1917
) {
2018
}
2119

22-
public function dispatch(AbstractBlock $block, mixed $componentData): void
20+
public function dispatch(ComponentInterface $component, mixed $componentData): void
2321
{
24-
$component = $this->componentRegistry->getComponentFromBlock($block);
2522
$this->eventManager->dispatch('loki_components_repository_dispatch', ['component' => $component]);
2623

2724
$repository = $component->getRepository();

0 commit comments

Comments
 (0)