Skip to content

Commit 10dacaa

Browse files
author
Stanislav Idolov
authored
ENGCOM-3291: Set cache id prefix on installation #18641
2 parents 1c253b8 + 18ad5eb commit 10dacaa

File tree

4 files changed

+182
-8
lines changed

4 files changed

+182
-8
lines changed

setup/src/Magento/Setup/Model/ConfigOptionsList/Cache.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ class Cache implements ConfigOptionsListInterface
2727
const INPUT_KEY_CACHE_BACKEND_REDIS_DATABASE = 'cache-backend-redis-db';
2828
const INPUT_KEY_CACHE_BACKEND_REDIS_PORT = 'cache-backend-redis-port';
2929
const INPUT_KEY_CACHE_BACKEND_REDIS_PASSWORD = 'cache-backend-redis-password';
30+
const INPUT_KEY_CACHE_ID_PREFIX = 'cache-id-prefix';
3031

3132
const CONFIG_PATH_CACHE_BACKEND = 'cache/frontend/default/backend';
3233
const CONFIG_PATH_CACHE_BACKEND_SERVER = 'cache/frontend/default/backend_options/server';
3334
const CONFIG_PATH_CACHE_BACKEND_DATABASE = 'cache/frontend/default/backend_options/database';
3435
const CONFIG_PATH_CACHE_BACKEND_PORT = 'cache/frontend/default/backend_options/port';
3536
const CONFIG_PATH_CACHE_BACKEND_PASSWORD = 'cache/frontend/default/backend_options/password';
37+
const CONFIG_PATH_CACHE_ID_PREFIX = 'cache/frontend/default/id_prefix';
3638

3739
/**
3840
* @var array
@@ -77,7 +79,7 @@ public function __construct(RedisConnectionValidator $redisValidator)
7779
}
7880

7981
/**
80-
* {@inheritdoc}
82+
* @inheritdoc
8183
*/
8284
public function getOptions()
8385
{
@@ -112,6 +114,12 @@ public function getOptions()
112114
TextConfigOption::FRONTEND_WIZARD_TEXT,
113115
self::CONFIG_PATH_CACHE_BACKEND_PASSWORD,
114116
'Redis server password'
117+
),
118+
new TextConfigOption(
119+
self::INPUT_KEY_CACHE_ID_PREFIX,
120+
TextConfigOption::FRONTEND_WIZARD_TEXT,
121+
self::CONFIG_PATH_CACHE_ID_PREFIX,
122+
'ID prefix for cache keys'
115123
)
116124
];
117125
}
@@ -122,6 +130,11 @@ public function getOptions()
122130
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
123131
{
124132
$configData = new ConfigData(ConfigFilePool::APP_ENV);
133+
if (isset($options[self::INPUT_KEY_CACHE_ID_PREFIX])) {
134+
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $options[self::INPUT_KEY_CACHE_ID_PREFIX]);
135+
} else {
136+
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $this->generateCachePrefix());
137+
}
125138

126139
if (isset($options[self::INPUT_KEY_CACHE_BACKEND])) {
127140
if ($options[self::INPUT_KEY_CACHE_BACKEND] == self::INPUT_VALUE_CACHE_REDIS) {
@@ -241,4 +254,14 @@ private function getDefaultConfigValue($inputKey)
241254
return '';
242255
}
243256
}
257+
258+
/**
259+
* Generate default cache ID prefix based on installation dir
260+
*
261+
* @return string
262+
*/
263+
private function generateCachePrefix(): string
264+
{
265+
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
266+
}
244267
}

setup/src/Magento/Setup/Model/ConfigOptionsList/PageCache.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class PageCache implements ConfigOptionsListInterface
2828
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PORT = 'page-cache-redis-port';
2929
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_COMPRESS_DATA = 'page-cache-redis-compress-data';
3030
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PASSWORD = 'page-cache-redis-password';
31+
const INPUT_KEY_PAGE_CACHE_ID_PREFIX = 'page-cache-id-prefix';
3132

3233
const CONFIG_PATH_PAGE_CACHE_BACKEND = 'cache/frontend/page_cache/backend';
3334
const CONFIG_PATH_PAGE_CACHE_BACKEND_SERVER = 'cache/frontend/page_cache/backend_options/server';
3435
const CONFIG_PATH_PAGE_CACHE_BACKEND_DATABASE = 'cache/frontend/page_cache/backend_options/database';
3536
const CONFIG_PATH_PAGE_CACHE_BACKEND_PORT = 'cache/frontend/page_cache/backend_options/port';
3637
const CONFIG_PATH_PAGE_CACHE_BACKEND_COMPRESS_DATA = 'cache/frontend/page_cache/backend_options/compress_data';
3738
const CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD = 'cache/frontend/page_cache/backend_options/password';
39+
const CONFIG_PATH_PAGE_CACHE_ID_PREFIX = 'cache/frontend/page_cache/id_prefix';
3840

3941
/**
4042
* @var array
@@ -81,7 +83,7 @@ public function __construct(RedisConnectionValidator $redisValidator)
8183
}
8284

8385
/**
84-
* {@inheritdoc}
86+
* @inheritdoc
8587
*/
8688
public function getOptions()
8789
{
@@ -122,6 +124,12 @@ public function getOptions()
122124
TextConfigOption::FRONTEND_WIZARD_TEXT,
123125
self::CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD,
124126
'Redis server password'
127+
),
128+
new TextConfigOption(
129+
self::INPUT_KEY_PAGE_CACHE_ID_PREFIX,
130+
TextConfigOption::FRONTEND_WIZARD_TEXT,
131+
self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX,
132+
'ID prefix for cache keys'
125133
)
126134
];
127135
}
@@ -132,6 +140,11 @@ public function getOptions()
132140
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
133141
{
134142
$configData = new ConfigData(ConfigFilePool::APP_ENV);
143+
if (isset($options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX])) {
144+
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX]);
145+
} else {
146+
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $this->generateCachePrefix());
147+
}
135148

136149
if (isset($options[self::INPUT_KEY_PAGE_CACHE_BACKEND])) {
137150
if ($options[self::INPUT_KEY_PAGE_CACHE_BACKEND] == self::INPUT_VALUE_PAGE_CACHE_REDIS) {
@@ -252,4 +265,14 @@ private function getDefaultConfigValue($inputKey)
252265
return '';
253266
}
254267
}
268+
269+
/**
270+
* Generate default cache ID prefix based on installation dir
271+
*
272+
* @return string
273+
*/
274+
private function generateCachePrefix(): string
275+
{
276+
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
277+
}
255278
}

setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/CacheTest.php

+67-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function setUp()
4545
public function testGetOptions()
4646
{
4747
$options = $this->configOptionsList->getOptions();
48-
$this->assertCount(5, $options);
48+
$this->assertCount(6, $options);
4949

5050
$this->assertArrayHasKey(0, $options);
5151
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
@@ -66,6 +66,10 @@ public function testGetOptions()
6666
$this->assertArrayHasKey(4, $options);
6767
$this->assertInstanceOf(TextConfigOption::class, $options[4]);
6868
$this->assertEquals('cache-backend-redis-password', $options[4]->getName());
69+
70+
$this->assertArrayHasKey(5, $options);
71+
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
72+
$this->assertEquals('cache-id-prefix', $options[5]->getName());
6973
}
7074

7175
/**
@@ -85,7 +89,8 @@ public function testCreateConfigCacheRedis()
8589
'port' => '',
8690
'database' => '',
8791
'password' => ''
88-
]
92+
],
93+
'id_prefix' => $this->expectedIdPrefix(),
8994
]
9095
]
9196
]
@@ -111,7 +116,8 @@ public function testCreateConfigWithRedisConfig()
111116
'port' => '1234',
112117
'database' => '5',
113118
'password' => ''
114-
]
119+
],
120+
'id_prefix' => $this->expectedIdPrefix(),
115121
]
116122
]
117123
]
@@ -128,6 +134,54 @@ public function testCreateConfigWithRedisConfig()
128134
$this->assertEquals($expectedConfigData, $configData->getData());
129135
}
130136

137+
/**
138+
* testCreateConfigCacheRedis
139+
*/
140+
public function testCreateConfigWithFileCache()
141+
{
142+
$this->deploymentConfigMock->method('get')->willReturn('');
143+
144+
$expectedConfigData = [
145+
'cache' => [
146+
'frontend' => [
147+
'default' => [
148+
'id_prefix' => $this->expectedIdPrefix(),
149+
]
150+
]
151+
]
152+
];
153+
154+
$configData = $this->configOptionsList->createConfig([], $this->deploymentConfigMock);
155+
156+
$this->assertEquals($expectedConfigData, $configData->getData());
157+
}
158+
159+
/**
160+
* testCreateConfigCacheRedis
161+
*/
162+
public function testCreateConfigWithIdPrefix()
163+
{
164+
$this->deploymentConfigMock->method('get')->willReturn('');
165+
166+
$explicitPrefix = 'XXX_';
167+
$expectedConfigData = [
168+
'cache' => [
169+
'frontend' => [
170+
'default' => [
171+
'id_prefix' => $explicitPrefix,
172+
]
173+
]
174+
]
175+
];
176+
177+
$configData = $this->configOptionsList->createConfig(
178+
['cache-id-prefix' => $explicitPrefix],
179+
$this->deploymentConfigMock
180+
);
181+
182+
$this->assertEquals($expectedConfigData, $configData->getData());
183+
}
184+
131185
/**
132186
* testValidateWithValidInput
133187
*/
@@ -160,4 +214,14 @@ public function testValidateWithInvalidInput()
160214
$this->assertCount(1, $errors);
161215
$this->assertEquals("Invalid cache handler 'clay-tablet'", $errors[0]);
162216
}
217+
218+
/**
219+
* The default ID prefix, based on installation directory
220+
*
221+
* @return string
222+
*/
223+
private function expectedIdPrefix(): string
224+
{
225+
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
226+
}
163227
}

setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/PageCacheTest.php

+67-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function setUp()
4545
public function testGetOptions()
4646
{
4747
$options = $this->configList->getOptions();
48-
$this->assertCount(6, $options);
48+
$this->assertCount(7, $options);
4949

5050
$this->assertArrayHasKey(0, $options);
5151
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
@@ -70,6 +70,10 @@ public function testGetOptions()
7070
$this->assertArrayHasKey(5, $options);
7171
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
7272
$this->assertEquals('page-cache-redis-password', $options[5]->getName());
73+
74+
$this->assertArrayHasKey(6, $options);
75+
$this->assertInstanceOf(TextConfigOption::class, $options[6]);
76+
$this->assertEquals('page-cache-id-prefix', $options[6]->getName());
7377
}
7478

7579
/**
@@ -90,7 +94,8 @@ public function testCreateConfigWithRedis()
9094
'database' => '',
9195
'compress_data' => '',
9296
'password' => ''
93-
]
97+
],
98+
'id_prefix' => $this->expectedIdPrefix(),
9499
]
95100
]
96101
]
@@ -117,7 +122,8 @@ public function testCreateConfigWithRedisConfiguration()
117122
'database' => '6',
118123
'compress_data' => '1',
119124
'password' => ''
120-
]
125+
],
126+
'id_prefix' => $this->expectedIdPrefix(),
121127
]
122128
]
123129
]
@@ -136,6 +142,54 @@ public function testCreateConfigWithRedisConfiguration()
136142
$this->assertEquals($expectedConfigData, $configData->getData());
137143
}
138144

145+
/**
146+
* testCreateConfigWithRedis
147+
*/
148+
public function testCreateConfigWithFileCache()
149+
{
150+
$this->deploymentConfigMock->method('get')->willReturn('');
151+
152+
$expectedConfigData = [
153+
'cache' => [
154+
'frontend' => [
155+
'page_cache' => [
156+
'id_prefix' => $this->expectedIdPrefix(),
157+
]
158+
]
159+
]
160+
];
161+
162+
$configData = $this->configList->createConfig([], $this->deploymentConfigMock);
163+
164+
$this->assertEquals($expectedConfigData, $configData->getData());
165+
}
166+
167+
/**
168+
* testCreateConfigCacheRedis
169+
*/
170+
public function testCreateConfigWithIdPrefix()
171+
{
172+
$this->deploymentConfigMock->method('get')->willReturn('');
173+
174+
$explicitPrefix = 'XXX_';
175+
$expectedConfigData = [
176+
'cache' => [
177+
'frontend' => [
178+
'page_cache' => [
179+
'id_prefix' => $explicitPrefix,
180+
]
181+
]
182+
]
183+
];
184+
185+
$configData = $this->configList->createConfig(
186+
['page-cache-id-prefix' => $explicitPrefix],
187+
$this->deploymentConfigMock
188+
);
189+
190+
$this->assertEquals($expectedConfigData, $configData->getData());
191+
}
192+
139193
/**
140194
* testValidationWithValidData
141195
*/
@@ -169,4 +223,14 @@ public function testValidationWithInvalidData()
169223
$this->assertCount(1, $errors);
170224
$this->assertEquals('Invalid cache handler \'foobar\'', $errors[0]);
171225
}
226+
227+
/**
228+
* The default ID prefix, based on installation directory
229+
*
230+
* @return string
231+
*/
232+
private function expectedIdPrefix(): string
233+
{
234+
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
235+
}
172236
}

0 commit comments

Comments
 (0)