Skip to content

Commit e6e8486

Browse files
author
Rafael Grigorian
committed
Modified console interface (incomplete)
1 parent 50892d3 commit e6e8486

File tree

10 files changed

+143
-154
lines changed

10 files changed

+143
-154
lines changed

README.md

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,17 @@ varnishadm "vcl.load default /usr/local/etc/varnish/default.vcl"
99
varnishadm "vcl.use default"
1010

1111

12-
13-
14-
app code JetRails
15-
app design JetRails
16-
17-
18-
MODEL -> MAKE Model->Adminhtml
19-
20-
21-
22-
23-
Steps to configure
24-
__________________________________________________________________
25-
make sure you enable varnish in config->system->fpc as cache
26-
27-
28-
29-
30-
12+
varnish:config
13+
varnish:status
14+
varnish:status:set <enable / disable>
3115
varnish:purge:url <url>
3216
varnish:purge:store <store_view_id>
3317
varnish:purge:all
3418

35-
varnish:cache:status
36-
varnish:cache:enable
37-
varnish:cache:disable
19+
primer:config
20+
primer:status
21+
primer:status:set <enable / disable>
22+
primer:queue
23+
primer:queue:show
24+
3825

39-
varnish:primer:status
40-
varnish:primer:queue
41-
varnish:primer:enable
42-
varnish:primer:disable

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212

13-
class AbstractCommand extends Command {
13+
abstract class AbstractCommand extends Command {
1414

1515
protected $_data;
1616

@@ -25,20 +25,53 @@ public function __construct ( Data $data, Logger $logger, Purger $purger ) {
2525
parent::__construct ();
2626
}
2727

28+
private function _printLabel ( $output, $label, $value ) {
29+
$message = sprintf ( "<fg=cyan>%-16s%s</>", "$label", $value );
30+
$output->writeln ( $message );
31+
}
32+
33+
private function _printLine ( $output, $label, $value ) {
34+
$message = sprintf ( "<fg=yellow>%-16s</>%s", "$label", $value );
35+
$output->writeln ( $message );
36+
}
37+
2838
protected function execute ( InputInterface $input, OutputInterface $output ) {
39+
$output->writeln ("");
40+
$this->_printLabel ( $output, "Powered By", "The JetRails Team" );
41+
$this->_printLabel ( $output, "Email Us", "[email protected]" );
42+
$this->_printLabel ( $output, "Call Us", "+1 (888) 554-9990" );
43+
$output->writeln ("");
44+
2945
$purgeCommand = preg_match ( "/^varnish:purge/", $this->getName () );
46+
3047
if ( $purgeCommand && !$this->_data->isEnabled () ) {
3148
$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"
49+
"Cache application must be set to <options=underscore>Varnish Cache</>\n" .
50+
"<fg=yellow>Backend</>: Stores → Advanced → Developer → System → Full Page Cache → Caching Application\n" .
51+
"<fg=yellow>Console</>: varnish:status:set enable\n"
3552
);
3653
}
3754
else {
38-
$output->writeln ("");
39-
$this->runCommand ( $input, $output );
55+
56+
$response = ( object ) $this->runCommand ( $input );
57+
58+
$status = $response->status ? "successful" : ( $response->status === null ? "mixed" : "unsuccessful" );
59+
$parameters = $input->getArguments ();
60+
unset ( $parameters ["command"] );
61+
$parametersKeys = array_keys ( $parameters );
62+
$parametersKeys = array_map ( function ( $i ) use ( $parameters ) { return "$i" . $parameters [ $i ]; }, $parametersKeys );
63+
$parameter = $parametersKeys;
64+
$parameters = count ( $parameters ) == 0 ? "none" : implode ( ", ", $parameter );
65+
66+
$this->_printLine ( $output, "Command", $this->getName () );
67+
$this->_printLine ( $output, "Parameters", $parameters );
68+
$this->_printLine ( $output, "Status", $status );
69+
$this->_printLine ( $output, "Response", $response->message );
70+
if ( isset ( $response->payload ) ) $output->writeln ( "\n" . implode ( "\n", $response->payload ) );
4071
$output->writeln ("");
4172
}
4273
}
74+
75+
protected abstract function runCommand ( InputInterface $input );
4376

4477
}

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/app/code/JetRails/Varnish/Console/Command/Purge/Url.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ protected function configure () {
1616
->addArgument ( "url", InputArgument::REQUIRED, "What URL do you want to purge?" );
1717
}
1818

19-
protected function runCommand ( InputInterface $input, OutputInterface $output ) {
19+
protected function runCommand ( InputInterface $input ) {
2020
// Load passed url parameter and validate it
2121
$url = $input->getArgument ("url");
2222
$url = $this->_purger->validateUrl ( $url );
2323
// If an object was returned, then it was a valid url
2424
if ( gettype ( $url ) == "object" ) {
25+
$total = 0;
26+
$success = 0;
27+
$payload = [];
28+
2529
// Ask to purge and iterate over responses
2630
foreach ( $this->_purger->purgeUrl ( $url ) as $response ) {
2731
// Log what we are trying to do
@@ -37,21 +41,24 @@ protected function runCommand ( InputInterface $input, OutputInterface $output )
3741
// Add success response message
3842
$targetHtml = "<fg=green>$response->target</>";
3943
$serverHtml = "<fg=green>$response->server</>";
40-
$message = "Successfully purged url $targetHtml on $serverHtml";
41-
$output->writeln ( $message );
44+
$message = "<bg=green> </>\n<bg=green;fg=white;options=bold> SUCCESS </> purged url $targetHtml on $serverHtml\n<bg=green> </>";
45+
array_push ( $payload, $message );
46+
$success++;
4247
}
4348
else {
4449
// Otherwise add an error message
4550
$targetHtml = "<fg=red>$response->target</>";
4651
$serverHtml = "<fg=red>$response->server</>";
4752
$statusHtml = "<fg=red>$response->status</>";
48-
$message = "Error purging url $targetHtml on $serverHtml with response code $statusHtml";
49-
$output->writeln ( $message );
53+
$message = "<bg=red> </>\n<bg=red;fg=white;options=bold> ERROR </> couldn't purge url $targetHtml on $serverHtml with response code $statusHtml\n<bg=red> </>";
54+
array_push ( $payload, $message );
5055
}
56+
$total++;
5157
}
58+
return [ "status" => $success > 0 && $total - $success > 0 ? null : $total == $success, "message" => "purged url from $success/$total varnish servers", "payload" => $payload ];
5259
}
5360
// Otherwise an error was returned in the form of a string
54-
else { $output->writeln ( "<fg=red>" . $url . "</>" ); }
61+
return [ "status" => false, "message" => $url ];
5562
}
5663

5764
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Console\Command;
4+
5+
use JetRails\Varnish\Console\Command\AbstractCommand;
6+
use Symfony\Component\Console\Input\ArrayInput;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class Status extends AbstractCommand {
12+
13+
protected function configure () {
14+
// Register the command and set the arguments
15+
$this->setName ("varnish:status")
16+
->setDescription ("Check to see if what caching application is being used");
17+
}
18+
19+
protected function runCommand ( InputInterface $input ) {
20+
// Check to see if varnish caching is enabled and prepare message
21+
$status = $this->_data->isEnabled ();
22+
$message = $status ? "<options=underscore>Varnish Cache</>" : "<options=underscore>Built-in Cache</>";
23+
$message = "Caching application is set to $message";
24+
// Return the message and status to caller
25+
return [ "status" => $status, "message" => $message ];
26+
}
27+
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Console\Command\Status;
4+
5+
use JetRails\Varnish\Console\Command\AbstractCommand;
6+
use Magento\PageCache\Model\Config;
7+
use Symfony\Component\Console\Input\ArrayInput;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class Set extends AbstractCommand {
13+
14+
protected function configure () {
15+
// Register the command and set the arguments
16+
$this->setName ("varnish:status:set")
17+
->setDescription ("Check to see if what caching application is being used")
18+
->addArgument ( "state", InputArgument::REQUIRED, "Should we enable varnish cache?" );
19+
}
20+
21+
protected function runCommand ( InputInterface $input ) {
22+
// Get the desired state and current state
23+
$state = $input->getArgument ("state");
24+
$current = $this->_data->isEnabled ();
25+
// Check to see if the passed argument is valid
26+
if ( $state == "enable" || $state == "disable" ) {
27+
// Convert state to boolean
28+
$state = $state == "enable";
29+
// If the desired state is different then the current one
30+
if ( $state != $current ) {
31+
// Set the state value in the store config
32+
$stateValue = $state ? Config::VARNISH : Config::BUILT_IN;
33+
$this->_data->setCachingApplication ( $stateValue );
34+
// Construct the message and respond to caller
35+
$message = $state ? "Varnish" : "Built-in";
36+
$message = "<options=underscore>" . $message . " Cache</>";
37+
$message = "Caching application is now set to " . $message;
38+
return [ "status" => true, "message" => $message ];
39+
}
40+
// Respond to caller that the desired argument is already in place
41+
$message = "Requested state already in place";
42+
return [ "status" => false, "message" => $message ];
43+
}
44+
// Respond to caller stating what arguments are valid
45+
$message = "Invalid argument, pass either enable or disable as an argument";
46+
return [ "status" => false, "message" => $message ];
47+
}
48+
49+
}

src/app/code/JetRails/Varnish/etc/di.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
<type name="Magento\Framework\Console\CommandList" >
77
<arguments>
88
<argument name="commands" xsi:type="array" >
9-
<item name="cache:status" xsi:type="object" >JetRails\Varnish\Console\Command\Cache\Status</item>
10-
<item name="cache:enable" xsi:type="object" >JetRails\Varnish\Console\Command\Cache\Enable</item>
11-
<item name="cache:disable" xsi:type="object" >JetRails\Varnish\Console\Command\Cache\Disable</item>
12-
<item name="config:show" xsi:type="object" >JetRails\Varnish\Console\Command\Config\Show</item>
9+
<item name="status" xsi:type="object" >JetRails\Varnish\Console\Command\Status</item>
10+
<item name="status:set" xsi:type="object" >JetRails\Varnish\Console\Command\Status\Set</item>
11+
<!-- <item name="config:show" xsi:type="object" >JetRails\Varnish\Console\Command\Config\Show</item> -->
1312
<item name="purge:url" xsi:type="object" >JetRails\Varnish\Console\Command\Purge\Url</item>
14-
<item name="purge:store" xsi:type="object" >JetRails\Varnish\Console\Command\Purge\Store</item>
15-
<item name="purge:all" xsi:type="object" >JetRails\Varnish\Console\Command\Purge\All</item>
13+
<!-- <item name="purge:store" xsi:type="object" >JetRails\Varnish\Console\Command\Purge\Store</item> -->
14+
<!-- <item name="purge:all" xsi:type="object" >JetRails\Varnish\Console\Command\Purge\All</item> -->
1615
</argument>
1716
</arguments>
1817
</type>

src/app/code/JetRails/Varnish/etc/templates/default.vcl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ vcl 4.0;
22

33
import std;
44

5-
# The minimal Varnish version is 4.0
6-
# For SSL offloading, pass the following header in your proxy server or load balancer: 'X-Forwarded-Proto: https'
7-
85
backend default {
96
.host = "{{_BACKEND_HOST_}}";
107
.port = "{{_BACKEND_PORT_}}";

0 commit comments

Comments
 (0)