From 8eb58b6de6c3a1b60eb296d4a4dc30f537a0458d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20G=C3=A1l?= Date: Fri, 26 Aug 2016 17:49:14 +0200 Subject: [PATCH 1/2] Listen to cache:flush events - if magerun cache:flush runs - if admin is pressing on "flush magento cache" - if admin is pressing on "flush cache storage" Then the DI container files should be removed accordingly. This also fixes a bug when during capistrano deployments, the container were not cleaned up (as 99% of times only magerun cache:flush is called) --- .../SymfonyContainer/Model/Observer.php | 29 ++++++++++++------- .../Inviqa/SymfonyContainer/etc/config.xml | 20 ++++++++++++- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php b/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php index 7b13fde..d76a662 100644 --- a/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php +++ b/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php @@ -12,19 +12,12 @@ class Inviqa_SymfonyContainer_Model_Observer public function onCacheRefresh(Varien_Event_Observer $event) { - if (ConfigurationBuilder::MODEL_ALIAS === $event->getType()) { - $containerFilePath = $this->containerCachePath(); - $metaFilePath = $this->containerCacheMetaPath(); - - if (file_exists($containerFilePath)) { - unlink($containerFilePath); - } - - if (file_exists($metaFilePath)) { - unlink($metaFilePath); - } + $eventType = $event->getType(); + if ($eventType === ConfigurationBuilder::MODEL_ALIAS || is_null($eventType)) { + $this->clearCache(); } } + public function onPreDispatch(Varien_Event_Observer $event) { $controller = $event->getControllerAction(); @@ -34,6 +27,20 @@ public function onPreDispatch(Varien_Event_Observer $event) ])->setupDependencies($controller); } + private function clearCache() + { + $containerFilePath = $this->containerCachePath(); + $metaFilePath = $this->containerCacheMetaPath(); + + if (file_exists($containerFilePath)) { + unlink($containerFilePath); + } + + if (file_exists($metaFilePath)) { + unlink($metaFilePath); + } + } + private function containerCachePath() { return Mage::getBaseDir('cache') . DIRECTORY_SEPARATOR . ConfigurationBuilder::CACHED_CONTAINER; diff --git a/app/code/community/Inviqa/SymfonyContainer/etc/config.xml b/app/code/community/Inviqa/SymfonyContainer/etc/config.xml index 35c408d..b2d4895 100644 --- a/app/code/community/Inviqa/SymfonyContainer/etc/config.xml +++ b/app/code/community/Inviqa/SymfonyContainer/etc/config.xml @@ -22,6 +22,24 @@ + + + + Inviqa_SymfonyContainer_Model_Observer + singleton + onCacheRefresh + + + + + + + Inviqa_SymfonyContainer_Model_Observer + singleton + onCacheRefresh + + + @@ -43,4 +61,4 @@ - \ No newline at end of file + From 25d68833646f51d7111f4512ab05c43134fb3c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20G=C3=A1l?= Date: Mon, 12 Sep 2016 11:25:42 +0200 Subject: [PATCH 2/2] Improve code readability/understandability by separating flush event cache cleans to separate method --- .../community/Inviqa/SymfonyContainer/Model/Observer.php | 8 ++++++-- app/code/community/Inviqa/SymfonyContainer/etc/config.xml | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php b/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php index d76a662..5d5e8b3 100644 --- a/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php +++ b/app/code/community/Inviqa/SymfonyContainer/Model/Observer.php @@ -12,12 +12,16 @@ class Inviqa_SymfonyContainer_Model_Observer public function onCacheRefresh(Varien_Event_Observer $event) { - $eventType = $event->getType(); - if ($eventType === ConfigurationBuilder::MODEL_ALIAS || is_null($eventType)) { + if ($event->getType() === ConfigurationBuilder::MODEL_ALIAS) { $this->clearCache(); } } + public function onCacheFlush() + { + $this->clearCache(); + } + public function onPreDispatch(Varien_Event_Observer $event) { $controller = $event->getControllerAction(); diff --git a/app/code/community/Inviqa/SymfonyContainer/etc/config.xml b/app/code/community/Inviqa/SymfonyContainer/etc/config.xml index b2d4895..f23cb53 100644 --- a/app/code/community/Inviqa/SymfonyContainer/etc/config.xml +++ b/app/code/community/Inviqa/SymfonyContainer/etc/config.xml @@ -27,7 +27,7 @@ Inviqa_SymfonyContainer_Model_Observer singleton - onCacheRefresh + onCacheFlush @@ -36,7 +36,7 @@ Inviqa_SymfonyContainer_Model_Observer singleton - onCacheRefresh + onCacheFlush