Skip to content

Commit 4be9829

Browse files
authored
MAGECLOUD-4168: Provide an Option to Specify Custom URL for Magento (#163)
1 parent 7c7bdea commit 4be9829

File tree

13 files changed

+170
-44
lines changed

13 files changed

+170
-44
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ git:
66
addons:
77
hosts:
88
- magento2.docker
9+
- magento2.test
910

1011
services:
1112
- docker

src/Command/BuildCompose.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ protected function configure(): void
204204
null,
205205
InputOption::VALUE_OPTIONAL,
206206
'Cloud environment variables'
207+
)
208+
->addOption(
209+
Source\CliSource::OPTION_HOST,
210+
null,
211+
InputOption::VALUE_OPTIONAL,
212+
'Host name'
213+
)
214+
->addOption(
215+
Source\CliSource::OPTION_PORT,
216+
null,
217+
InputOption::VALUE_OPTIONAL,
218+
'Port'
207219
);
208220

209221
parent::configure();

src/Compose/Manager.php

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

88
namespace Magento\CloudDocker\Compose;
99

10+
use Magento\CloudDocker\Config\Config;
11+
1012
/**
1113
* Compose configuration manager
1214
*/
1315
class Manager
1416
{
15-
public const DOMAIN = 'magento2.docker';
16-
1717
/**
1818
* @var string
1919
*/
@@ -34,6 +34,19 @@ class Manager
3434
*/
3535
private $volumes = [];
3636

37+
/**
38+
* @var Config
39+
*/
40+
private $config;
41+
42+
/**
43+
* @param Config $config
44+
*/
45+
public function __construct(Config $config)
46+
{
47+
$this->config = $config;
48+
}
49+
3750
/**
3851
* @param string $name
3952
* @param array $extConfig
@@ -42,7 +55,7 @@ class Manager
4255
*/
4356
public function addService(string $name, array $extConfig, array $networks, array $depends): void
4457
{
45-
$hostname = $name . '.' . self::DOMAIN;
58+
$hostname = $name . '.' . $this->config->getHost();
4659

4760
$config = [
4861
'hostname' => $hostname,
@@ -89,16 +102,6 @@ public function addVolume(string $name, array $config): void
89102
$this->volumes[$name] = $config;
90103
}
91104

92-
/**
93-
* @param array $volumes
94-
*/
95-
public function addVolumes(array $volumes): void
96-
{
97-
foreach ($volumes as $name => $config) {
98-
$this->volumes[$name] = $config;
99-
}
100-
}
101-
102105
/**
103106
* @param array $volumes
104107
*/

src/Compose/ManagerFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77

88
namespace Magento\CloudDocker\Compose;
99

10+
use Magento\CloudDocker\Config\Config;
11+
use Magento\CloudDocker\Config\Source\SourceInterface;
12+
1013
/**
1114
* Creates instance of Manager
1215
*
1316
* @see Manager
1417
*/
1518
class ManagerFactory
1619
{
17-
public function create(): Manager
20+
/**
21+
* Creates instance of Manager
22+
*
23+
* @param Config $config
24+
* @return Manager
25+
* @throws \Magento\CloudDocker\App\ConfigurationMismatchException
26+
*/
27+
public function create(Config $config): Manager
1828
{
19-
return new Manager();
29+
return new Manager($config);
2030
}
2131
}

src/Compose/ProductionBuilder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Magento\CloudDocker\Compose;
99

10+
use Magento\CloudDocker\App\ConfigurationMismatchException;
1011
use Magento\CloudDocker\Compose\Php\ExtensionResolver;
1112
use Magento\CloudDocker\Compose\ProductionBuilder\VolumeResolver;
1213
use Magento\CloudDocker\Config\Config;
1314
use Magento\CloudDocker\Config\Environment\Converter;
14-
use Magento\CloudDocker\App\ConfigurationMismatchException;
1515
use Magento\CloudDocker\Filesystem\FileList;
1616
use Magento\CloudDocker\Service\ServiceFactory;
1717
use Magento\CloudDocker\Service\ServiceInterface;
@@ -134,7 +134,7 @@ public function __construct(
134134
*/
135135
public function build(Config $config): Manager
136136
{
137-
$manager = $this->managerFactory->create();
137+
$manager = $this->managerFactory->create($config);
138138

139139
$phpVersion = $config->getServiceVersion(ServiceInterface::SERVICE_PHP);
140140
$dbVersion = $config->getServiceVersion(ServiceInterface::SERVICE_DB);
@@ -292,10 +292,13 @@ public function build(Config $config): Manager
292292
[
293293
'volumes' => $volumesRo,
294294
'environment' => [
295-
'VIRTUAL_HOST=magento2.docker',
295+
'VIRTUAL_HOST=' . $config->getHost(),
296296
'VIRTUAL_PORT=80',
297297
'HTTPS_METHOD=noredirect',
298298
'WITH_XDEBUG=' . (int)$config->hasServiceEnabled(ServiceInterface::SERVICE_FPM_XDEBUG)
299+
],
300+
'ports' => [
301+
$config->getPort() . ':80'
299302
]
300303
]
301304
),
@@ -312,7 +315,7 @@ public function build(Config $config): Manager
312315
[
313316
'networks' => [
314317
self::NETWORK_MAGENTO => [
315-
'aliases' => [Manager::DOMAIN]
318+
'aliases' => [$config->getHost()]
316319
]
317320
]
318321
]

src/Config/Config.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\CloudDocker\Compose\BuilderFactory;
1313
use Magento\CloudDocker\Compose\DeveloperBuilder;
1414
use Magento\CloudDocker\Compose\ProductionBuilder;
15-
use Magento\CloudDocker\Config\Source\CliSource;
1615
use Magento\CloudDocker\Config\Source\SourceException;
1716
use Magento\CloudDocker\Config\Source\SourceInterface;
1817
use Magento\CloudDocker\Service\ServiceInterface;
@@ -292,4 +291,24 @@ public function getVariables(): array
292291

293292
return $config->get(SourceInterface::VARIABLES);
294293
}
294+
295+
/**
296+
* Returns host value or default if host not set
297+
*
298+
* @return string
299+
*/
300+
public function getHost(): string
301+
{
302+
return $this->get(SourceInterface::CONFIG_HOST);
303+
}
304+
305+
/**
306+
* Returns port value or default if port not set
307+
*
308+
* @return string
309+
*/
310+
public function getPort(): string
311+
{
312+
return $this->get(SourceInterface::CONFIG_PORT);
313+
}
295314
}

src/Config/Dist/Generator.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
namespace Magento\CloudDocker\Config\Dist;
99

1010
use Magento\CloudDocker\App\ConfigurationMismatchException;
11+
use Magento\CloudDocker\Compose\Manager;
1112
use Magento\CloudDocker\Config\Config;
1213
use Magento\CloudDocker\Config\Relationship;
14+
use Magento\CloudDocker\Config\Source\BaseSource;
15+
use Magento\CloudDocker\Config\Source\SourceInterface;
1316
use Magento\CloudDocker\Filesystem\DirectoryList;
1417
use Magento\CloudDocker\Filesystem\Filesystem;
1518
use Magento\CloudDocker\Config\Environment\Shared\Reader as EnvReader;
@@ -51,27 +54,6 @@ class Generator
5154
*/
5255
private $envCoder;
5356

54-
/**
55-
* @var array
56-
*/
57-
private static $baseConfig = [
58-
'MAGENTO_CLOUD_ROUTES' => [
59-
'http://magento2.docker/' => [
60-
'type' => 'upstream',
61-
'original_url' => 'http://{default}'
62-
],
63-
'https://magento2.docker/' => [
64-
'type' => 'upstream',
65-
'original_url' => 'https://{default}'
66-
],
67-
],
68-
'MAGENTO_CLOUD_VARIABLES' => [
69-
'ADMIN_EMAIL' => '[email protected]',
70-
'ADMIN_PASSWORD' => '123123q',
71-
'ADMIN_URL' => 'admin'
72-
],
73-
];
74-
7557
/**
7658
* @param DirectoryList $directoryList
7759
* @param Filesystem $filesystem
@@ -131,7 +113,7 @@ private function generateByServices(Config $config): array
131113
{
132114
return array_merge(
133115
['MAGENTO_CLOUD_RELATIONSHIPS' => $this->relationship->get($config)],
134-
self::$baseConfig
116+
$this->getBaseConfig($config)
135117
);
136118
}
137119

@@ -168,4 +150,39 @@ private function saveConfigEnv(string $filePath, array $config): void
168150

169151
$this->filesystem->put($filePath, $result);
170152
}
153+
154+
/**
155+
* Returns base configuration
156+
*
157+
* @param Config $config
158+
* @return array
159+
* @throws ConfigurationMismatchException
160+
*/
161+
private function getBaseConfig(Config $config): array
162+
{
163+
$host = $config->getHost();
164+
$port = $config->getPort();
165+
166+
if (!empty($port) && $port != BaseSource::DEFAULT_PORT) {
167+
$host .= ':' . $port;
168+
}
169+
170+
return [
171+
'MAGENTO_CLOUD_ROUTES' => [
172+
sprintf('http://%s/', $host) => [
173+
'type' => 'upstream',
174+
'original_url' => 'http://{default}'
175+
],
176+
sprintf('https://%s/', $host) => [
177+
'type' => 'upstream',
178+
'original_url' => 'https://{default}'
179+
],
180+
],
181+
'MAGENTO_CLOUD_VARIABLES' => [
182+
'ADMIN_EMAIL' => '[email protected]',
183+
'ADMIN_PASSWORD' => '123123q',
184+
'ADMIN_URL' => 'admin'
185+
],
186+
];
187+
}
171188
}

src/Config/Source/BaseSource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
class BaseSource implements SourceInterface
1919
{
20+
public const DEFAULT_HOST = 'magento2.docker';
21+
public const DEFAULT_PORT = '80';
22+
2023
/**
2124
* @var EnvReader
2225
*/
@@ -41,6 +44,8 @@ public function read(): Repository
4144
self::SYSTEM_MODE => BuilderFactory::BUILDER_PRODUCTION,
4245
self::SYSTEM_SYNC_ENGINE => null,
4346
self::CRON_ENABLED => false,
47+
self::CONFIG_PORT => self::DEFAULT_PORT,
48+
self::CONFIG_HOST => self::DEFAULT_HOST,
4449
]);
4550

4651
try {

src/Config/Source/CliSource.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class CliSource implements SourceInterface
4848
*/
4949
public const OPTION_ENV_VARIABLES = 'env-vars';
5050

51+
/**
52+
* Host configuration
53+
*/
54+
public const OPTION_HOST = 'host';
55+
public const OPTION_PORT = 'port';
56+
5157
/**
5258
* Option key to config name map
5359
*
@@ -149,8 +155,16 @@ public function read(): Repository
149155
$repository->set(self::VARIABLES, (array) json_decode($envs, true));
150156
}
151157

152-
if ($port = $this->input->getOption(self::OPTION_EXPOSE_DB_PORT)) {
153-
$repository->set(self::SYSTEM_EXPOSE_DB_PORTS, $port);
158+
if ($dbPort = $this->input->getOption(self::OPTION_EXPOSE_DB_PORT)) {
159+
$repository->set(self::SYSTEM_EXPOSE_DB_PORTS, $dbPort);
160+
}
161+
162+
if ($host = $this->input->getOption(self::OPTION_HOST)) {
163+
$repository->set(self::CONFIG_HOST, $host);
164+
}
165+
166+
if ($port = $this->input->getOption(self::OPTION_PORT)) {
167+
$repository->set(self::CONFIG_PORT, $port);
154168
}
155169

156170
return $repository;

src/Config/Source/SourceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ interface SourceInterface
9494
public const SYSTEM_SYNC_ENGINE = 'system.sync_engine';
9595
public const SYSTEM_TMP_MOUNTS = 'system.tmp_mounts';
9696
public const SYSTEM_MODE = 'system.mode';
97+
public const CONFIG_HOST = 'config.host';
98+
public const CONFIG_PORT = 'config.port';
9799
public const SYSTEM_EXPOSE_DB_PORTS = 'system.expose_db_ports';
98100

99101
public const VARIABLES = 'variables';

0 commit comments

Comments
 (0)