Skip to content

Commit af7f3a0

Browse files
PWA-2343 moving computed resolver into connector, passing additional resolvers to UPWARD (#16)
1 parent cffe292 commit af7f3a0

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

Controller/UpwardControllerFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\ObjectManagerInterface;
1212
use Magento\Upward\Controller as UpwardController;
1313
use Magento\UpwardConnector\Api\UpwardPathManagerInterface;
14+
use Magento\UpwardConnector\Resolver\Computed;
1415

1516
class UpwardControllerFactory
1617
{
@@ -51,6 +52,17 @@ public function create(RequestInterface $request): UpwardController
5152
throw new \RuntimeException('Path to UPWARD configuration file not set.');
5253
}
5354

54-
return $this->objectManager->create(UpwardController::class, compact('request', 'upwardConfig'));
55+
$additionalResolvers = [
56+
Computed::RESOLVER_TYPE => Computed::class
57+
];
58+
59+
return $this->objectManager->create(
60+
UpwardController::class,
61+
compact(
62+
'request',
63+
'upwardConfig',
64+
'additionalResolvers'
65+
)
66+
);
5567
}
5668
}

Resolver/Computed.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\UpwardConnector\Resolver;
10+
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Upward\Definition;
13+
use Magento\Upward\Resolver\AbstractResolver;
14+
use Magento\UpwardConnector\Api\ComputedInterface;
15+
use Magento\UpwardConnector\Model\ComputedPool;
16+
17+
class Computed extends AbstractResolver
18+
{
19+
public const RESOLVER_TYPE = 'computed';
20+
21+
/** @var \Magento\UpwardConnector\Model\ComputedPool */
22+
private $computed;
23+
24+
public function __construct()
25+
{
26+
$this->computed = ObjectManager::getInstance()
27+
->get(ComputedPool::class);
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getIndicator(): string
34+
{
35+
return 'computed';
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function isValid(Definition $definition): bool
42+
{
43+
return $definition->has('type');
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function resolve($definition)
50+
{
51+
$computeType = $this->getIterator()->get('type', $definition);
52+
$computeResolver = $this->computed->getItem($computeType);
53+
if (!($computeResolver instanceof ComputedInterface)) {
54+
throw new \RuntimeException(sprintf(
55+
'Compute definition %s is not valid.',
56+
$computeType
57+
));
58+
}
59+
60+
return $computeResolver->resolve($this->getIterator(), $definition);
61+
}
62+
}

0 commit comments

Comments
 (0)