|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Classes en rapport avec les observers |
|
4
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
5
|
|
|
* @version 1.0 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace BFW; |
|
9
|
|
|
|
|
10
|
|
|
use \Exception; |
|
11
|
|
|
use \SplSubject; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Classe Observer |
|
15
|
|
|
* @package bfw |
|
16
|
|
|
*/ |
|
17
|
|
|
class Observer implements \SplObserver |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Méthode par défaut appelé lorsque l'observer se déclanche |
|
21
|
|
|
* |
|
22
|
|
|
* @param SplSubject $subject Le sujet déclanchant l'observer |
|
23
|
|
|
*/ |
|
24
|
|
|
public function update(SplSubject $subject) |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Méthode appelé lorsque l'observer se déclanche via la classe Kernel |
|
31
|
|
|
* |
|
32
|
|
|
* @param SplSubject $subject Le sujet déclanchant l'observer |
|
33
|
|
|
* @param string $action L'action à faire lors du déclanchement |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \Exception : Si le paramètre $subject n'est pas un objet ou n'est pas une instance de \BFW\Kernel |
|
36
|
|
|
*/ |
|
37
|
|
|
public function updateWithAction($subject, $action) |
|
38
|
|
|
{ |
|
39
|
|
|
if(!is_object($subject)) |
|
40
|
|
|
{ |
|
41
|
|
|
throw new Exception('Le paramètre $subject doit être un objet.'); |
|
42
|
|
|
} |
|
43
|
|
|
elseif(is_object($subject) && get_class($subject) != '\SplSubject') |
|
44
|
|
|
{ |
|
45
|
|
|
throw new Exception('Le paramètre $subject doit être un objet de type \SplSubject.'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//Gestion de l'action. |
|
49
|
|
|
/* |
|
50
|
|
|
* $action : |
|
51
|
|
|
* Maclass::MaMethode() |
|
52
|
|
|
* Maclass->MaMethode() |
|
53
|
|
|
* MaFonction() |
|
54
|
|
|
*/ |
|
55
|
|
|
//Il faut gérer la porter des variables (classe/arguments) |
|
56
|
|
|
|
|
57
|
|
|
$class = null; |
|
58
|
|
|
$args = null; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$match = array(); |
|
61
|
|
|
$regex = '([0-9a-zA-Z.,_-]+)'; |
|
62
|
|
|
$regex2 = '([0-9a-zA-Z.,_-]*)'; |
|
63
|
|
|
|
|
64
|
|
|
if(strpos($action, '::') !== false) |
|
65
|
|
|
{ |
|
66
|
|
|
$preg_match = preg_match('^'.$regex.'::'.$regex.'\('.$regex2.'\);$', $action, $match); |
|
67
|
|
|
$class = $match[1]; |
|
68
|
|
|
$args = $match[3]; |
|
69
|
|
|
} |
|
70
|
|
|
elseif(strpos($action, '->') !== false) |
|
71
|
|
|
{ |
|
72
|
|
|
$preg_match = preg_match('^'.$regex.'::'.$regex.'\('.$regex2.'\);$', $action, $match); |
|
73
|
|
|
$class = $match[1]; |
|
74
|
|
|
$args = $match[3]; |
|
75
|
|
|
} |
|
76
|
|
|
else |
|
77
|
|
|
{ |
|
78
|
|
|
$preg_match = preg_match('^'.$regex.'\('.$regex2.'\);$', $action, $match); |
|
79
|
|
|
$args = $match[2]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(!is_null($class)) |
|
83
|
|
|
{ |
|
84
|
|
|
$class = str_replace('$', '', $class); |
|
85
|
|
|
global ${$class}; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if(!is_null($args)) |
|
89
|
|
|
{ |
|
90
|
|
|
$args = explode(',', $args); |
|
91
|
|
|
foreach($args as $arg) |
|
92
|
|
|
{ |
|
93
|
|
|
$arg = str_replace('$', '', $arg); |
|
94
|
|
|
global ${$arg}; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
try |
|
99
|
|
|
{ |
|
100
|
|
|
eval($action); |
|
101
|
|
|
} |
|
102
|
|
|
catch(\Exception $e) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
echo $e->getMessage(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.