|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Benrowe\Laravel\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Arrilot\Widgets\AbstractWidget as BaseAbstractWidget; |
|
6
|
|
|
use Arrilot\Widgets\WidgetId; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractWidget extends BaseAbstractWidget |
|
12
|
|
|
{ |
|
13
|
|
|
const ID_PREFIX = 'widget-'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $id; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* Store the configuration & initialise the widget |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $config |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($config = []) |
|
27
|
|
|
{ |
|
28
|
|
|
foreach ($config as $key => $value) { |
|
29
|
|
|
$this->addCfg($key, $value); |
|
30
|
|
|
} |
|
31
|
|
|
$this->init(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Custom initilisation for the widget |
|
36
|
|
|
* |
|
37
|
|
|
* @return boolean |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function init() |
|
40
|
|
|
{ |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve the configuration value from the widget, based |
|
46
|
|
|
* on the supplied key |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $key |
|
49
|
|
|
* @param mixed $default |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function cfg($key, $default = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (array_key_exists($key, $this->config)) { |
|
55
|
|
|
return $this->config[$key]; |
|
56
|
|
|
} |
|
57
|
|
|
if ($this->isConfigProperty($key)) { |
|
58
|
|
|
return $this->$key; |
|
59
|
|
|
} |
|
60
|
|
|
return $default; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Add a configuration into the widget |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $key |
|
67
|
|
|
* @param mixed $value |
|
68
|
|
|
* @return nil|null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function addCfg($key, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->isConfigProperty($key)) { |
|
73
|
|
|
$this->$key = $value; |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
$this->config[$key] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retrieve the widget id |
|
81
|
|
|
* |
|
82
|
|
|
* @param boolean $autoGenerate automatically generate the id if none is |
|
83
|
|
|
* already set |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getId($autoGenerate = true) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!$this->id && $autoGenerate) { |
|
89
|
|
|
$this->id = self::ID_PREFIX . WidgetId::get(); |
|
90
|
|
|
} |
|
91
|
|
|
return $this->id; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the widget identifier |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $id |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setId($id) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->id = $id; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Determine if the public property exists, and is public |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $propertyName |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
|
|
private function isConfigProperty($propertyName) |
|
111
|
|
|
{ |
|
112
|
|
|
try { |
|
113
|
|
|
$reflect = new \ReflectionClass($this); |
|
114
|
|
|
$property = $reflect->getProperty($propertyName); |
|
115
|
|
|
return $property->isPublic() && !$property->isStatic(); |
|
116
|
|
|
} catch (\ReflectionException $e) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: