|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Proxy |
|
5
|
|
|
* @copyright Copyright (c) 2011 - 2012 Aleksandr Torosh (http://wezoom.com.ua) |
|
6
|
|
|
* @author Aleksandr Torosh <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Application\Widget; |
|
10
|
|
|
|
|
11
|
|
|
class Proxy extends \Phalcon\Mvc\User\Component |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const NULLCACHE = 'NULLCACHE'; |
|
15
|
|
|
|
|
16
|
|
|
public static $cache = null; // injected |
|
17
|
|
|
private $cacheEnabled = true; |
|
18
|
|
|
private $cacheTime = 60; |
|
19
|
|
|
private $object; |
|
20
|
|
|
private $namespace; |
|
21
|
|
|
private $hide_for_mobile = false; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct($namespace = 'Index', array $params = array()) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->namespace = $namespace; |
|
26
|
|
|
|
|
27
|
|
|
ucfirst($namespace); |
|
28
|
|
|
$class = $namespace . '\\Widget\\' . $namespace . 'Widget'; |
|
29
|
|
|
$this->object = new $class(); |
|
30
|
|
|
$this->object->setModule($namespace); |
|
31
|
|
|
|
|
32
|
|
|
$registry = $this->getDI()->get('registry'); |
|
|
|
|
|
|
33
|
|
|
$this->cacheEnabled = $registry->cms['WIDGETS_CACHE']; |
|
34
|
|
|
|
|
35
|
|
|
if (isset($params['cache']) && !$params['cache']) { |
|
36
|
|
|
$this->cacheEnabled = false; |
|
37
|
|
|
} |
|
38
|
|
|
if (isset($params['time']) && $params['time']) { |
|
39
|
|
|
$this->cacheTime = (int) $params['time']; |
|
40
|
|
|
} |
|
41
|
|
|
if (isset($params['hide_for_mobile']) && $params['hide_for_mobile']) { |
|
42
|
|
|
if (MOBILE_DEVICE) { |
|
43
|
|
|
$this->hide_for_mobile = true; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __call($method, array $params) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->hide_for_mobile) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
try { |
|
55
|
|
|
if ($this->cacheEnabled) { |
|
56
|
|
|
$paramsString = md5(serialize($params)); |
|
57
|
|
|
$cacheKey = md5($this->namespace . '::' . $method . $paramsString . LANG . (string) MOBILE_DEVICE); |
|
58
|
|
|
$results = self::$cache->get($cacheKey); |
|
59
|
|
|
if (!$results) { |
|
60
|
|
|
if (method_exists($this->object, $method)) { |
|
61
|
|
|
$results = $this->getResults($method, $params); |
|
62
|
|
|
if (!$results) { |
|
63
|
|
|
$results = self::NULLCACHE; |
|
64
|
|
|
} |
|
65
|
|
|
$cacheTime = $this->cacheTime + rand(0, 60); |
|
66
|
|
|
self::$cache->save($cacheKey, $results, $cacheTime); |
|
67
|
|
|
if ($results !== self::NULLCACHE) { |
|
68
|
|
|
return $results; |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
echo $this->namespace . 'Widget::' . $method . ' not exists'; |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
|
|
if ($results == self::NULLCACHE) { |
|
75
|
|
|
return; |
|
76
|
|
|
} else { |
|
77
|
|
|
return $results; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
return $this->getResults($method, $params); |
|
82
|
|
|
} |
|
83
|
|
|
} catch (\Exception $e) { |
|
84
|
|
|
$this->cacheEnabled = false; |
|
85
|
|
|
echo '<!--' . htmlspecialchars('Error. ' . $this->namespace . 'Widget::' . $method . '. ' . $e->getMessage()) . '-->'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function getResults($method, $params) |
|
91
|
|
|
{ |
|
92
|
|
|
ob_start(); |
|
93
|
|
|
call_user_func_array(array($this->object, $method), $params); |
|
94
|
|
|
$results = ob_get_contents(); |
|
95
|
|
|
ob_end_clean(); |
|
96
|
|
|
return $results; |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.