1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nexcess.net Turpentine Extension for Magento |
5
|
|
|
* Copyright (C) 2012 Nexcess.net L.L.C. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Nexcessnet_Turpentine_Model_Observer_Varnish extends Varien_Event_Observer { |
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Check sentinel flags and set headers/cookies as needed |
25
|
|
|
* |
26
|
|
|
* Events: http_response_send_before |
27
|
|
|
* |
28
|
|
|
* @param mixed $eventObject |
29
|
|
|
* @return null |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function setCacheFlagHeader($eventObject) { |
|
|
|
|
32
|
|
|
$response = $eventObject->getResponse(); |
33
|
|
|
if (Mage::helper('turpentine/varnish')->shouldResponseUseVarnish()) { |
34
|
|
|
$response->setHeader('X-Turpentine-Cache', |
35
|
|
|
Mage::registry('turpentine_nocache_flag') ? '0' : '1'); |
36
|
|
|
if (Mage::helper('turpentine/varnish')->getVarnishDebugEnabled()) { |
37
|
|
|
Mage::helper('turpentine/debug')->logDebug( |
38
|
|
|
'Set Varnish cache flag header to: '. |
39
|
|
|
(Mage::registry('turpentine_nocache_flag') ? '0' : '1') ); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add a rewrite for catalog/product_list_toolbar if config option enabled |
46
|
|
|
* |
47
|
|
|
* @param Varien_Object $eventObject |
48
|
|
|
* @return null |
49
|
|
|
*/ |
50
|
|
|
public function addProductListToolbarRewrite($eventObject) { |
51
|
|
|
if (Mage::helper('turpentine/varnish')->shouldFixProductListToolbar()) { |
52
|
|
|
Mage::getSingleton('turpentine/shim_mage_core_app') |
53
|
|
|
->shim_addClassRewrite('block', 'catalog', 'product_list_toolbar', |
54
|
|
|
'Nexcessnet_Turpentine_Block_Catalog_Product_List_Toolbar'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Turpentine sets the fake cookie 'frontend=crawler-session' when a crawler is detected. |
60
|
|
|
* This causes lock problems with Cm_RedisSession, because all crawler hits are requesting the same session lock. |
61
|
|
|
* Cm_RedisSession provides the define CM_REDISSESSION_LOCKING_ENABLED to overrule if locking should be enabled. |
62
|
|
|
* |
63
|
|
|
* @param $eventObject |
64
|
|
|
* @return null |
65
|
|
|
*/ |
66
|
|
|
public function fixCmRedisSessionLocks($eventObject) { |
|
|
|
|
67
|
|
|
if (Mage::helper('core')->isModuleEnabled('Cm_RedisSession')) { |
68
|
|
|
if (!empty($_COOKIE['frontend']) && 'crawler-session' == $_COOKIE['frontend']) { |
69
|
|
|
define('CM_REDISSESSION_LOCKING_ENABLED', false); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Re-apply and save Varnish configuration on config change |
76
|
|
|
* |
77
|
|
|
* @param mixed $eventObject |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function adminSystemConfigChangedSection($eventObject) { |
81
|
|
|
if (Mage::helper('turpentine/varnish')->getVarnishEnabled() && |
82
|
|
|
Mage::helper('turpentine/data')->getAutoApplyOnSave()) { |
83
|
|
|
$result = Mage::getModel('turpentine/varnish_admin')->applyConfig(); |
84
|
|
|
$session = Mage::getSingleton('core/session'); |
85
|
|
|
foreach ($result as $name => $value) { |
86
|
|
|
if ($value === true) { |
87
|
|
|
$session->addSuccess(Mage::helper('turpentine/data') |
88
|
|
|
->__('VCL successfully applied to: '.$name)); |
89
|
|
|
} else { |
90
|
|
|
$session->addError(Mage::helper('turpentine/data') |
91
|
|
|
->__(sprintf('Failed to apply the VCL to %s: %s', |
92
|
|
|
$name, $value))); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
$cfgr = Mage::getModel('turpentine/varnish_admin')->getConfigurator(); |
96
|
|
|
if (is_null($cfgr)) { |
97
|
|
|
$session->addError(Mage::helper('turpentine/data') |
98
|
|
|
->__('Failed to load configurator')); |
99
|
|
|
} else { |
100
|
|
|
$result = $cfgr->save($cfgr->generate()); |
101
|
|
|
if ($result[0]) { |
102
|
|
|
$session->addSuccess(Mage::helper('turpentine/data') |
103
|
|
|
->__('The VCL file has been saved.')); |
104
|
|
|
} else { |
105
|
|
|
$session->addError(Mage::helper('turpentine/data') |
106
|
|
|
->__('Failed to save the VCL file: '.$result[1]['message'])); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.