Skip to content

Commit 8703dd2

Browse files
committed
Add new PHP Attribute JsProperty for usage in ComponentViewModels
1 parent f7c3f33 commit 8703dd2

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

Attribute/JsProperty.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Attribute;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_METHOD)]
8+
final class JsProperty
9+
{
10+
public function __construct(
11+
public readonly string $name,
12+
) {
13+
}
14+
}

Component/Component.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Loki\Components\Component;
55

66
use Loki\Components\Util\Ajax;
7+
use Loki\Components\Util\JsPropertyResolver;
78
use Magento\Framework\ObjectManagerInterface;
89
use Magento\Framework\View\Element\AbstractBlock;
910
use Magento\Framework\View\LayoutInterface;
@@ -26,7 +27,8 @@ public function __construct(
2627
protected LayoutInterface $layout,
2728
protected ComponentContextInterface $context,
2829
protected GlobalMessageRegistry $globalMessageRegistry,
29-
LocalMessageRegistryFactory $localMessageRegistryFactory,
30+
protected LocalMessageRegistryFactory $localMessageRegistryFactory,
31+
protected JsPropertyResolver $jsPropertyResolver,
3032
protected Validator $validator,
3133
protected Filter $filter,
3234
protected Ajax $ajax,
@@ -37,7 +39,6 @@ public function __construct(
3739
protected ?string $repositoryClass = null,
3840
protected ?string $viewModelClass = null,
3941
) {
40-
$this->localMessageRegistry = $localMessageRegistryFactory->create();
4142
}
4243

4344
public function getName(): string
@@ -172,6 +173,10 @@ public function getGlobalMessageRegistry(): GlobalMessageRegistry
172173

173174
public function getLocalMessageRegistry(): LocalMessageRegistry
174175
{
176+
if (false === $this->localMessageRegistry instanceof LocalMessageRegistry) {
177+
$this->localMessageRegistry = $this->localMessageRegistryFactory->create();
178+
}
179+
175180
return $this->localMessageRegistry;
176181
}
177182

@@ -202,4 +207,9 @@ public function getLayout(): LayoutInterface
202207
{
203208
return $this->layout;
204209
}
210+
211+
public function getJsPropertyResolver(): JsPropertyResolver
212+
{
213+
return $this->jsPropertyResolver;
214+
}
205215
}

Component/ComponentViewModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Loki\Components\Component;
55

66
use Loki\Components\Util\Block\GetElementId;
7+
use Loki\Components\Util\JsPropertyResolver;
78
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\View\Element\AbstractBlock;
910
use Loki\Components\Filter\Filter;
@@ -228,9 +229,11 @@ public function isVisible(): bool
228229
public function getJsData(): array
229230
{
230231
$jsDataFromBlock = (array)$this->getBlock()->getJsData();
232+
$jsProperties = $this->getComponent()->getJsPropertyResolver()->getJsProperties($this);
231233

232234
return [
233235
...(array)$jsDataFromBlock,
236+
...$jsProperties,
234237
'lazyLoad' => $this->isLazyLoad(),
235238
'lazyUpdate' => $this->isLazyUpdate(),
236239
'lazyUpdateTimeout' => $this->getLazyUpdateTimeout(),

Util/JsPropertyResolver.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Util;
4+
5+
use Loki\Components\Attribute\JsProperty;
6+
use Loki\Components\Component\ComponentViewModelInterface;
7+
use ReflectionClass;
8+
9+
class JsPropertyResolver
10+
{
11+
public function getJsProperties(ComponentViewModelInterface $componentViewModel): array
12+
{
13+
$reflection = new ReflectionClass($componentViewModel::class);
14+
$jsProperties = [];
15+
16+
foreach ($reflection->getMethods() as $method) {
17+
$attributes = $method->getAttributes(JsProperty::class);
18+
foreach ($attributes as $attribute) {
19+
$instance = $attribute->newInstance();
20+
$jsProperties[$instance->name] = $method->invoke($componentViewModel);
21+
}
22+
}
23+
24+
return $jsProperties;
25+
}
26+
}

0 commit comments

Comments
 (0)