-
Notifications
You must be signed in to change notification settings - Fork 1
/
Abstract.php
301 lines (272 loc) · 8.76 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Magento Ves Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Ves Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/ves-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Ves
* @package Ves_Optimize
* @copyright Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/ves-edition
*/
/**
* Abstract placeholder container
*/
abstract class Ves_Optimize_Model_Container_Abstract
{
/**
* @var null|Ves_Optimize_Model_Processor
*/
protected $_processor;
/**
* Placeholder instance
*
* @var Ves_Optimize_Model_Container_Placeholder
*/
protected $_placeholder;
/**
* Placeholder block instance
*
* @var Mage_Core_Block_Abstract
*/
protected $_placeholderBlock;
/**
* Class constructor
*
* @param Ves_Optimize_Model_Container_Placeholder $placeholder
*/
public function __construct($placeholder)
{
$this->_placeholder = $placeholder;
}
/**
* Get container individual cache id
*
* @return string|false
*/
protected function _getCacheId()
{
return false;
}
/**
* Generate placeholder content before application was initialized and apply to page content if possible
*
* @param string $content
* @return bool
*/
public function applyWithoutApp(&$content)
{
$cacheId = $this->_getCacheId();
if ($cacheId === false) {
$this->_applyToContent($content, '');
return true;
}
$block = $this->_loadCache($cacheId);
if ($block === false) {
return false;
}
$block = Ves_Optimize_Helper_Url::replaceUenc($block);
$this->_applyToContent($content, $block);
return true;
}
/**
* Generate and apply container content in controller after application is initialized
*
* @param string $content
* @return bool
*/
public function applyInApp(&$content)
{
$blockContent = $this->_renderBlock();
if ($blockContent === false) {
return false;
}
if (Mage::getStoreConfig(Ves_Optimize_Model_Processor::XML_PATH_CACHE_DEBUG)) {
$debugBlock = new Ves_Optimize_Block_Debug();
$debugBlock->setDynamicBlockContent($blockContent);
$debugBlock->setTags($this->_getPlaceHolderBlock()->getCacheTags());
$debugBlock->setType($this->_placeholder->getName());
$this->_applyToContent($content, $debugBlock->toHtml());
} else {
$this->_applyToContent($content, $blockContent);
}
$subprocessor = $this->_processor->getSubprocessor();
if ($subprocessor) {
$contentWithoutNestedBlocks = $subprocessor->replaceContentToPlaceholderReplacer($blockContent);
$this->saveCache($contentWithoutNestedBlocks);
}
return true;
}
/**
* Save rendered block content to cache storage
*
* @param string $blockContent
* @param array $tags
* @return Ves_Optimize_Model_Container_Abstract
*/
public function saveCache($blockContent, $tags = array())
{
$cacheId = $this->_getCacheId();
if ($cacheId !== false) {
$this->_saveCache($blockContent, $cacheId, $tags);
}
return $this;
}
/**
* Render block content from placeholder
*
* @return string|false
*/
protected function _renderBlock()
{
return false;
}
/**
* Replace container placeholder in content on container content
*
* @param string $content
* @param string $containerContent
*/
protected function _applyToContent(&$content, $containerContent)
{
$containerContent = $this->_placeholder->getStartTag() . $containerContent . $this->_placeholder->getEndTag();
$content = str_replace($this->_placeholder->getReplacer(), $containerContent, $content);
return $content;
//$helper = new Ves_Optimize_Helper_Filter();
//$content = str_replace('text/javascript','text/rocketscript',$content);//$helper->processContent($content);
}
/**
* Load cached data by cache id
*
* @param string $id
* @return string|false
*/
protected function _loadCache($id)
{
return Ves_Optimize_Model_Cache::getCacheInstance()->load($id);
}
/**
* Save data to cache storage
*
* @param string $data
* @param string $id
* @param array $tags
* @param null|int $lifetime
* @return Ves_Optimize_Model_Container_Abstract
*/
protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
{
$tags[] = Ves_Optimize_Model_Processor::CACHE_TAG;
$tags = array_merge($tags, $this->_getPlaceHolderBlock()->getCacheTags());
if (is_null($lifetime)) {
$lifetime = $this->_placeholder->getAttribute('cache_lifetime') ?
$this->_placeholder->getAttribute('cache_lifetime') : false;
}
Ves_Optimize_Helper_Data::prepareContentPlaceholders($data);
Ves_Optimize_Model_Cache::getCacheInstance()->save($data, $id, $tags, $lifetime);
return $this;
}
/**
* Retrieve cookie value
*
* @param string $cookieName
* @param mixed $defaultValue
* @return string
*/
protected function _getCookieValue($cookieName, $defaultValue = null)
{
return (array_key_exists($cookieName, $_COOKIE) ? $_COOKIE[$cookieName] : $defaultValue);
}
/**
* Set processor for container needs
*
* @param Ves_Optimize_Model_Processor $processor
* @return Ves_Optimize_Model_Container_Abstract
*/
public function setProcessor(Ves_Optimize_Model_Processor $processor)
{
$this->_processor = $processor;
return $this;
}
/**
* Get last visited category id
*
* @return string|null
*/
protected function _getCategoryId()
{
if ($this->_processor) {
$categoryId = $this->_processor
->getMetadata(Ves_Optimize_Model_Processor_Category::METADATA_CATEGORY_ID);
if ($categoryId) {
return $categoryId;
}
}
//If it is not product page and not category page - we have no any category (not using last visited)
if (!$this->_getProductId()) {
return null;
}
return $this->_getCookieValue(Ves_Optimize_Model_Cookie::COOKIE_CATEGORY_ID, null);
}
/**
* Get current product id
*
* @return string|null
*/
protected function _getProductId()
{
if (!$this->_processor) {
return null;
}
return $this->_processor->getMetadata(Ves_Optimize_Model_Processor_Product::METADATA_PRODUCT_ID);
}
/**
* Get current request id
*
* @return string|null
*/
protected function _getRequestId()
{
return !$this->_processor ? null : $this->_processor->getRequestId();
}
/**
* Get Placeholder Block
*
* @return Mage_Core_Block_Abstract
*/
protected function _getPlaceHolderBlock()
{
if (null === $this->_placeholderBlock) {
$blockName = $this->_placeholder->getAttribute('block');
$this->_placeholderBlock = new $blockName;
$this->_placeholderBlock->setTemplate($this->_placeholder->getAttribute('template'));
$this->_placeholderBlock->setLayout(Mage::app()->getLayout());
$this->_placeholderBlock->setSkipRenderTag(true);
}
return $this->_placeholderBlock;
}
/**
* Set placeholder block
*
* @param Mage_Core_Block_Abstract $block
* @return Ves_Optimize_Model_Container_Abstract
*/
public function setPlaceholderBlock(Mage_Core_Block_Abstract $block) {
$this->_placeholderBlock = $block;
return $this;
}
}