|
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
|
|
|
!defined('CM_REDISSESSION_LOCKING_ENABLED') ) { |
|
70
|
|
|
define('CM_REDISSESSION_LOCKING_ENABLED', false); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Re-apply and save Varnish configuration on config change |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $eventObject |
|
79
|
|
|
* @return null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function adminSystemConfigChangedSection($eventObject) { |
|
82
|
|
|
if (Mage::helper('turpentine/varnish')->getVarnishEnabled() && |
|
83
|
|
|
Mage::helper('turpentine/data')->getAutoApplyOnSave()) { |
|
84
|
|
|
$result = Mage::getModel('turpentine/varnish_admin')->applyConfig(); |
|
85
|
|
|
$session = Mage::getSingleton('core/session'); |
|
86
|
|
|
foreach ($result as $name => $value) { |
|
87
|
|
|
if ($value === true) { |
|
88
|
|
|
$session->addSuccess(Mage::helper('turpentine/data') |
|
89
|
|
|
->__('VCL successfully applied to: '.$name)); |
|
90
|
|
|
} else { |
|
91
|
|
|
$session->addError(Mage::helper('turpentine/data') |
|
92
|
|
|
->__(sprintf('Failed to apply the VCL to %s: %s', |
|
93
|
|
|
$name, $value))); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
$cfgr = Mage::getModel('turpentine/varnish_admin')->getConfigurator(); |
|
97
|
|
|
if (is_null($cfgr)) { |
|
98
|
|
|
$session->addError(Mage::helper('turpentine/data') |
|
99
|
|
|
->__('Failed to load configurator')); |
|
100
|
|
|
} else { |
|
101
|
|
|
$result = $cfgr->save($cfgr->generate()); |
|
102
|
|
|
if ($result[0]) { |
|
103
|
|
|
$session->addSuccess(Mage::helper('turpentine/data') |
|
104
|
|
|
->__('The VCL file has been saved.')); |
|
105
|
|
|
} else { |
|
106
|
|
|
$session->addError(Mage::helper('turpentine/data') |
|
107
|
|
|
->__('Failed to save the VCL file: '.$result[1]['message'])); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.