Skip to content

Commit 9141fe5

Browse files
Indrani SonawaneIndrani Sonawane
authored andcommitted
Merge remote-tracking branch '36538/patch-3' into 247beta3_jan
2 parents 1d03b99 + d11fb65 commit 9141fe5

File tree

7 files changed

+230
-26
lines changed

7 files changed

+230
-26
lines changed

app/code/Magento/PageCache/Console/Command/GenerateVclCommand.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,36 @@ class GenerateVclCommand extends Command
2929
/**
3030
* Access list option name
3131
*/
32-
const ACCESS_LIST_OPTION = 'access-list';
32+
public const ACCESS_LIST_OPTION = 'access-list';
3333

3434
/**
3535
* Backend host option name
3636
*/
37-
const BACKEND_HOST_OPTION = 'backend-host';
37+
public const BACKEND_HOST_OPTION = 'backend-host';
3838

3939
/**
4040
* Backend port option name
4141
*/
42-
const BACKEND_PORT_OPTION = 'backend-port';
42+
public const BACKEND_PORT_OPTION = 'backend-port';
4343

4444
/**
4545
* Varnish version option name
4646
*/
47-
const EXPORT_VERSION_OPTION = 'export-version';
47+
public const EXPORT_VERSION_OPTION = 'export-version';
4848

4949
/**
5050
* Grace period option name
5151
*/
52-
const GRACE_PERIOD_OPTION = 'grace-period';
52+
public const GRACE_PERIOD_OPTION = 'grace-period';
5353

54+
/**
55+
* Input file option name
56+
*/
57+
public const INPUT_FILE_OPTION = 'input-file';
5458
/**
5559
* Output file option name
5660
*/
57-
const OUTPUT_FILE_OPTION = 'output-file';
61+
public const OUTPUT_FILE_OPTION = 'output-file';
5862

5963
/**
6064
* @var \Magento\Framework\Filesystem\Directory\WriteFactory
@@ -130,14 +134,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
130134
}
131135

132136
try {
137+
$inputFile = $input->getOption(self::INPUT_FILE_OPTION);
133138
$outputFile = $input->getOption(self::OUTPUT_FILE_OPTION);
134139
$varnishVersion = $input->getOption(self::EXPORT_VERSION_OPTION);
135140
$vclParameters = array_merge($this->inputToVclParameters($input), [
136141
'sslOffloadedHeader' => $this->getSslOffloadedHeader(),
137142
'designExceptions' => $this->getDesignExceptions(),
138143
]);
139144
$vclGenerator = $this->vclGeneratorFactory->create($vclParameters);
140-
$vcl = $vclGenerator->generateVcl($varnishVersion);
145+
$vcl = $vclGenerator->generateVcl($varnishVersion, $inputFile);
141146

142147
if ($outputFile) {
143148
$writer = $this->writeFactory->create($outputFile, DriverPool::FILE, 'w+');
@@ -201,6 +206,12 @@ private function getOptionList()
201206
'Grace period in seconds',
202207
300
203208
),
209+
new InputOption(
210+
self::INPUT_FILE_OPTION,
211+
null,
212+
InputOption::VALUE_REQUIRED,
213+
'Input file to generate vcl from'
214+
),
204215
new InputOption(
205216
self::OUTPUT_FILE_OPTION,
206217
null,
@@ -211,6 +222,8 @@ private function getOptionList()
211222
}
212223

213224
/**
225+
* Input vcl params
226+
*
214227
* @param InputInterface $input
215228
* @return array
216229
*/

app/code/Magento/PageCache/Model/Varnish/VclGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ public function __construct(
8484
* Return generated varnish.vcl configuration file
8585
*
8686
* @param int $version
87+
* @param string $inputFile
8788
* @return string
8889
*/
89-
public function generateVcl($version)
90+
public function generateVcl($version, $inputFile = null)
9091
{
91-
$template = $this->vclTemplateLocator->getTemplate($version);
92+
$template = $this->vclTemplateLocator->getTemplate($version, $inputFile);
9293
return strtr($template, $this->getReplacements());
9394
}
9495

app/code/Magento/PageCache/Model/Varnish/VclTemplateLocator.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\Module\Dir;
1111
use Magento\Framework\Module\Dir\Reader;
12+
use Magento\Framework\Filesystem\DirectoryList;
1213
use Magento\Framework\Filesystem\Directory\ReadFactory;
1314
use Magento\PageCache\Model\VclTemplateLocatorInterface;
1415
use Magento\PageCache\Exception\UnsupportedVarnishVersion;
@@ -21,32 +22,32 @@ class VclTemplateLocator implements VclTemplateLocatorInterface
2122
/**
2223
* XML path to Varnish 5 config template path
2324
*/
24-
const VARNISH_6_CONFIGURATION_PATH = 'system/full_page_cache/varnish6/path';
25+
public const VARNISH_6_CONFIGURATION_PATH = 'system/full_page_cache/varnish6/path';
2526

2627
/**
2728
* XML path to Varnish 5 config template path
2829
*/
29-
const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path';
30+
public const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path';
3031

3132
/**
3233
* XML path to Varnish 4 config template path
3334
*/
34-
const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path';
35+
public const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path';
3536

3637
/**
3738
* Varnish 4 supported version
3839
*/
39-
const VARNISH_SUPPORTED_VERSION_4 = '4';
40+
public const VARNISH_SUPPORTED_VERSION_4 = '4';
4041

4142
/**
4243
* Varnish 5 supported version
4344
*/
44-
const VARNISH_SUPPORTED_VERSION_5 = '5';
45+
public const VARNISH_SUPPORTED_VERSION_5 = '5';
4546

4647
/**
4748
* Varnish 6 supported version
4849
*/
49-
const VARNISH_SUPPORTED_VERSION_6 = '6';
50+
public const VARNISH_SUPPORTED_VERSION_6 = '6';
5051

5152
/**
5253
* @var array
@@ -72,30 +73,46 @@ class VclTemplateLocator implements VclTemplateLocatorInterface
7273
*/
7374
private $scopeConfig;
7475

76+
/**
77+
* @var DirectoryList
78+
*/
79+
private $directoryList;
80+
7581
/**
7682
* VclTemplateLocator constructor.
7783
*
7884
* @param Reader $reader
7985
* @param ReadFactory $readFactory
8086
* @param ScopeConfigInterface $scopeConfig
87+
* @param DirectoryList $directoryList
8188
*/
82-
public function __construct(Reader $reader, ReadFactory $readFactory, ScopeConfigInterface $scopeConfig)
83-
{
89+
public function __construct(
90+
Reader $reader,
91+
ReadFactory $readFactory,
92+
ScopeConfigInterface $scopeConfig,
93+
DirectoryList $directoryList
94+
) {
8495
$this->reader = $reader;
8596
$this->readFactory = $readFactory;
8697
$this->scopeConfig = $scopeConfig;
98+
$this->directoryList = $directoryList;
8799
}
88100

89101
/**
90102
* @inheritdoc
91103
*/
92-
public function getTemplate($version)
104+
public function getTemplate($version, $inputFile = null)
93105
{
94-
$moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_PageCache');
95-
$configFilePath = $moduleEtcPath . '/' . $this->scopeConfig->getValue($this->getVclTemplatePath($version));
96-
$directoryRead = $this->readFactory->create($moduleEtcPath);
97-
$configFilePath = $directoryRead->getRelativePath($configFilePath);
98-
$template = $directoryRead->readFile($configFilePath);
106+
if ($inputFile === null) {
107+
$moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_PageCache');
108+
$configFilePath = $moduleEtcPath . '/' . $this->scopeConfig->getValue($this->getVclTemplatePath($version));
109+
$directoryRead = $this->readFactory->create($moduleEtcPath);
110+
$configFilePath = $directoryRead->getRelativePath($configFilePath);
111+
$template = $directoryRead->readFile($configFilePath);
112+
} else {
113+
$reader = $this->readFactory->create($this->directoryList->getRoot());
114+
$template = $reader->readFile($inputFile);
115+
}
99116
return $template;
100117
}
101118

app/code/Magento/PageCache/Model/VclGeneratorInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ interface VclGeneratorInterface
1616
* Return generated varnish.vcl configuration file
1717
*
1818
* @param int $version
19+
* @param string $inputFile
1920
* @return string
2021
* @since 100.2.0
2122
*/
22-
public function generateVcl($version);
23+
public function generateVcl($version, $inputFile = null);
2324
}

app/code/Magento/PageCache/Model/VclTemplateLocatorInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\PageCache\Exception\UnsupportedVarnishVersion;
1010

1111
/**
12-
* Vcl template locator interface
12+
* Vcl template locator
1313
*
1414
* @api
1515
* @since 100.2.0
@@ -20,9 +20,10 @@ interface VclTemplateLocatorInterface
2020
* Get Varnish Vcl template
2121
*
2222
* @param int $version
23+
* @param string $inputFile
2324
* @return string
2425
* @throws UnsupportedVarnishVersion
2526
* @since 100.2.0
2627
*/
27-
public function getTemplate($version);
28+
public function getTemplate($version, $inputFile = null);
2829
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Test\Unit\Console\Command;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Filesystem\File\WriteFactory;
12+
use Magento\Framework\Serialize\Serializer\Json;
13+
use Magento\PageCache\Console\Command\GenerateVclCommand;
14+
use Magento\PageCache\Model\Varnish\VclGenerator;
15+
use Magento\PageCache\Model\VclGeneratorInterfaceFactory;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
class GenerateVclCommandInputOptionTest extends TestCase
20+
{
21+
/**
22+
* @var GenerateVclCommand
23+
*/
24+
private $command;
25+
26+
/**
27+
* @var VclGeneratorInterfaceFactory
28+
*/
29+
private $vclGeneratorInterfaceFactory;
30+
31+
/**
32+
* @var WriteFactory
33+
*/
34+
private $writeFactoryMock;
35+
36+
/**
37+
* @var ScopeConfigInterface
38+
*/
39+
private $scopeConfigMock;
40+
41+
/**
42+
* @var VclGenerator
43+
*/
44+
private $vclGenerator;
45+
46+
/**
47+
* @var Json
48+
*/
49+
private $serializer;
50+
51+
protected function setUp(): void
52+
{
53+
$this->vclGeneratorInterfaceFactory = $this->createMock(VclGeneratorInterfaceFactory::class);
54+
$this->vclGenerator = $this->createMock(VclGenerator::class);
55+
$this->vclGenerator->method('generateVcl')->willReturn('test.vcl" file can\'t be read.');
56+
$this->vclGeneratorInterfaceFactory->method('create')->willReturn($this->vclGenerator);
57+
$this->writeFactoryMock = $this->createMock(WriteFactory::class);
58+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
59+
$this->serializer = $this->getMockBuilder(Json::class)
60+
->onlyMethods(['unserialize'])
61+
->getMockForAbstractClass();
62+
63+
$this->command = new GenerateVclCommand(
64+
$this->vclGeneratorInterfaceFactory,
65+
$this->writeFactoryMock,
66+
$this->scopeConfigMock,
67+
$this->serializer
68+
);
69+
}
70+
71+
public function testConfigure()
72+
{
73+
$this->assertEquals('varnish:vcl:generate', $this->command->getName());
74+
$this->assertEquals(
75+
'Generates Varnish VCL and echos it to the command line',
76+
$this->command->getDescription()
77+
);
78+
}
79+
80+
public function testInputOption()
81+
{
82+
$options = [
83+
'--' . GenerateVclCommand::INPUT_FILE_OPTION => 'test.vcl',
84+
'--' . GenerateVclCommand::EXPORT_VERSION_OPTION => 6,
85+
];
86+
87+
$commandTester = new CommandTester($this->command);
88+
$commandTester->execute($options, ['interactive' => false]);
89+
$this->assertStringContainsString(
90+
'test.vcl" file can\'t be read.',
91+
$commandTester->getDisplay()
92+
);
93+
}
94+
}

0 commit comments

Comments
 (0)