Skip to content

Commit ba35173

Browse files
Yevhen Miroshnychenkoshiftedreality
authored andcommitted
MAGECLOUD-1242: [Magento Cloud] - Base url after forking no longer seems to be update (#100)
1 parent 4735164 commit ba35173

File tree

9 files changed

+720
-101
lines changed

9 files changed

+720
-101
lines changed

src/App/Container.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Magento\MagentoCloud\Process\ConfigDump as ConfigDumpProcess;
2525
use Magento\MagentoCloud\Process\PostDeploy as PostDeployProcess;
2626
use Psr\Container\ContainerInterface;
27-
use Magento\MagentoCloud\Process as Process;
27+
use Magento\MagentoCloud\Process;
2828

2929
/**
3030
* @inheritdoc
@@ -199,6 +199,16 @@ function () use ($root, $config) {
199199
],
200200
]);
201201
});
202+
$this->container->when(DeployProcess\InstallUpdate\ConfigUpdate\Urls::class)
203+
->needs(ProcessInterface::class)
204+
->give(function () {
205+
return $this->container->makeWith(ProcessComposite::class, [
206+
'processes' => [
207+
$this->container->make(DeployProcess\InstallUpdate\ConfigUpdate\Urls\Database::class),
208+
$this->container->make(DeployProcess\InstallUpdate\ConfigUpdate\Urls\Environment::class),
209+
],
210+
]);
211+
});
202212
$this->container->when(ConfigDump::class)
203213
->needs(ProcessInterface::class)
204214
->give(function () {

src/Process/Deploy/InstallUpdate/ConfigUpdate/Urls.php

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
namespace Magento\MagentoCloud\Process\Deploy\InstallUpdate\ConfigUpdate;
77

88
use Magento\MagentoCloud\Config\Environment;
9-
use Magento\MagentoCloud\DB\ConnectionInterface;
109
use Magento\MagentoCloud\Process\ProcessInterface;
11-
use Magento\MagentoCloud\Util\UrlManager;
1210
use Psr\Log\LoggerInterface;
1311

1412
/**
@@ -22,83 +20,44 @@ class Urls implements ProcessInterface
2220
private $environment;
2321

2422
/**
25-
* @var ConnectionInterface
23+
* @var ProcessInterface
2624
*/
27-
private $connection;
25+
private $process;
2826

2927
/**
3028
* @var LoggerInterface
3129
*/
3230
private $logger;
3331

34-
/**
35-
* @var UrlManager
36-
*/
37-
private $urlManager;
38-
3932
/**
4033
* @param Environment $environment
41-
* @param ConnectionInterface $connection
34+
* @param ProcessInterface $process
4235
* @param LoggerInterface $logger
43-
* @param UrlManager $urlManager
4436
*/
4537
public function __construct(
4638
Environment $environment,
47-
ConnectionInterface $connection,
48-
LoggerInterface $logger,
49-
UrlManager $urlManager
39+
ProcessInterface $process,
40+
LoggerInterface $logger
5041
) {
5142
$this->environment = $environment;
52-
$this->connection = $connection;
43+
$this->process = $process;
5344
$this->logger = $logger;
54-
$this->urlManager = $urlManager;
5545
}
5646

5747
/**
5848
* @inheritdoc
5949
*/
6050
public function execute()
6151
{
62-
if (!$this->environment->isUpdateUrlsEnabled()) {
52+
if ($this->environment->isMasterBranch()
53+
|| !$this->environment->isUpdateUrlsEnabled() ) {
6354
$this->logger->info('Skipping URL updates');
6455

6556
return;
6657
}
6758

6859
$this->logger->info('Updating secure and unsecure URLs');
6960

70-
foreach ($this->urlManager->getUrls() as $urlType => $urls) {
71-
foreach ($urls as $route => $url) {
72-
$this->update($urlType, $url, $route);
73-
}
74-
}
75-
}
76-
77-
/**
78-
* Updates core config with new URLs.
79-
*
80-
* @param string $urlType
81-
* @param string $url
82-
* @param string $route
83-
*/
84-
private function update(string $urlType, string $url, string $route)
85-
{
86-
$prefix = 'unsecure' === $urlType ? UrlManager::PREFIX_UNSECURE : UrlManager::PREFIX_SECURE;
87-
if (!strlen($route)) {
88-
// @codingStandardsIgnoreStart
89-
$this->connection->affectingQuery(
90-
"UPDATE `core_config_data` SET `value` = '$url' WHERE `path` = 'web/$urlType/base_url' AND `scope_id` = '0'"
91-
);
92-
93-
// @codingStandardsIgnoreEnd
94-
return;
95-
}
96-
$likeKey = $prefix . $route . '%';
97-
$likeKeyParsed = $prefix . str_replace('.', '---', $route) . '%';
98-
// @codingStandardsIgnoreStart
99-
$this->connection->affectingQuery(
100-
"UPDATE `core_config_data` SET `value` = '$url' WHERE `path` = 'web/$urlType/base_url' AND (`value` LIKE '$likeKey' OR `value` LIKE '$likeKeyParsed')"
101-
);
102-
// @codingStandardsIgnoreEnd
61+
$this->process->execute();
10362
}
10463
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Process\Deploy\InstallUpdate\ConfigUpdate\Urls;
7+
8+
use Magento\MagentoCloud\Config\Environment;
9+
use Magento\MagentoCloud\DB\ConnectionInterface;
10+
use Magento\MagentoCloud\Process\ProcessInterface;
11+
use Magento\MagentoCloud\Util\UrlManager;
12+
use Psr\Log\LoggerInterface;
13+
14+
/**
15+
* Updates the base_url configuration in the `core_config_data` table
16+
*
17+
* {@inheritdoc}
18+
*/
19+
class Database implements ProcessInterface
20+
{
21+
/**
22+
* @var Environment
23+
*/
24+
private $environment;
25+
26+
/**
27+
* @var ConnectionInterface
28+
*/
29+
private $connection;
30+
31+
/**
32+
* @var LoggerInterface
33+
*/
34+
private $logger;
35+
36+
/**
37+
* @var UrlManager
38+
*/
39+
private $urlManager;
40+
41+
/**
42+
* @param Environment $environment
43+
* @param ConnectionInterface $connection
44+
* @param LoggerInterface $logger
45+
* @param UrlManager $urlManager
46+
*/
47+
public function __construct(
48+
Environment $environment,
49+
ConnectionInterface $connection,
50+
LoggerInterface $logger,
51+
UrlManager $urlManager
52+
) {
53+
$this->environment = $environment;
54+
$this->connection = $connection;
55+
$this->logger = $logger;
56+
$this->urlManager = $urlManager;
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public function execute()
63+
{
64+
$this->logger->info('Updating secure and unsecure URLs in core_config_data table.');
65+
66+
$baseUrls = $this->getBaseUrls();
67+
68+
foreach ($this->urlManager->getUrls() as $typeUrl => $actualUrl) {
69+
if (empty($actualUrl['']) || empty($baseUrls[$typeUrl])) {
70+
continue;
71+
}
72+
$baseHost = parse_url($baseUrls[$typeUrl], PHP_URL_HOST);
73+
$actualHost = parse_url($actualUrl[''], PHP_URL_HOST);
74+
75+
if ($baseHost === $actualHost) {
76+
continue;
77+
}
78+
79+
$changedRowsCount = $this->updateUrl($baseHost, $actualHost);
80+
81+
if (!$changedRowsCount) {
82+
continue;
83+
}
84+
$this->logger->info(sprintf('Host was replaced: [%s] => [%s]', $baseHost, $actualHost));
85+
}
86+
}
87+
88+
/**
89+
* Returns the base_url configuration from `core_config_data` table.
90+
*
91+
* ```php
92+
* array(
93+
* 'secure' => 'https://example.com',
94+
* 'unsecure' => 'http://example.com',
95+
* )
96+
* ```
97+
* @return array
98+
*/
99+
private function getBaseUrls(): array
100+
{
101+
$configBaseUrls = $this->connection->select(
102+
'SELECT `value`, `path` FROM `core_config_data` WHERE (`path`=? OR `path`= ?) AND `scope_id` = ?',
103+
[
104+
'web/unsecure/base_url',
105+
'web/secure/base_url',
106+
0,
107+
]
108+
);
109+
$result = [];
110+
foreach ($configBaseUrls as $configBaseUrl) {
111+
$key = $configBaseUrl['path'] === 'web/secure/base_url' ? 'secure' : 'unsecure';
112+
$result[$key] = $configBaseUrl['value'];
113+
}
114+
115+
return $result;
116+
}
117+
118+
/**
119+
* Updates the base_url configuration in the `core_config_data` table.
120+
*
121+
* @param $baseHost
122+
* @param $actualHost
123+
*
124+
* @return int Returns the number of updated URLs
125+
*/
126+
private function updateUrl(string $baseHost, string $actualHost): int
127+
{
128+
return $this->connection->affectingQuery(
129+
'UPDATE `core_config_data` SET `value` = REPLACE(`value`, ?, ?) WHERE `value` LIKE ?',
130+
[
131+
$baseHost,
132+
$actualHost,
133+
'%' . $baseHost . '%',
134+
]
135+
);
136+
}
137+
}

0 commit comments

Comments
 (0)