Skip to content

Commit cffe292

Browse files
PWA-1738 Ability to run Luma and PWA Studio on same instance (#11)
* PWA-1738 changing prerender configurations to website scope, validating config value before changing area, changing config path to website scope * PWA-1738 adding descriptive comment of how to disable, adjusting readme * PWA-1738 changing scope of upward config to store, reverting exposing config to admin, adding documentation how to set value on store scope * PWA-1738 Adding new entry to env.php to manage upward path, adding model for new entry, adding test coverage, refactor to use new model instead of scope config * PWA-1738 adding CLI command to set value, adding check on scope type, update README * PWA-1738 fixing coding standards * PWA-1738 changing new param to backwards compatible, adding missing property to test * PWA-1738 fixing error message in test * PWA-1738 fixing double error message
1 parent b10dbc8 commit cffe292

File tree

12 files changed

+721
-59
lines changed

12 files changed

+721
-59
lines changed

Api/UpwardPathManagerInterface.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Api;
10+
11+
interface UpwardPathManagerInterface
12+
{
13+
/** @var string Path to the upward yaml in the deployment config */
14+
public const PARAM_PATH_CONFIG = 'pwa_path';
15+
16+
public const SCOPE_CODE_DEAULT = 'default';
17+
public const SCOPE_DEFAULT = 'default';
18+
public const SCOPE_WEBSITE = 'website';
19+
public const SCOPE_STORE = 'store';
20+
21+
/**
22+
* Get the configured upward yaml paths
23+
*
24+
* @return string[]
25+
*/
26+
public function getPaths(): array;
27+
28+
/**
29+
* Get current store's configured value
30+
*
31+
* @return string|null
32+
*/
33+
public function getPath(): ?string;
34+
35+
/**
36+
* Set the upward yaml path in the deployment config
37+
*
38+
* @param string|null $path
39+
* @param string|null $scopeType
40+
* @param string|null $scopeCode
41+
*
42+
* @return \Magento\UpwardConnector\Api\UpwardPathManagerInterface
43+
*/
44+
public function setPath(
45+
?string $path,
46+
?string $scopeType = self::SCOPE_DEFAULT,
47+
?string $scopeCode = self::SCOPE_CODE_DEAULT
48+
): UpwardPathManagerInterface;
49+
50+
/**
51+
* Get available scope types
52+
*
53+
* @return string[]
54+
*/
55+
public function getScopeTypes(): array;
56+
}

Console/Command/UpwardPathCommand.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\UpwardConnector\Console\Command;
8+
9+
use Magento\UpwardConnector\Api\UpwardPathManagerInterface;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Command\Command;
14+
15+
/**
16+
* Class UpwardPathCommand
17+
*
18+
* Command for setting the UPWARD yaml config path
19+
*/
20+
class UpwardPathCommand extends Command
21+
{
22+
private const UPWARD_PATH = 'path';
23+
private const SCOPE_TYPE = 'scopeType';
24+
private const SCOPE_CODE = 'scopeCode';
25+
26+
/** @var \Magento\UpwardConnector\Api\UpwardPathManagerInterface */
27+
private $pathManager;
28+
29+
/**
30+
* @param \Magento\UpwardConnector\Api\UpwardPathManagerInterface $pathManager
31+
*/
32+
public function __construct(
33+
UpwardPathManagerInterface $pathManager
34+
) {
35+
$this->pathManager = $pathManager;
36+
parent::__construct();
37+
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
protected function configure()
43+
{
44+
$this->setName('pwa:upward:set')
45+
->setDescription('Sets the path to the UPWARD yaml file');
46+
47+
$this->addOption(
48+
self::UPWARD_PATH,
49+
'p',
50+
InputOption::VALUE_OPTIONAL,
51+
'UPWARD yaml path',
52+
''
53+
);
54+
55+
$this->addOption(
56+
self::SCOPE_TYPE,
57+
's',
58+
InputOption::VALUE_OPTIONAL,
59+
'Scope type <' . implode(',', $this->pathManager->getScopeTypes()) . '>'
60+
);
61+
62+
$this->addOption(
63+
self::SCOPE_CODE,
64+
'c',
65+
InputOption::VALUE_OPTIONAL,
66+
'Scope Code (website or store view code)'
67+
);
68+
69+
parent::configure();
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
protected function execute(InputInterface $input, OutputInterface $output)
76+
{
77+
try {
78+
$path = $input->getOption(self::UPWARD_PATH);
79+
$scopeType = $input->getOption(self::SCOPE_TYPE);
80+
$scopeCode = $input->getOption(self::SCOPE_CODE);
81+
82+
if ($scopeType && !$scopeCode) {
83+
$output->writeln('<error>Please enter a valid scope code</error>');
84+
85+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
86+
}
87+
88+
if ($scopeType) {
89+
$this->pathManager->setPath($path, $scopeType, $scopeCode);
90+
} else {
91+
$this->pathManager->setPath($path);
92+
}
93+
94+
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
95+
} catch (\Exception $e) {
96+
$output->writeln('<error>' . $e->getMessage() . '</error>');
97+
98+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
99+
}
100+
}
101+
}

Controller/UpwardControllerFactory.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,33 @@
77

88
namespace Magento\UpwardConnector\Controller;
99

10-
use Magento\Framework\App\Config\ScopeConfigInterface;
1110
use Magento\Framework\App\RequestInterface;
1211
use Magento\Framework\ObjectManagerInterface;
1312
use Magento\Upward\Controller as UpwardController;
13+
use Magento\UpwardConnector\Api\UpwardPathManagerInterface;
1414

1515
class UpwardControllerFactory
1616
{
1717
/**
18-
* Config path where UPWARD config file path is found.
19-
*/
20-
public const UPWARD_CONFIG_PATH = 'web/upward/path';
21-
22-
/**
23-
* @var ScopeConfigInterface
18+
* @var ObjectManagerInterface
2419
*/
25-
private $config;
20+
private $objectManager;
2621

2722
/**
28-
* @var ObjectManagerInterface
23+
* @var \Magento\UpwardConnector\Api\UpwardPathManagerInterface
2924
*/
30-
private $objectManager;
25+
private $pathManager;
3126

3227
/**
33-
* @param ObjectManagerInterface $objectManager
34-
* @param ScopeConfigInterface $scopeConfig
28+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
29+
* @param \Magento\UpwardConnector\Api\UpwardPathManagerInterface $pathManager
3530
*/
36-
public function __construct(ObjectManagerInterface $objectManager, ScopeConfigInterface $scopeConfig)
37-
{
31+
public function __construct(
32+
ObjectManagerInterface $objectManager,
33+
UpwardPathManagerInterface $pathManager
34+
) {
3835
$this->objectManager = $objectManager;
39-
$this->config = $scopeConfig;
36+
$this->pathManager = $pathManager;
4037
}
4138

4239
/**
@@ -48,10 +45,7 @@ public function __construct(ObjectManagerInterface $objectManager, ScopeConfigIn
4845
*/
4946
public function create(RequestInterface $request): UpwardController
5047
{
51-
$upwardConfig = $this->config->getValue(
52-
static::UPWARD_CONFIG_PATH,
53-
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
54-
);
48+
$upwardConfig = $this->pathManager->getPath();
5549

5650
if (empty($upwardConfig)) {
5751
throw new \RuntimeException('Path to UPWARD configuration file not set.');

Model/Prerender.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function getPrerenderedPageResponse(RequestInterface $request)
121121
public function shouldShowPrerenderedPage(RequestInterface $request)
122122
{
123123
if (!$this->getPrerenderUrl() ||
124-
!$this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER, scopeInterface::SCOPE_STORE)
124+
!$this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER, ScopeInterface::SCOPE_STORE)
125125
) {
126126
return false;
127127
}
@@ -156,7 +156,7 @@ public function shouldShowPrerenderedPage(RequestInterface $request)
156156
*/
157157
private function getPrerenderToken()
158158
{
159-
return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_TOKEN);
159+
return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_TOKEN, ScopeInterface::SCOPE_WEBSITE);
160160
}
161161

162162
/**
@@ -166,7 +166,7 @@ private function getPrerenderToken()
166166
*/
167167
private function getPrerenderUrl()
168168
{
169-
return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_URL);
169+
return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_URL, ScopeInterface::SCOPE_WEBSITE);
170170
}
171171

172172
/**
@@ -190,7 +190,7 @@ private function isCrawlerUserAgent(RequestInterface $request)
190190
}
191191

192192
$crawlerUserAgents = $this->getList(
193-
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_CRAWLERS)
193+
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_CRAWLERS, ScopeInterface::SCOPE_WEBSITE)
194194
);
195195

196196
foreach ($crawlerUserAgents as $crawlerUserAgent) {
@@ -211,7 +211,7 @@ private function isCrawlerUserAgent(RequestInterface $request)
211211
private function isInAllowedList(string $requestUri)
212212
{
213213
$allowedList = $this->getList(
214-
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_ALLOWED_LIST)
214+
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_ALLOWED_LIST, ScopeInterface::SCOPE_WEBSITE)
215215
);
216216

217217
return empty($allowedList) || $this->isListed([$requestUri], $allowedList);
@@ -226,7 +226,7 @@ private function isInAllowedList(string $requestUri)
226226
private function isInBlockedList(array $uris)
227227
{
228228
$blockedList = $this->getList(
229-
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_BLOCKED_LIST)
229+
(string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_BLOCKED_LIST, ScopeInterface::SCOPE_WEBSITE)
230230
);
231231

232232
return !empty($blockedList) && $this->isListed($uris, $blockedList);

0 commit comments

Comments
 (0)