Skip to content

Commit 792d864

Browse files
committed
wip: Rewrite to composable AbstractComponentContext
1 parent 7534f3d commit 792d864

3 files changed

Lines changed: 70 additions & 16 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\Components\Component;
5+
6+
use Magento\Framework\Exception\LocalizedException;
7+
use Magento\Framework\Phrase;
8+
9+
class AbstractComponentContext implements ComponentContextInterface
10+
{
11+
public function __construct(
12+
protected array $dependencies = []
13+
) {
14+
}
15+
16+
public function hasMethod(string $methodName)
17+
{
18+
if (str_starts_with($methodName, 'get')) {
19+
$objectName = lcfirst(substr($methodName, 3));
20+
if (array_key_exists($objectName, $this->dependencies)) {
21+
return true;
22+
}
23+
}
24+
25+
return false;
26+
}
27+
28+
public function __call(string $methodName, array $arguments)
29+
{
30+
$objectName = lcfirst(substr($methodName, 3));
31+
if (str_starts_with($methodName, 'get')) {
32+
if (array_key_exists($objectName, $this->dependencies)) {
33+
return $this->dependencies[$objectName];
34+
}
35+
}
36+
37+
if (isset($this->dependencies['parentContext'])) {
38+
$parentContext = $this->dependencies['parentContext'];
39+
return $parentContext->{$methodName}(...$arguments);
40+
}
41+
42+
throw new LocalizedException(
43+
new Phrase(
44+
'Invalid method %1::%2. No object "%3": %4',
45+
[
46+
get_class($this),
47+
$methodName,
48+
$objectName,
49+
implode(', ', array_keys($this->dependencies)),
50+
]
51+
)
52+
);
53+
}
54+
}

Component/ComponentContext.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,15 @@
33

44
namespace Loki\Components\Component;
55

6-
use Magento\Customer\Model\Session as CustomerSession;
7-
use Magento\Framework\UrlFactory;
8-
9-
class ComponentContext implements ComponentContextInterface
6+
/**
7+
* @method \Magento\Customer\Model\Session getCustomerSession()
8+
* @method \Magento\Framework\UrlFactory getUrlFactory()
9+
*/
10+
class ComponentContext extends AbstractComponentContext
1011
{
1112
public function __construct(
12-
private readonly CustomerSession $customerSession,
13-
private readonly UrlFactory $urlFactory,
13+
protected array $dependencies = []
1414
) {
15-
}
16-
17-
public function getCustomerSession(): CustomerSession
18-
{
19-
return $this->customerSession;
20-
}
21-
22-
public function getUrlFactory(): UrlFactory
23-
{
24-
return $this->urlFactory;
15+
parent::__construct($dependencies);
2516
}
2617
}

etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,13 @@
110110
type="Loki\Components\Plugin\AddComponentViewModelToRendererPlugin"
111111
/>
112112
</type>
113+
114+
<type name="Loki\Components\Component\ComponentContext">
115+
<arguments>
116+
<argument name="dependencies" xsi:type="array">
117+
<item name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</item>
118+
<item name="urlFactory" xsi:type="object">Magento\Framework\UrlFactory\Proxy</item>
119+
</argument>
120+
</arguments>
121+
</type>
113122
</config>

0 commit comments

Comments
 (0)