Skip to content

Commit 50892d3

Browse files
author
Rafael Grigorian
committed
Export VCL for Varnish 4 controller complete
1 parent f8c2162 commit 50892d3

File tree

12 files changed

+264
-183
lines changed

12 files changed

+264
-183
lines changed

src/app/code/JetRails/Varnish/Console/Command/AbstractCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@ public function __construct ( Data $data, Logger $logger, Purger $purger ) {
2626
}
2727

2828
protected function execute ( InputInterface $input, OutputInterface $output ) {
29-
$output->writeln ("");
30-
$this->runCommand ( $input, $output );
31-
$output->writeln ("");
29+
$purgeCommand = preg_match ( "/^varnish:purge/", $this->getName () );
30+
if ( $purgeCommand && !$this->_data->isEnabled () ) {
31+
$output->writeln (
32+
"\nCache application must be set to <fg=red>Varnish Cache</>, set it by configuring" .
33+
" Stores → Advanced → Developer → System → Full Page Cache → Caching Application.\n" .
34+
"Alternatively, you can run the <fg=red>varnish:cache:enable</> command.\n"
35+
);
36+
}
37+
else {
38+
$output->writeln ("");
39+
$this->runCommand ( $input, $output );
40+
$output->writeln ("");
41+
}
3242
}
3343

3444
}

src/app/code/JetRails/Varnish/Console/Command/Cache/Status.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ protected function configure () {
2626
}
2727

2828
protected function execute ( InputInterface $input, OutputInterface $output ) {
29-
// Clear config cache
30-
$arrayInput = new ArrayInput ([ "command" => "cache:clean", "types" => [ "config" ] ]);
31-
$command = $this->getApplication ()->find ("cache:clean");
32-
$command->run ( $arrayInput, new NullOutput () );
3329
// Check to see if varnish caching is enabled
3430
$status = $this->_data->isEnabled ();
3531
$type = $status ? "<fg=green>Varnish Cache</>" : "<fg=red>Built-in Cache</>";

src/app/code/JetRails/Varnish/Controller/Adminhtml/Export/Config.php

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,82 @@
22

33
namespace JetRails\Varnish\Controller\Adminhtml\Export;
44

5+
use JetRails\Varnish\Helper\Data;
56
use Magento\Backend\App\Action;
67
use Magento\Framework\App\Action\Context;
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\Response\Http\FileFactory;
710
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\Filesystem\Directory\ReadFactory;
812
use Magento\Framework\Module\Dir\Reader;
913

1014
class Config extends Action {
1115

12-
protected $_response;
16+
protected $_data;
17+
protected $_fileFactory;
1318
protected $_moduleReader;
19+
protected $_readFactory;
20+
protected $_response;
1421

15-
public function __construct ( Context $context, Reader $moduleReader ) {
16-
$this->_response = $context->getResponse ();
22+
public function __construct (
23+
Context $context,
24+
Data $data,
25+
FileFactory $fileFactory,
26+
ReadFactory $readFactory,
27+
Reader $moduleReader
28+
) {
29+
$this->_data = $data;
30+
$this->_fileFactory = $fileFactory;
1731
$this->_moduleReader = $moduleReader;
32+
$this->_readFactory = $readFactory;
33+
$this->_response = $context->getResponse ();
1834
parent::__construct ( $context );
1935
}
2036

37+
private function _returnError ( $message ) {
38+
$redirect = $this->resultFactory->create ( ResultFactory::TYPE_REDIRECT );
39+
$this->messageManager->addError ( $message );
40+
return $redirect->setPath ("adminhtml/system_config/edit/section/jetrails_varnish");
41+
}
42+
2143
public function execute () {
22-
// Get the path to the template file
44+
// Get the currently saved configuration
45+
$backendInput = $this->_data->getBackendServer ();
46+
$varnishInput = $this->_data->getVarnishServersWithPorts ();
47+
$varnishInput = array_map ( function ( $i ) { return "\"$i->host\""; }, $varnishInput );
48+
$varnishInput = array_unique ( $varnishInput );
49+
// Check to see if the inputs are valid
50+
if ( $backendInput === false || count ( $varnishInput ) < 1 ) {
51+
// If they are not valid, then generate an error message and exit
52+
$blame = !$backendInput ? "Backend Server" : "Varnish Servers";
53+
$blame = "<font color='#E22626' ><b>$blame</b></font>";
54+
$message = "To export the VCL file, ensure that the $blame field is not empty ";
55+
$message .= "and all changes have been saved before exporting";
56+
return $this->_returnError ( $message );
57+
}
58+
// Define the path to the varnish configuration template
2359
$templatePath = $this->_moduleReader->getModuleDir ( "etc", "JetRails_Varnish" );
2460
$templatePath .= "/templates/default.vcl";
25-
// Check to see if the file exists
26-
if ( file_exists ( $templatePath ) ) {
27-
// Set the headers to force download
28-
// Not using Magento to set headers on purpose
29-
header ( "JetRails-No-Cache-Blame-Module: jetrails/varnish" );
30-
header ( "Content-Type: application/octet-stream" );
31-
header ( "Content-Disposition: attachment; filename=default.vcl" );
32-
header ( "Content-Transfer-Encoding: binary" );
33-
header ( "Expires: 0" );
34-
header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
35-
header ( "Pragma: public" );
36-
header ( "Content-Length: " . filesize ( $templatePath ) );
37-
// Flush buffer
38-
ob_clean ();
39-
flush ();
40-
// Attempt to read the file, if successful, exit
41-
if ( ( $content = file_get_contents ( $templatePath ) ) !== false ) {
42-
// Output the content and exit
43-
echo $content;
44-
return true;
45-
}
61+
// Check to see if the template exists
62+
if ( !file_exists ( $templatePath ) ) {
63+
// If it doesn't then return an error message
64+
$errorMessage = "<font color='#E22626' >etc/templates/default.vcl</font>";
65+
$errorMessage = "Template file for VCL not accessible at $errorMessage";
66+
return $this->_returnError ( $errorMessage );
4667
}
47-
// Report that the template file is missing
48-
$this->messageManager->addError ("Template file not accessible in /etc/templates");
49-
// Redirect back to cache management page
50-
$redirect = $this->resultFactory->create ( ResultFactory::TYPE_REDIRECT );
51-
return $redirect->setPath ("adminhtml/cache/index");
68+
// Get the contents of the template file
69+
$template = file_get_contents ( $templatePath );
70+
// Define the replacements for the template file
71+
$replacements = [
72+
"{{_BACKEND_HOST_}}" => $backendInput->host,
73+
"{{_BACKEND_PORT_}}" => $backendInput->port,
74+
"{{_PURGE_ACL_}}" => implode ( ";\n\t", $varnishInput ) . ";"
75+
];
76+
// Define the output file name, and preform replacements on the template
77+
$filename = "default.vcl";
78+
$content = strtr ( $template, $replacements );
79+
// Serve the file to the client
80+
return $this->_fileFactory->create ( $filename, $content, DirectoryList::VAR_DIR );
5281
}
5382

5483
}

src/app/code/JetRails/Varnish/Controller/Adminhtml/Purge/All.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,40 @@ public function __construct (
2828
}
2929

3030
public function execute () {
31-
// Ask to purge and iterate over responses
32-
foreach ( $this->_purger->purgeAll () as $response ) {
33-
// Log what we are trying to do
34-
$message = [
35-
"action" => "purge:all",
36-
"status" => $response->status,
37-
"server" => $response->server
38-
];
39-
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
40-
// Check to see if response was successful
41-
if ( $response->status == 200 ) {
42-
// Add success response message
43-
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
44-
$msg = "Successfully purged all cache on $serverHtml";
45-
$this->messageManager->addSuccess ( $msg );
46-
}
47-
else {
48-
// Otherwise add an error message
49-
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
50-
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
51-
$msg = "Error Purging all cache on $serverHtml with response code $statusHtml";
52-
$this->messageManager->addError ( $msg );
31+
// Check to see if varnish cache is enabled
32+
if ( $this->_data->isEnabled () ) {
33+
// Ask to purge and iterate over responses
34+
foreach ( $this->_purger->purgeAll () as $response ) {
35+
// Log what we are trying to do
36+
$message = [
37+
"action" => "purge:all",
38+
"status" => $response->status,
39+
"server" => $response->server
40+
];
41+
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
42+
// Check to see if response was successful
43+
if ( $response->status == 200 ) {
44+
// Add success response message
45+
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
46+
$msg = "Successfully purged all cache on $serverHtml";
47+
$this->messageManager->addSuccess ( $msg );
48+
}
49+
else {
50+
// Otherwise add an error message
51+
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
52+
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
53+
$msg = "Error Purging all cache on $serverHtml with response code $statusHtml";
54+
$this->messageManager->addError ( $msg );
55+
}
5356
}
5457
}
58+
else {
59+
// Cache application is not Varnish, warn user
60+
$this->messageManager->addError (
61+
"Cache application must be set to <b>Varnish Cache</b>, set it by configuring" .
62+
" <b>Stores → Advanced → Developer → System → Full Page Cache → Caching Application</b>"
63+
);
64+
}
5565
// Redirect back to cache management page
5666
$redirect = $this->resultFactory->create ( ResultFactory::TYPE_REDIRECT );
5767
return $redirect->setPath ("adminhtml/cache/index");

src/app/code/JetRails/Varnish/Controller/Adminhtml/Purge/Store.php

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,51 @@ public function __construct (
2828
}
2929

3030
public function execute () {
31-
// Load passed store id and validate it's existence
32-
$storeId = intval ( $this->getRequest ()->getParam ("id") );
33-
// Make sure store id is valid
34-
$url = $this->_purger->validateAndResolveStoreId ( $storeId );
35-
if ( gettype ( $url ) == "object" ) {
36-
// Ask to purge and iterate over responses
37-
foreach ( $this->_purger->purgeStore ( $url ) as $response ) {
38-
// Log what we are trying to do
39-
$message = [
40-
"status" => $response->status,
41-
"action" => "purge:store",
42-
"target" => $response->target,
43-
"server" => $response->server
44-
];
45-
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
46-
// Check to see if response was successful
47-
if ( $response->status == 200 ) {
48-
// Add success response message
49-
$storeHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
50-
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
51-
$msg = "Successfully purged store view $storeHtml on $serverHtml";
52-
$this->messageManager->addSuccess ( $msg );
53-
}
54-
else {
55-
// Otherwise add an error message
56-
$storeHtml = "<font color='#E22626' ><b>$response->target</b></font>";
57-
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
58-
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
59-
$msg = "Error Purging store view $storeHtml on $serverHtml with response code $statusHtml";
60-
$this->messageManager->addError ( $msg );
31+
// Check to see if varnish cache is enabled
32+
if ( $this->_data->isEnabled () ) {
33+
// Load passed store id and validate it's existence
34+
$storeId = intval ( $this->getRequest ()->getParam ("id") );
35+
// Make sure store id is valid
36+
$url = $this->_purger->validateAndResolveStoreId ( $storeId );
37+
if ( gettype ( $url ) == "object" ) {
38+
// Ask to purge and iterate over responses
39+
foreach ( $this->_purger->purgeStore ( $url ) as $response ) {
40+
// Log what we are trying to do
41+
$message = [
42+
"status" => $response->status,
43+
"action" => "purge:store",
44+
"target" => $response->target,
45+
"server" => $response->server
46+
];
47+
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
48+
// Check to see if response was successful
49+
if ( $response->status == 200 ) {
50+
// Add success response message
51+
$storeHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
52+
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
53+
$msg = "Successfully purged store view $storeHtml on $serverHtml";
54+
$this->messageManager->addSuccess ( $msg );
55+
}
56+
else {
57+
// Otherwise add an error message
58+
$storeHtml = "<font color='#E22626' ><b>$response->target</b></font>";
59+
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
60+
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
61+
$msg = "Error Purging store view $storeHtml on $serverHtml with response code $statusHtml";
62+
$this->messageManager->addError ( $msg );
63+
}
6164
}
6265
}
66+
// If it is invalid, warn caller
67+
else { $this->messageManager->addError ( "Invalid store id '$storeId' passed" ); }
68+
}
69+
else {
70+
// Cache application is not Varnish, warn user
71+
$this->messageManager->addError (
72+
"Cache application must be set to <b>Varnish Cache</b>, set it by configuring" .
73+
" <b>Stores → Advanced → Developer → System → Full Page Cache → Caching Application</b>"
74+
);
6375
}
64-
// If it is invalid, warn caller
65-
else { $this->messageManager->addError ( "Invalid store id '$storeId' passed" ); }
6676
// Redirect back to cache management page
6777
$redirect = $this->resultFactory->create ( ResultFactory::TYPE_REDIRECT );
6878
return $redirect->setPath ("adminhtml/cache/index");

src/app/code/JetRails/Varnish/Controller/Adminhtml/Purge/Url.php

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,51 @@ public function __construct (
2828
}
2929

3030
public function execute () {
31-
// Load passed url parameter and validate it
32-
$url = $this->getRequest ()->getParam ("url");
33-
$url = $this->_purger->validateUrl ( $url );
34-
// If an object was returned, then it was a valid url
35-
if ( gettype ( $url ) == "object" ) {
36-
// Ask to purge and iterate over responses
37-
foreach ( $this->_purger->purgeUrl ( $url ) as $response ) {
38-
// Log what we are trying to do
39-
$message = [
40-
"status" => $response->status,
41-
"action" => "purge:url",
42-
"target" => $response->target,
43-
"server" => $response->server
44-
];
45-
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
46-
// Check to see if response was successful
47-
if ( $response->status == 200 ) {
48-
// Add success response message
49-
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
50-
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
51-
$message = "Successfully purged url $targetHtml on $serverHtml";
52-
$this->messageManager->addSuccess ( $message );
53-
}
54-
else {
55-
// Otherwise add an error message
56-
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
57-
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
58-
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
59-
$message = "Error purging url $targetHtml on $serverHtml with response code $statusHtml";
60-
$this->messageManager->addError ( $message );
31+
// Check to see if varnish cache is enabled
32+
if ( $this->_data->isEnabled () ) {
33+
// Load passed url parameter and validate it
34+
$url = $this->getRequest ()->getParam ("url");
35+
$url = $this->_purger->validateUrl ( $url );
36+
// If an object was returned, then it was a valid url
37+
if ( gettype ( $url ) == "object" ) {
38+
// Ask to purge and iterate over responses
39+
foreach ( $this->_purger->purgeUrl ( $url ) as $response ) {
40+
// Log what we are trying to do
41+
$message = [
42+
"status" => $response->status,
43+
"action" => "purge:url",
44+
"target" => $response->target,
45+
"server" => $response->server
46+
];
47+
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
48+
// Check to see if response was successful
49+
if ( $response->status == 200 ) {
50+
// Add success response message
51+
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
52+
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
53+
$message = "Successfully purged url $targetHtml on $serverHtml";
54+
$this->messageManager->addSuccess ( $message );
55+
}
56+
else {
57+
// Otherwise add an error message
58+
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
59+
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
60+
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
61+
$message = "Error purging url $targetHtml on $serverHtml with response code $statusHtml";
62+
$this->messageManager->addError ( $message );
63+
}
6164
}
6265
}
66+
// Otherwise an error was returned in the form of a string
67+
else { $this->messageManager->addError ( $url ); }
68+
}
69+
else {
70+
// Cache application is not Varnish, warn user
71+
$this->messageManager->addError (
72+
"Cache application must be set to <b>Varnish Cache</b>, set it by configuring" .
73+
" <b>Stores → Advanced → Developer → System → Full Page Cache → Caching Application</b>"
74+
);
6375
}
64-
// Otherwise an error was returned in the form of a string
65-
else { $this->messageManager->addError ( $url ); }
6676
// Redirect back to cache management page
6777
$redirect = $this->resultFactory->create ( ResultFactory::TYPE_REDIRECT );
6878
return $redirect->setPath ("adminhtml/cache/index");

0 commit comments

Comments
 (0)