1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14:
15: class IllApps_Shipsync_Helper_Data extends Mage_Core_Helper_Abstract
16: {
17:
18: 19: 20:
21: const VERSION = '3.0.1';
22:
23: 24: 25:
26: const = 'X-ChromePhp-Data';
27:
28: 29: 30:
31: const BACKTRACE_LEVEL = 'backtrace_level';
32:
33: 34: 35:
36: const LOG = 'log';
37:
38: 39: 40:
41: const WARN = 'warn';
42:
43: 44: 45:
46: const ERROR = 'error';
47:
48: 49: 50:
51: const GROUP = 'group';
52:
53: 54: 55:
56: const INFO = 'info';
57:
58: 59: 60:
61: const GROUP_END = 'groupEnd';
62:
63: 64: 65:
66: const GROUP_COLLAPSED = 'groupCollapsed';
67:
68: 69: 70:
71: protected $_php_version;
72:
73: 74: 75:
76: protected $_timestamp;
77:
78: 79: 80:
81: protected $_json = array(
82: 'version' => self::VERSION,
83: 'columns' => array('label', 'log', 'backtrace', 'type'),
84: 'rows' => array()
85: );
86:
87: 88: 89:
90: protected $_backtraces = array();
91:
92: 93: 94:
95: protected $_error_triggered = false;
96:
97: 98: 99:
100: protected $_settings = array(
101: self::BACKTRACE_LEVEL => 1
102: );
103:
104: 105: 106:
107: protected static $_instance;
108:
109: 110: 111: 112: 113:
114: protected $_processed = array();
115:
116: 117: 118:
119: public function __construct()
120: {
121: $this->_php_version = phpversion();
122: $this->_timestamp = $this->_php_version >= 5.1 ? $_SERVER['REQUEST_TIME'] : time();
123: $this->_json['request_uri'] = $_SERVER['REQUEST_URI'];
124: }
125:
126: 127: 128: 129: 130:
131: public static function getInstance()
132: {
133: if (self::$_instance === null) {
134: self::$_instance = new self();
135: }
136: return self::$_instance;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146:
147: public static function log()
148: {
149: $args = func_get_args();
150: $severity = count($args) == 3 ? array_pop($args) : '';
151:
152:
153: if ($severity == self::LOG) {
154: $severity = '';
155: }
156:
157: return self::_log($args + array('type' => $severity));
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: public static function warn()
168: {
169: return self::_log(func_get_args() + array('type' => self::WARN));
170: }
171:
172: 173: 174: 175: 176: 177: 178:
179: public static function error()
180: {
181: return self::_log(func_get_args() + array('type' => self::ERROR));
182: }
183:
184: 185: 186: 187: 188:
189: public static function group()
190: {
191: return self::_log(func_get_args() + array('type' => self::GROUP));
192: }
193:
194: 195: 196: 197: 198:
199: public static function info()
200: {
201: return self::_log(func_get_args() + array('type' => self::INFO));
202: }
203:
204: 205: 206: 207: 208:
209: public static function groupCollapsed()
210: {
211: return self::_log(func_get_args() + array('type' => self::GROUP_COLLAPSED));
212: }
213:
214: 215: 216: 217: 218:
219: public static function groupEnd()
220: {
221: return self::_log(func_get_args() + array('type' => self::GROUP_END));
222: }
223:
224: 225: 226: 227: 228: 229:
230: protected static function _log(array $args)
231: {
232: $type = $args['type'];
233: unset($args['type']);
234:
235:
236: if (count($args) == 0 && $type != self::GROUP_END) {
237: return;
238: }
239:
240:
241: $label = null;
242: $value = isset($args[0]) ? $args[0] : '';
243:
244: $logger = self::getInstance();
245:
246:
247: if (count($args) == 2) {
248: $label = $args[0];
249: $value = $args[1];
250: }
251:
252: $logger->_processed = array();
253: $value = $logger->_convert($value);
254:
255: $backtrace = debug_backtrace(false);
256: $level = $logger->getSetting(self::BACKTRACE_LEVEL);
257:
258: $backtrace_message = 'unknown';
259: if (isset($backtrace[$level]['file']) && isset($backtrace[$level]['line'])) {
260: $backtrace_message = $backtrace[$level]['file'] . ' : ' . $backtrace[$level]['line'];
261: }
262:
263: $logger->_addRow($label, $value, $backtrace_message, $type);
264: }
265:
266: 267: 268: 269: 270: 271:
272: protected function _convert($object)
273: {
274:
275: if (!is_object($object)) {
276: return $object;
277: }
278:
279:
280:
281: $this->_processed[] = $object;
282:
283: $object_as_array = array();
284:
285:
286: $object_as_array['___class_name'] = get_class($object);
287:
288:
289: $object_vars = get_object_vars($object);
290: foreach ($object_vars as $key => $value) {
291:
292:
293: if ($value === $object || in_array($value, $this->_processed, true)) {
294: $value = 'recursion - parent object [' . get_class($value) . ']';
295: }
296: $object_as_array[$key] = $this->_convert($value);
297: }
298:
299: $reflection = new ReflectionClass($object);
300:
301:
302: foreach ($reflection->getProperties() as $property) {
303:
304:
305: if (array_key_exists($property->getName(), $object_vars)) {
306: continue;
307: }
308: $type = $this->_getPropertyKey($property);
309:
310: if ($this->_php_version >= 5.3) {
311: $property->setAccessible(true);
312: }
313:
314: try {
315: $value = $property->getValue($object);
316: } catch (ReflectionException $e) {
317: $value = 'only PHP 5.3 can access private/protected properties';
318: }
319:
320:
321: if ($value === $object || in_array($value, $this->_processed, true)) {
322: $value = 'recursion - parent object [' . get_class($value) . ']';
323: }
324:
325: $object_as_array[$type] = $this->_convert($value);
326: }
327: return $object_as_array;
328: }
329:
330: 331: 332: 333: 334: 335:
336: protected function _getPropertyKey(ReflectionProperty $property)
337: {
338: $static = $property->isStatic() ? ' static' : '';
339: if ($property->isPublic()) {
340: return 'public' . $static . ' ' . $property->getName();
341: }
342:
343: if ($property->isProtected()) {
344: return 'protected' . $static . ' ' . $property->getName();
345: }
346:
347: if ($property->isPrivate()) {
348: return 'private' . $static . ' ' . $property->getName();
349: }
350: }
351:
352: 353: 354: 355: 356: 357:
358: protected function _addRow($label, $log, $backtrace, $type)
359: {
360:
361: if (in_array($backtrace, $this->_backtraces)) {
362: $backtrace = null;
363: }
364:
365: if ($backtrace !== null) {
366: $this->_backtraces[] = $backtrace;
367: }
368:
369: $row = array($label, $log, $backtrace, $type);
370:
371: $this->_json['rows'][] = $row;
372: $this->_writeHeader($this->_json);
373: }
374:
375: protected function ($data)
376: {
377: header(self::HEADER_NAME . ': ' . $this->_encode($data));
378: }
379:
380: 381: 382: 383: 384: 385:
386: protected function _encode($data)
387: {
388: return base64_encode(utf8_encode(json_encode($data)));
389: }
390:
391: 392: 393: 394: 395: 396: 397:
398: public function addSetting($key, $value)
399: {
400: $this->_settings[$key] = $value;
401: }
402:
403: 404: 405: 406: 407: 408:
409: public function addSettings(array $settings)
410: {
411: foreach ($settings as $key => $value) {
412: $this->addSetting($key, $value);
413: }
414: }
415:
416: 417: 418: 419: 420: 421:
422: public function getSetting($key)
423: {
424: if (!isset($this->_settings[$key])) {
425: return null;
426: }
427: return $this->_settings[$key];
428: }
429:
430: public static function mageLog($var, $name)
431: {
432: if (Mage::getStoreConfig('carriers/fedex/mage_log')) {
433: Mage::log($var, null, 'shipsync_' . $name . '.log');
434: }
435: }
436: }
437: