Skip to content

Commit 20580a2

Browse files
author
Rafael Grigorian
committed
Fixed GH-8
1 parent a9d025d commit 20580a2

File tree

4 files changed

+131
-80
lines changed

4 files changed

+131
-80
lines changed

src/app/code/JetRails/Varnish/Helper/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private function _getClientBrowser () {
8080
$type = "Unknown";
8181
$os = "Unknown";
8282
// Extract the operating system
83-
if (preg_match ( "/linux/i", $agent ) ) $os = "Linux";
83+
if ( preg_match ( "/linux/i", $agent ) ) $os = "Linux";
8484
if ( preg_match ( "/macintosh|mac os x/i", $agent ) ) $os = "Mac";
8585
if ( preg_match ( "/windows|win32/i", $agent ) ) $os = "Windows";
8686
// Extract the browser type

src/app/code/JetRails/Varnish/Helper/Purger.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use JetRails\Varnish\Helper\Data;
66
use Magento\Framework\App\Helper\AbstractHelper;
77
use Magento\Store\Model\StoreManagerInterface;
8+
use Magento\UrlRewrite\Model\UrlFinderInterface;
9+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
810

911
/**
1012
* Purger.php - This class is encapsulated with purge specific methods in order to use them on
@@ -24,20 +26,56 @@ class Purger extends AbstractHelper {
2426
* the class using dependency injection on runtime.
2527
* @var Data _data Instance of the Data helper class
2628
* @var Data _storeManager Instance of the StoreManager
29+
*
30+
*
31+
*
32+
*
2733
*/
2834
protected $_data;
2935
protected $_storeManager;
36+
protected $_urlFinder;
3037

3138
/**
3239
* This constructor is overloaded from the parent class in order to use dependency injection
3340
* to get the dependency classes that we need for this module's command actions to execute.
3441
* @var Data data Instance of the Data helper class
3542
* @var Data storeManager Instance of the StoreManager
43+
*
3644
*/
37-
public function __construct ( Data $data, StoreManagerInterface $storeManager ) {
45+
public function __construct (
46+
Data $data,
47+
StoreManagerInterface $storeManager,
48+
UrlFinderInterface $urlFinder
49+
) {
3850
// Save the injected class instances
3951
$this->_data = $data;
4052
$this->_storeManager = $storeManager;
53+
$this->_urlFinder = $urlFinder;
54+
}
55+
56+
/**
57+
* This is a helper method that helps resolve url rewrites. It looks for all url rewrites with a
58+
* given target path. It then uses this target path to find all request paths that lead to said
59+
* target path. This search is done recursively, so if the rewrite is not direct and instead there
60+
* are many rewrites that lead to the target path, we will find them all. This method is also
61+
* acyclic therefore if there is a cycle in the rewrite logic, we won't fall for it.
62+
* @param string targetPath Target path to look for
63+
* @param array visited Already seen target paths
64+
* @return array Request paths that lead to target path
65+
*/
66+
public function getUrlRewrites ( $targetPath, $visited = [] ) {
67+
// Base case, if already seen then return
68+
if ( in_array ( $targetPath, $visited ) ) return [];
69+
array_push ( $visited, $targetPath );
70+
// Find all rewrites with target path and recursively find the derivatives
71+
$rewrites = $this->_urlFinder->findAllByData ( [ UrlRewrite::TARGET_PATH => $targetPath ] );
72+
$rewrites = array_map ( function ( $i ) { return $i->getRequestPath (); }, $rewrites );
73+
$results = array_merge ( [ $targetPath ], $rewrites );
74+
foreach ( $rewrites as $rewrite ) {
75+
$results = array_merge ( $results, $this->getUrlRewrites ( $rewrite, $visited ) );
76+
}
77+
// Return a unique set of rewrites
78+
return array_unique ( $results );
4179
}
4280

4381
/**
@@ -188,4 +226,4 @@ public function purgeAll () {
188226
return $this->_purge ( $url, $additionalHeaders );
189227
}
190228

191-
}
229+
}

src/app/code/JetRails/Varnish/Observer/Save/Page.php

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use JetRails\Varnish\Helper\Data;
66
use JetRails\Varnish\Helper\Purger;
77
use JetRails\Varnish\Logger\Logger;
8-
use Magento\Cms\Helper\Page as CmsPage;
98
use Magento\Framework\Event\Observer;
109
use Magento\Framework\Event\ObserverInterface;
1110
use Magento\Framework\Message\ManagerInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
1212

1313
/**
1414
* Page.php - This observer is triggered when the CMS page save event is fired. It then finds
@@ -28,37 +28,37 @@ class Page implements ObserverInterface {
2828
* @var Data _data Instance of the Data helper class
2929
* @var Logger _logger Instance of the custom Logger class
3030
* @var ManagerInterface _messageManager Instance of the ManagerInterface
31-
* @var CmsPage _page Instance of the CmsPage
3231
* @var Purger _purger Instance of the Purger helper class
32+
* @var StoreManager _storeManager Instance of the StoreManager
3333
*/
3434
protected $_data;
3535
protected $_logger;
3636
protected $_messageManager;
37-
protected $_page;
3837
protected $_purger;
38+
protected $_storeManager;
3939

4040
/**
4141
* This constructor is overloaded from the parent class in order to use dependency injection
4242
* to get the dependency classes that we need for this module's command actions to execute.
4343
* @param Data data Instance of the Data helper class
4444
* @param Logger logger Instance of the custom Logger class
4545
* @param ManagerInterface messageManager Instance of the ManagerInterface
46-
* @param CmsPage page Instance of the CmsPage
4746
* @param Purger purger Instance of the Purger helper class
47+
* @param StoreManager storeManager Instance of the StoreManager
4848
*/
4949
public function __construct (
5050
Data $data,
5151
Logger $logger,
52-
CmsPage $page,
5352
ManagerInterface $messageManager,
54-
Purger $purger
53+
Purger $purger,
54+
StoreManagerInterface $storeManager
5555
) {
5656
// Save injected class instances
5757
$this->_data = $data;
5858
$this->_logger = $logger;
59-
$this->_page = $page;
6059
$this->_messageManager = $messageManager;
6160
$this->_purger = $purger;
61+
$this->_storeManager = $storeManager;
6262
}
6363

6464
/**
@@ -71,37 +71,44 @@ public function __construct (
7171
public function execute ( Observer $observer ) {
7272
// Check to see if event is enabled
7373
if ( $this->_data->isEnabled () && $this->_data->shouldPurgeAfterCmsPageSave () ) {
74-
// Get the page url
75-
$pageUrl = $this->_page->getPageUrl ( $observer->getPage ()->getId () );
76-
$temp = explode ( "?", $pageUrl );
77-
$pageUrl = reset ( $temp );
78-
// Validate the url
79-
$url = $this->_purger->validateUrl ( $pageUrl );
80-
// Loop though responses
81-
foreach ( $this->_purger->purgeUrl ( $url ) as $response ) {
82-
// Log what we are trying to do
83-
$message = [
84-
"status" => $response->status,
85-
"action" => "auto_purge:cms_save",
86-
"target" => $response->target,
87-
"server" => $response->server
88-
];
89-
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
90-
// Check to see if response was successful
91-
if ( $response->status == 200 ) {
92-
// Add success response message
93-
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
94-
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
95-
$message = "Successfully purged varnish cache for $targetHtml on $serverHtml";
96-
$this->_messageManager->addSuccess ( $message );
97-
}
98-
else {
99-
// Otherwise add an error message
100-
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
101-
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
102-
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
103-
$message = "Error Purging varnish cache for $targetHtml on $serverHtml with response code $statusHtml";
104-
$this->_messageManager->addError ( $message );
74+
// Get the base url for the current store
75+
$baseUrl = $this->_storeManager->getStore ()->getBaseUrl ();
76+
// Get the product id and retrieve all rewrites recursively and acyclicly.
77+
$pid = $observer->getPage ()->getId ();
78+
$rewrites = $this->_purger->getUrlRewrites ("cms/page/view/page_id/$pid");
79+
// Define purge command for each found URL
80+
$actions = array_fill_keys ( $rewrites, "purgeUrl" );
81+
$actions ["cms/page/view/page_id/$pid/"] = "purgeStore"; // note the trailing slash
82+
// Loop through each rewrite
83+
foreach ( $actions as $rewrite => $command ) {
84+
// Validate the url
85+
$url = $this->_purger->validateUrl ( $baseUrl . $rewrite );
86+
// Loop though responses
87+
foreach ( $this->_purger->{ $command } ( $url ) as $response ) {
88+
// Log what we are trying to do
89+
$message = [
90+
"status" => $response->status,
91+
"action" => "auto_purge:cms_save",
92+
"target" => $response->target,
93+
"server" => $response->server
94+
];
95+
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
96+
// Check to see if response was successful
97+
if ( $response->status == 200 ) {
98+
// Add success response message
99+
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
100+
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
101+
$message = "Successfully purged varnish cache for $targetHtml on $serverHtml";
102+
$this->_messageManager->addSuccess ( $message );
103+
}
104+
else {
105+
// Otherwise add an error message
106+
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
107+
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
108+
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
109+
$message = "Error Purging varnish cache for $targetHtml on $serverHtml with response code $statusHtml";
110+
$this->_messageManager->addError ( $message );
111+
}
105112
}
106113
}
107114
}

src/app/code/JetRails/Varnish/Observer/Save/Product.php

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Magento\Framework\Event\Observer;
99
use Magento\Framework\Event\ObserverInterface;
1010
use Magento\Framework\Message\ManagerInterface;
11-
use Magento\Catalog\Helper\Product as ProductHelper;
11+
use Magento\Store\Model\StoreManagerInterface;
1212

1313
/**
1414
* Product.php - This observer is triggered when the product save event is fired. It then finds
@@ -29,37 +29,37 @@ class Product implements ObserverInterface {
2929
* @var Data _data Instance of the Data helper class
3030
* @var Logger _logger Instance of the custom Logger class
3131
* @var ManagerInterface _messageManager Instance of the ManagerInterface
32-
* @var ProductHelper _productHelper Instance of the ProductHelper
3332
* @var Purger _purger Instance of the Purger helper class
33+
* @var StoreManager _storeManager Instance of the StoreManager
3434
*/
3535
protected $_data;
3636
protected $_logger;
3737
protected $_messageManager;
38-
protected $_productHelper;
3938
protected $_purger;
39+
protected $_storeManager;
4040

4141
/**
4242
* This constructor is overloaded from the parent class in order to use dependency injection
4343
* to get the dependency classes that we need for this module's command actions to execute.
4444
* @param Data data Instance of the Data helper class
4545
* @param Logger logger Instance of the custom Logger class
4646
* @param ManagerInterface messageManager Instance of the ManagerInterface
47-
* @param ProductHelper productHelper Instance of the ProductHelper
4847
* @param Purger purger Instance of the Purger helper class
48+
* @param StoreManager storeManager Instance of the StoreManager
4949
*/
5050
public function __construct (
5151
Data $data,
5252
Logger $logger,
5353
ManagerInterface $messageManager,
54-
ProductHelper $productHelper,
55-
Purger $purger
54+
Purger $purger,
55+
StoreManagerInterface $storeManager
5656
) {
5757
// Save the injected class instances
5858
$this->_data = $data;
5959
$this->_logger = $logger;
6060
$this->_messageManager = $messageManager;
61-
$this->_productHelper = $productHelper;
6261
$this->_purger = $purger;
62+
$this->_storeManager = $storeManager;
6363
}
6464

6565
/**
@@ -72,38 +72,44 @@ public function __construct (
7272
public function execute ( Observer $observer ) {
7373
// Check to see if event is enabled
7474
if ( $this->_data->isEnabled () && $this->_data->shouldPurgeAfterProductSave () ) {
75-
// Get the product and the product url specific to store view
76-
$product = $observer->getProduct ();
77-
$productUrl = $this->_productHelper->getProductUrl ( $product->getId () );
78-
$temp = explode ( "/key/", $productUrl );
79-
$productUrl = reset ( $temp );
80-
// Validate the url
81-
$url = $this->_purger->validateUrl ( $productUrl );
82-
// Loop though responses, after purging store (store so we can use starts with for url)
83-
foreach ( $this->_purger->purgeStore ( $url ) as $response ) {
84-
// Log what we are trying to do
85-
$message = [
86-
"status" => $response->status,
87-
"action" => "auto_purge:product_save",
88-
"target" => $response->target,
89-
"server" => $response->server
90-
];
91-
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
92-
// Check to see if response was successful
93-
if ( $response->status == 200 ) {
94-
// Add success response message
95-
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
96-
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
97-
$message = "Successfully purged varnish cache for $targetHtml on $serverHtml";
98-
$this->_messageManager->addSuccess ( $message );
99-
}
100-
else {
101-
// Otherwise add an error message
102-
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
103-
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
104-
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
105-
$message = "Error Purging varnish cache for $targetHtml on $serverHtml with response code $statusHtml";
106-
$this->_messageManager->addError ( $message );
75+
// Get the base url for the current store
76+
$baseUrl = $this->_storeManager->getStore ()->getBaseUrl ();
77+
// Get the product id and retrieve all rewrites recursively and acyclicly.
78+
$pid = $observer->getProduct ()->getId ();
79+
$rewrites = $this->_purger->getUrlRewrites ("catalog/product/view/id/$pid");
80+
// Define purge command for each found URL
81+
$actions = array_fill_keys ( $rewrites, "purgeUrl" );
82+
$actions ["catalog/product/view/id/$pid/"] = "purgeStore"; // note the trailing slash
83+
// Loop through each rewrite
84+
foreach ( $actions as $rewrite => $command ) {
85+
// Validate the url
86+
$url = $this->_purger->validateUrl ( $baseUrl . $rewrite );
87+
// Loop though responses, after purging store (store so we can use 'starts with' for url)
88+
foreach ( $this->_purger->{ $command } ( $url ) as $response ) {
89+
// Log what we are trying to do
90+
$message = [
91+
"status" => $response->status,
92+
"action" => "auto_purge:product_save",
93+
"target" => $response->target,
94+
"server" => $response->server
95+
];
96+
$this->_logger->blame ( $this->_data->getLoggedInUserInfo (), $message );
97+
// Check to see if response was successful
98+
if ( $response->status == 200 ) {
99+
// Add success response message
100+
$targetHtml = "<font color='#79A22E' ><b>$response->target</b></font>";
101+
$serverHtml = "<font color='#79A22E' ><b>$response->server</b></font>";
102+
$message = "Successfully purged varnish cache for $targetHtml on $serverHtml";
103+
$this->_messageManager->addSuccess ( $message );
104+
}
105+
else {
106+
// Otherwise add an error message
107+
$targetHtml = "<font color='#E22626' ><b>$response->target</b></font>";
108+
$serverHtml = "<font color='#E22626' ><b>$response->server</b></font>";
109+
$statusHtml = "<font color='#E22626' ><b>$response->status</b></font>";
110+
$message = "Error Purging varnish cache for $targetHtml on $serverHtml with response code $statusHtml";
111+
$this->_messageManager->addError ( $message );
112+
}
107113
}
108114
}
109115
}

0 commit comments

Comments
 (0)