Skip to content

Commit 4685ace

Browse files
author
Rafael Grigorian
committed
Fixed #54
1 parent 22e1e46 commit 4685ace

File tree

17 files changed

+848
-119
lines changed

17 files changed

+848
-119
lines changed

Block/Adminhtml/Export.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Block\Adminhtml;
4+
5+
use Magento\Backend\Block\Widget\Button;
6+
use Magento\Config\Block\System\Config\Form\Field;
7+
use Magento\Framework\Data\Form\Element\AbstractElement;
8+
use Magento\PageCache\Model\Config;
9+
10+
/**
11+
* @version 2.0.2
12+
* @package JetRails® Varnish
13+
* @author Rafael Grigorian - JetRails®
14+
* @copyright JetRails®, all rights reserved
15+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
16+
*/
17+
class Export extends Field {
18+
19+
protected function _getElementHtml ( AbstractElement $element) {
20+
$buttonBlock = $this->getForm ()->getLayout ()->createBlock ( Button::class );
21+
$params = [
22+
"website" => $buttonBlock->getRequest ()->getParam ("website"),
23+
"varnish" => $this->getVarnishVersion ()
24+
];
25+
$data = [
26+
"id" => "system_full_page_cache_varnish_export_custom_button_version" . $this->getVarnishVersion (),
27+
"label" => $this->getLabel (),
28+
"onclick" => "setLocation ('" . $this->getVarnishUrl ( $params ) . "')",
29+
];
30+
$html = $buttonBlock->setData ( $data )->toHtml ();
31+
return $html;
32+
}
33+
34+
public function getVarnishVersion () {
35+
return 0;
36+
}
37+
38+
public function getLabel () {
39+
return __( "Export Custom VCL for Varnish %1", $this->getVarnishVersion () );
40+
}
41+
42+
public function getVarnishUrl ( $params = [] ) {
43+
return $this->getUrl ( "varnish/export/customconfig", $params );
44+
// return $this->getUrl ( "*/Export/customConfig", $params );
45+
}
46+
47+
public function getTtlValue () {
48+
return $this->_scopeConfig->getValue ( Config::XML_PAGECACHE_TTL );
49+
}
50+
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Block\Adminhtml\Export;
4+
5+
use JetRails\Varnish\Block\Adminhtml\Export;
6+
7+
/**
8+
* @version 2.0.2
9+
* @package JetRails® Varnish
10+
* @author Rafael Grigorian - JetRails®
11+
* @copyright JetRails®, all rights reserved
12+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
13+
*/
14+
class Varnish4 extends Export {
15+
16+
public function getVarnishVersion () {
17+
return 4;
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Block\Adminhtml\Export;
4+
5+
use JetRails\Varnish\Block\Adminhtml\Export;
6+
7+
/**
8+
* @version 2.0.2
9+
* @package JetRails® Varnish
10+
* @author Rafael Grigorian - JetRails®
11+
* @copyright JetRails®, all rights reserved
12+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
13+
*/
14+
class Varnish5 extends Export {
15+
16+
public function getVarnishVersion () {
17+
return 5;
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Block\Adminhtml\Export;
4+
5+
use JetRails\Varnish\Block\Adminhtml\Export;
6+
7+
/**
8+
* @version 2.0.2
9+
* @package JetRails® Varnish
10+
* @author Rafael Grigorian - JetRails®
11+
* @copyright JetRails®, all rights reserved
12+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
13+
*/
14+
class Varnish6 extends Export {
15+
16+
public function getVarnishVersion () {
17+
return 6;
18+
}
19+
20+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Console\Command\Vcl;
4+
5+
use Exception;
6+
use JetRails\Varnish\Helper\VclGenerator;
7+
use Magento\PageCache\Console\Command\GenerateVclCommand;
8+
use Magento\PageCache\Model\Varnish\VclTemplateLocator;
9+
use Magento\PageCache\Model\VclTemplateLocatorInterface;
10+
use Magento\PageCache\Model\VclGeneratorInterfaceFactory;
11+
use Magento\Framework\Filesystem\DriverPool;
12+
use Magento\Framework\Filesystem\File\WriteFactory;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\Console\Cli;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
/**
22+
* @version 2.0.2
23+
* @package JetRails® Varnish
24+
* @author Rafael Grigorian - JetRails®
25+
* @copyright JetRails®, all rights reserved
26+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
27+
*/
28+
class GenerateCustom extends Command {
29+
30+
protected $vclTemplateLocator;
31+
protected $vclGeneratorFactory;
32+
protected $writeFactory;
33+
protected $generator;
34+
35+
const EXPORT_VERSION_OPTION = "export-version";
36+
const OUTPUT_FILE_OPTION = "output-file";
37+
38+
public function __construct (
39+
VclTemplateLocatorInterface $vclTemplateLocator,
40+
VclGeneratorInterfaceFactory $vclGeneratorFactory,
41+
WriteFactory $writeFactory,
42+
VclGenerator $generator
43+
) {
44+
parent::__construct ();
45+
$this->vclTemplateLocator = $vclTemplateLocator;
46+
$this->vclGeneratorFactory = $vclGeneratorFactory;
47+
$this->writeFactory = $writeFactory;
48+
$this->generator = $generator;
49+
}
50+
51+
protected function configure () {
52+
$this->setName ("varnish:vcl:generate-custom")
53+
->setDescription ("Generates Varnish companion VCL and echos it to the command line")
54+
->setDefinition ( $this->getOptionList () );
55+
}
56+
57+
protected function execute ( InputInterface $input, OutputInterface $output ) {
58+
try {
59+
$outputFile = $input->getOption ( self::OUTPUT_FILE_OPTION );
60+
$varnishVersion = $input->getOption ( self::EXPORT_VERSION_OPTION );
61+
$vcl = $this->vclTemplateLocator->getTemplate ( $varnishVersion );
62+
$vcl = $this->generator->generateCustom ( $vcl );
63+
if ( $outputFile ) {
64+
$writer = $this->writeFactory->create ( $outputFile, DriverPool::FILE, "w+" );
65+
$writer->write ( $vcl );
66+
$writer->close ();
67+
}
68+
else {
69+
$output->writeln ( $vcl );
70+
}
71+
return Cli::RETURN_SUCCESS;
72+
}
73+
catch ( Exception $e ) {
74+
$output->writeln ("<error>" . $e->getMessage () . "</error>");
75+
if ($output->getVerbosity () >= OutputInterface::VERBOSITY_VERBOSE ) {
76+
$output->writeln ( $e->getTraceAsString () );
77+
}
78+
return Cli::RETURN_FAILURE;
79+
}
80+
}
81+
82+
private function getOptionList () {
83+
return [
84+
new InputOption(
85+
self::EXPORT_VERSION_OPTION,
86+
null,
87+
InputOption::VALUE_REQUIRED,
88+
'The version of Varnish file',
89+
VclTemplateLocator::VARNISH_SUPPORTED_VERSION_4
90+
),
91+
new InputOption(
92+
self::OUTPUT_FILE_OPTION,
93+
null,
94+
InputOption::VALUE_REQUIRED,
95+
'Path to the file to write vcl'
96+
),
97+
];
98+
}
99+
100+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Controller\Adminhtml\Export;
4+
5+
use JetRails\Varnish\Helper\VclGenerator;
6+
use Magento\Backend\App\Action;
7+
use Magento\Backend\App\Action\Context;
8+
use Magento\Framework\App\Action\HttpGetActionInterface;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\Response\Http\FileFactory;
11+
use Magento\PageCache\Model\Config;
12+
13+
/**
14+
* @version 2.0.2
15+
* @package JetRails® Varnish
16+
* @author Rafael Grigorian - JetRails®
17+
* @copyright JetRails®, all rights reserved
18+
* @license The JetRails License (SEE LICENSE IN LICENSE.md)
19+
*/
20+
class CustomConfig extends Action implements HttpGetActionInterface {
21+
22+
protected $fileFactory;
23+
protected $config;
24+
protected $generator;
25+
26+
const ADMIN_RESOURCE = "Magento_Backend::system";
27+
28+
public function __construct (
29+
Context $context,
30+
FileFactory $fileFactory,
31+
Config $config,
32+
VclGenerator $generator
33+
) {
34+
parent::__construct ( $context );
35+
$this->config = $config;
36+
$this->fileFactory = $fileFactory;
37+
$this->generator = $generator;
38+
}
39+
40+
public function execute () {
41+
$fileName = "default.custom.vcl";
42+
$version = $this->getRequest ()->getParam ("varnish");
43+
switch ( $version ) {
44+
case 6:
45+
$content = $this->config->getVclFile ( Config::VARNISH_6_CONFIGURATION_PATH );
46+
break;
47+
case 5:
48+
$content = $this->config->getVclFile ( Config::VARNISH_5_CONFIGURATION_PATH );
49+
break;
50+
default:
51+
$content = $this->config->getVclFile ( Config::VARNISH_4_CONFIGURATION_PATH );
52+
break;
53+
}
54+
$content = $this->generator->generateCustom ( $content );
55+
return $this->fileFactory->create ( $fileName, $content, DirectoryList::VAR_DIR );
56+
}
57+
}

0 commit comments

Comments
 (0)