Permalink
Browse files

initial import: Magneto_Varnish module created; draft varnish configu…

…ration added
  • Loading branch information...
0 parents commit 8b13320781582987acf1eb7cfe567283d9b4b808 @madalinoprea committed May 5, 2011
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<config>
+ <modules>
+ <Magneto_Varnish>
+ <active>true</active>
+ <codePool>community</codePool>
+ </Magneto_Varnish>
+ </modules>
+</config>
0 README
No changes.
@@ -0,0 +1,28 @@
+<?php
+
+class Magneto_Varnish_Helper_Data extends Mage_Core_Helper_Abstract
+{
+
+ public function purgeAll()
+ {
+
+ }
+
+ public function purge(array $urls)
+ {
+ try {
+ $varnishUrl = Mage::getBaseUrl();
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_URL, $varnishUrl);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+ $response = curl_exec($ch);
+ curl_close($ch);
+ }
+ catch(Exception e) {
+ Mage::log('Curl exception ' . $e->getFile(). ' ' . $e->getLine() . ' ' . $e->getMessage());
+ }
+ }
+}
@@ -0,0 +1,156 @@
+<?php
+
+class Magneto_Varnish_Model_Observer {
+
+ public function getCookie()
+ {
+ return Mage::app()->getCookie();
+ }
+
+ /**
+ * @param $observer Varien_Event_Observer
+ */
+ public function varnish(Varien_Event_Observer $observer)
+ {
+ $event = $observer->getEvent();
+ $response = $observer->getResponse();
+ }
+
+ public function purgeCache($observer)
+ {
+ $tags = $observer->getTags();
+ $urls = array();
+ // Mage::log("Tags: " . get_class($tags) . ' = ' . var_export($tags, true));
+
+ // compute the urls for affected entities
+ foreach ((array)$tags as $tag) {
+ //catalog_product_100 or catalog_category_186
+ $tag_fields = explode('_', $tag);
+ if (count($tag_fields)==3) {
+ if ($tag_fields[1]=='product') {
+ // Mage::log("Purge urls for product " . $tag_fields[2]);
+
+ // get urls for product
+ $product = Mage::getModel('catalog/product')->load($tag_fields[2]);
+ $urls = array_merge($urls, $this->_getUrlsForProduct($product));
+ } elseif ($tag_fields[1]=='category') {
+ // Mage::log('Purge urls for category ' . $tag_fields[2]);
+
+ $category = Mage::getModel('catalog/category')->load($tag_fields[2]);
+ $category_urls = $this->_getUrlsForCategory($category);
+ $urls = array_merge($urls, $category_urls);
+ }
+ }
+ }
+ Mage::log("purge all urls: " . var_export($urls, true));
+ // FIXME: Perform the actual purge
+ }
+
+ /**
+ * Returns all the urls related to product
+ * @param Mage_Catalog_Model_Product $product
+ */
+ private function _getUrlsForProduct($product){
+ $urls = array();
+
+ $store_id = $product->getStoreId();
+
+ $routePath = 'catalog/product/view';
+ $routeParams['id'] = $product->getId();
+ $routeParams['s'] = $product->getUrlKey();
+ $routeParams['_store'] = (!$store_id ? 1: $store_id);
+ $url = Mage::getUrl($routePath, $routeParams);
+ $urls[] = $url;
+
+ // Collect all rewrites
+ $rewrites = Mage::getModel('core/url_rewrite')->getCollection();
+ if (!Mage::getConfig('catalog/seo/product_use_categories')) {
+ $rewrites->getSelect()
+ ->where("id_path = 'product/{$product->getId()}'");
+ } else {
+ // Also show full links with categories
+ $rewrites->getSelect()
+ ->where("id_path = 'product/{$product->getId()}' OR id_path like 'product/{$product->getId()}/%'");
+ }
+ foreach($rewrites as $r) {
+ unset($routeParams);
+ $routePath = '';
+ $routeParams['_direct'] = $r->getRequestPath();
+ $routeParams['_store'] = $r->getStoreId();
+ $url = Mage::getUrl($routePath, $routeParams);
+ $urls[] = $url;
+ }
+
+ return $urls;
+ }
+
+
+ /**
+ * Returns all the urls pointing to the category
+ */
+ private function _getUrlsForCategory($category) {
+ $urls = array();
+ $routePath = 'catalog/category/view';
+
+ $store_id = $category->getStoreId();
+ $routeParams['id'] = $category->getId();
+ $routeParams['s'] = $category->getUrlKey();
+ $routeParams['_store'] = (!$store_id ? 1 : $store_id); # Default store id is 1
+ $url = Mage::getUrl($routePath, $routeParams);
+ $urls[] = $url;
+
+ // Collect all rewrites
+ $rewrites = Mage::getModel('core/url_rewrite')->getCollection();
+ $rewrites->getSelect()->where("id_path = 'category/{$category->getId()}'");
+ foreach($rewrites as $r) {
+ unset($routeParams);
+ $routePath = '';
+ $routeParams['_direct'] = $r->getRequestPath();
+ $routeParams['_store'] = $r->getStoreId();
+ $routeParams['_nosid'] = True;
+ $url = Mage::getUrl($routePath, $routeParams);
+ $urls[] = $url;
+ }
+
+ return $urls;
+ }
+
+
+ public function turnOnCache()
+ {
+ $this->getCookie()->delete('nocache');
+ }
+
+ public function turnOffCache()
+ {
+ $this->getCookie()->set('nocache', 1);
+ }
+
+ public function canCache()
+ {
+ return true;
+ }
+
+ protected function quoteHasItems()
+ {
+ $quote = Mage::getSingleton('checkout/session')->getQuote();
+ if($quote instanceof Mage_Sales_Model_Quote && $quote->hasItems()) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public function customerIsLogged()
+ {
+ $customerSession = Mage::getSingleton('customer/session');
+ if ($customerSession instanceof Mage_Customer_Model_Session &&
+ $customerSession->isLoggedIn() ){
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+}
+
@@ -0,0 +1,9 @@
+<?php
+class Magneto_Varnish_IndexController extends Mage_Core_Controller_Front_Action
+{
+ public function purgeAllAction()
+ {
+
+ }
+}
+
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<config>
+ <modules>
+ <Magneto_Varnish>
+ <version>0.1.0</version>
+ </Magneto_Varnish>
+ </modules>
+ <frontend>
+ <routers>
+ <varnish>
+ <use>standard</use>
+ <args>
+ <module>Magneto_Varnish</module>
+ <frontName>varnish</frontName>
+ </args>
+ </varnish>
+ </routers>
+ </frontend>
+ <global>
+ <models>
+ <varnish>
+ <class>Magneto_Varnish_Model</class>
+ <resourceModel>varnish_mysql4</resourceModel>
+ </varnish>
+ </models>
+ <helpers>
+ <varnish>
+ <class>Magneto_Varnish_Helper</class>
+ </varnish>
+ </helpers>
+ <events>
+ <http_response_send_before>
+ <observers>
+ <varnish>
+ <type>singleton</type>
+ <class>varnish/observer</class>
+ <method>varnish</method>
+ </varnish>
+ </observers>
+ </http_response_send_before>
+ <application_clean_cache>
+ <observers>
+ <varnish>
+ <type>singleton</type>
+ <class>varnish/observer</class>
+ <method>purgeCache</method>
+ </varnish>
+ </observers>
+ </application_clean_cache>
+ </events>
+ </global>
+</config>
5 modman
@@ -0,0 +1,5 @@
+# Comments are supported, begin a line with a hash
+code/Varnish app/code/community/Magneto/Varnish
+#design/layout/varnish.xml app/design/frontend/base/default/layout/varnish.xml
+#design/template/varnish app/design/frontend/base/default/template/varnish
+Magneto_Varnish.xml app/etc/modules/Magneto_Varnish.xml
Oops, something went wrong.

0 comments on commit 8b13320

Please sign in to comment.