|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @category Zookal_Mock |
|
5
|
|
|
* @package Model |
|
6
|
|
|
* @author Cyrill Schumacher | {firstName}@{lastName}.fm | @SchumacherFM |
|
7
|
|
|
* @copyright Copyright (c) Zookal Pty Ltd |
|
8
|
|
|
* @license OSL - Open Software Licence 3.0 | http://opensource.org/licenses/osl-3.0.php |
|
9
|
|
|
*/ |
|
10
|
|
|
class Zookal_Mock_Model_Observer |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* General Container for rewriting nodes entries |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $_newConfigNodes = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Only add these modules which are tightly coupled with the core and causes issues once active=>false |
|
21
|
|
|
* Module Name => model name |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $_mappingModel = array( |
|
26
|
|
|
'Mage_Wishlist' => 'wishlist', |
|
27
|
|
|
'Mage_Weee' => 'weee', |
|
28
|
|
|
'Mage_Usa' => 'usa', |
|
29
|
|
|
'Mage_Tag' => 'tag', |
|
30
|
|
|
'Mage_Tax' => 'tax', |
|
31
|
|
|
'Mage_Shipping' => 'shipping', |
|
32
|
|
|
'Mage_Sales' => 'sales', |
|
33
|
|
|
'Mage_Review' => 'review', |
|
34
|
|
|
'Mage_Reports' => 'reports', |
|
35
|
|
|
'Mage_Rating' => 'rating', |
|
36
|
|
|
'Mage_ProductAlert' => 'productalert', |
|
37
|
|
|
'Mage_Newsletter' => 'newsletter', |
|
38
|
|
|
'Mage_Log' => 'log', |
|
39
|
|
|
'Mage_GoogleCheckout' => 'googlecheckout', |
|
40
|
|
|
'Mage_GiftMessage' => 'giftmessage', |
|
41
|
|
|
'Mage_Dataflow' => 'dataflow', |
|
42
|
|
|
'Mage_Catalog' => 'catalog', |
|
43
|
|
|
'Mage_Customer' => 'customer', |
|
44
|
|
|
'Mage_Cms' => 'cms', |
|
45
|
|
|
'Mage_Backup' => 'backup', |
|
46
|
|
|
'Mage_Adminhtml' => 'adminhtml', |
|
47
|
|
|
'Mage_Admin' => 'admin', |
|
48
|
|
|
'Mage_AdminNotification' => 'adminnotification', |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* These methods will only be executed when that module has been disabled. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $_specialMethods = array( |
|
57
|
|
|
'Mage_Admin' => '_mageMockIncludePath', |
|
58
|
|
|
'Mage_Adminhtml' => '_mageMockIncludePath', |
|
59
|
|
|
'Mage_AdminNotification' => '_mageMockIncludePath', |
|
60
|
|
|
'Mage_Catalog' => '_mageCatalog', |
|
61
|
|
|
'Mage_Customer' => '_mageCustomer', |
|
62
|
|
|
'Mage_GiftMessage' => '_mageMockHelperIncludePath', |
|
63
|
|
|
'Mage_GoogleCheckout' => '_mageGoogleCheckout', |
|
64
|
|
|
'Mage_Log' => '_mageMockIncludePath', |
|
65
|
|
|
'Mage_ProductAlert' => '_mageMockHelper', |
|
66
|
|
|
'Mage_Review' => '_mageMockHelper', |
|
67
|
|
|
'Mage_Shipping' => '_mageMockHelperIncludePath', |
|
68
|
|
|
'Mage_Tag' => '_mageMockHelperIncludePath', |
|
69
|
|
|
'Mage_Tax' => '_mageTaxClass', |
|
70
|
|
|
'Mage_Usa' => '_mageMockHelper', |
|
71
|
|
|
'Mage_Wishlist' => '_mageMockHelper', |
|
72
|
|
|
'Mage_Weee' => '_mageMockIncludePath', |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* only needed for the crontab because implemented on the two events always and default. |
|
77
|
|
|
* For controller execution we're only observing controller_front_init_before which fires once |
|
78
|
|
|
* |
|
79
|
|
|
* @var int |
|
80
|
|
|
*/ |
|
81
|
|
|
private $_singleton = 0; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* To use this in a shell script (no cron) call it: Mage::getModel('zookal_mock/observer')->mockDisabledModules(); |
|
85
|
|
|
* @fire always |
|
86
|
|
|
* @fire default |
|
87
|
|
|
* @fire controller_front_init_before |
|
88
|
|
|
* @return null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function mockDisabledModules() |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->_singleton > 0) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
$this->_singleton++; |
|
96
|
|
|
$this->_initSpecialMethods(); |
|
97
|
|
|
$disabledModules = $this->_getDisabledModules(); |
|
98
|
|
|
$pathPrefix = 'global/models/'; |
|
99
|
|
|
|
|
100
|
|
|
foreach ($disabledModules as $moduleName => $module) { |
|
101
|
|
|
if (false === isset($this->_mappingModel[$moduleName])) { |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
$class = 'Zookal_Mock_Model_Mocks_' . $module[0]; |
|
105
|
|
|
$this->_setConfigNode($pathPrefix . $this->_mappingModel[$moduleName] . '/class', $class); |
|
106
|
|
|
$resource = $this->_mappingModel[$moduleName] . '_resource'; |
|
107
|
|
|
$this->_setConfigNode($pathPrefix . $this->_mappingModel[$moduleName] . '/resourceModel', $resource); |
|
108
|
|
|
$this->_setConfigNode($pathPrefix . $resource . '/class', $class); |
|
109
|
|
|
|
|
110
|
|
|
$o = array( |
|
111
|
|
|
'p' => $pathPrefix, |
|
112
|
|
|
'm' => $moduleName, |
|
113
|
|
|
'r' => $resource |
|
114
|
|
|
); |
|
115
|
|
|
if (is_array($this->_getSpecialMethod($moduleName))) { |
|
116
|
|
|
call_user_func($this->_getSpecialMethod($moduleName), $o); |
|
117
|
|
|
} else { |
|
118
|
|
|
$this->{$this->_getSpecialMethod($moduleName)}($o); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
$this->_processSetNodes(); |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Runs a specialMethod if its found otherwise _mageVoid will be executed |
|
127
|
|
|
* |
|
128
|
|
|
* @param $moduleName |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function _getSpecialMethod($moduleName) |
|
133
|
|
|
{ |
|
134
|
|
|
return isset($this->_specialMethods[$moduleName]) ? $this->_specialMethods[$moduleName] : '_mageVoid'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Allows you to remove or add special methods. If the value of a key is an array then your class will be called. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function _initSpecialMethods() |
|
141
|
|
|
{ |
|
142
|
|
|
Mage::dispatchEvent('zookal_mock_init_special_methods', array('mock' => $this)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Special Handling when Mage_Adminhtml/Mage_Log/Mage_Tag is disabled and physically removed |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function _mageMockIncludePath() |
|
149
|
|
|
{ |
|
150
|
|
|
Mage::helper('zookal_mock')->setMockPhpIncludePath(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Special Handling when Mage_GoogleCheckout is disabled. It has a dependency in Mage_Sales/etc/config.xml :-( |
|
155
|
|
|
* |
|
156
|
|
|
* @param array $o |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function _mageGoogleCheckout(array $o) |
|
159
|
|
|
{ |
|
160
|
|
|
$prefixes = $this->_getAllPathPrefixes(); |
|
161
|
|
|
foreach ($prefixes as $prefix) { |
|
162
|
|
|
$this->_setConfigNode($prefix . '/payment/' . $this->_mappingModel[$o['m']] . '/active', '0'); |
|
163
|
|
|
$this->_setConfigNode( |
|
164
|
|
|
$prefix . '/payment/' . $this->_mappingModel[$o['m']] . '/title', |
|
165
|
|
|
'Unavailable: ' . $this->_mappingModel[$o['m']] |
|
166
|
|
|
); |
|
167
|
|
|
$this->_setConfigNode( |
|
168
|
|
|
$prefix . '/payment/' . $this->_mappingModel[$o['m']] . '/model', |
|
169
|
|
|
'zookal_mock/mocks_mage_payment' |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Special Handling when Mage_ProductAlert is disabled, when need to fake a helper |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $o |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function _mageMockHelper(array $o) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->_setConfigNode('global/helpers/' . $this->_mappingModel[$o['m']] . '/class', 'zookal_mock/mocks_mage'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Special Handling when Mage_ProductAlert is disabled, when need to fake a helper |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $o |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function _mageMockHelperIncludePath(array $o) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->_mageMockHelper($o); |
|
192
|
|
|
$this->_mageMockIncludePath(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Special case when Mage_Catalog is disabled and Mage_Widget is enabled |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function _mageCatalog() |
|
199
|
|
|
{ |
|
200
|
|
|
$prefix = 'global/catalog/product/type/simple/'; |
|
201
|
|
|
$this->_setConfigNode($prefix . 'label', 'Simple Product'); |
|
202
|
|
|
$this->_setConfigNode($prefix . 'model', 'zookal_mock/mocks_mage_product'); |
|
203
|
|
|
$this->_setConfigNode($prefix . 'composite', '0'); |
|
204
|
|
|
$this->_setConfigNode($prefix . 'index_priority', '10'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Special case when Mage_CatalogIndex is enabled and Mage_Customer is disabled |
|
209
|
|
|
* Mage_Customer needs the tax_class table name for joining |
|
210
|
|
|
* |
|
211
|
|
|
* @param array $o |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function _mageCustomer(array $o) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->_setConfigNode($o['p'] . $o['r'] . '/entities/customer_group/table', 'customer_group'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Special case when Mage_Tax is disabled and Mage_Customer is enabled |
|
220
|
|
|
* Mage_Customer needs the tax_class table name for joining |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $o |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function _mageTaxClass(array $o) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->_setConfigNode($o['p'] . $o['r'] . '/entities/tax_class/table', 'tax_class'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* empty method for fallback |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function _mageVoid() |
|
233
|
|
|
{ |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function _getDisabledModules() |
|
240
|
|
|
{ |
|
241
|
|
|
$_disabledModules = array(); |
|
242
|
|
|
|
|
243
|
|
|
$modules = Mage::getConfig()->getNode('modules'); |
|
244
|
|
|
foreach ($modules->children() as $moduleName => $node) { |
|
245
|
|
|
/** @var $node Mage_Core_Model_Config_Element */ |
|
246
|
|
|
$isDisabled = strtolower($node->active) !== 'true'; |
|
247
|
|
|
if (true === $isDisabled) { |
|
248
|
|
|
$_disabledModules[$moduleName] = explode('_', $moduleName); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
return $_disabledModules; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @param string $path |
|
256
|
|
|
* @param string $value |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function _setConfigNode($path, $value) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->_newConfigNodes[$path] = $value; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* runs setNode on getConfig |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function _processSetNodes() |
|
267
|
|
|
{ |
|
268
|
|
|
foreach ($this->_newConfigNodes as $path => $value) { |
|
269
|
|
|
Mage::getConfig()->setNode($path, $value); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* refactor when used more than once |
|
275
|
|
|
* |
|
276
|
|
|
* @return array |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function _getAllPathPrefixes() |
|
279
|
|
|
{ |
|
280
|
|
|
$prefixes = array( |
|
281
|
|
|
'default' => 'default', |
|
282
|
|
|
'stores/admin' => 'stores/admin', |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
|
|
$stores = Mage::app()->getStores(); |
|
286
|
|
|
foreach ($stores as $store) { |
|
287
|
|
|
/** @var $store Mage_Core_Model_Store */ |
|
288
|
|
|
$prefixes['stores/' . $store->getCode()] = 'stores/' . $store->getCode(); |
|
289
|
|
|
} |
|
290
|
|
|
return $prefixes; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @return array |
|
295
|
|
|
*/ |
|
296
|
|
|
public function getSpecialMethods() |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->_specialMethods; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @param array $specialMethods |
|
303
|
|
|
* |
|
304
|
|
|
* @return $this |
|
305
|
|
|
*/ |
|
306
|
|
|
public function setSpecialMethods(array $specialMethods) |
|
307
|
|
|
{ |
|
308
|
|
|
$this->_specialMethods = $specialMethods; |
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
} |
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.