|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\command; |
|
3
|
|
|
use izzum\command\Command; |
|
4
|
|
|
use izzum\command\ICommand; |
|
5
|
|
|
use izzum\command\IComposite; |
|
6
|
|
|
use izzum\command\Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Command Pattern [GoF] implementation |
|
10
|
|
|
* CompositeCommand (aka MacroCommand) can be used to insert multiple ICommand |
|
11
|
|
|
* instances which will be handled in the execute() method |
|
12
|
|
|
* |
|
13
|
|
|
* @author Rolf Vreijdenberger |
|
14
|
|
|
* @link https://en.wikipedia.org/wiki/Command_pattern |
|
15
|
|
|
*/ |
|
16
|
|
|
class Composite extends Command implements IComposite { |
|
17
|
|
|
/** |
|
18
|
|
|
* an array of commands |
|
19
|
|
|
* |
|
20
|
|
|
* @var ICommand[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $commands; |
|
23
|
|
|
|
|
24
|
27 |
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
27 |
|
$this->commands = array(); |
|
27
|
27 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* this method will call all commands added to this class in order of |
|
31
|
|
|
* addition |
|
32
|
|
|
* |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
19 |
|
protected function _execute() |
|
36
|
|
|
{ |
|
37
|
19 |
|
foreach ($this->commands as $command) { |
|
38
|
19 |
|
$command->execute(); |
|
39
|
17 |
|
} |
|
40
|
17 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* this method can be used to add multiple commands that will be used in the |
|
44
|
|
|
* execute() method |
|
45
|
|
|
* |
|
46
|
|
|
* @param |
|
47
|
|
|
* command a concrete command that implements the ICommand |
|
48
|
|
|
* interface |
|
49
|
|
|
*/ |
|
50
|
22 |
|
public function add(ICommand $command) |
|
51
|
|
|
{ |
|
52
|
22 |
|
$this->commands [] = $command; |
|
53
|
22 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Removes a command if it is part of the composite (based on identity ===) |
|
57
|
|
|
* |
|
58
|
|
|
* @param ICommand $command |
|
59
|
|
|
* @return boolean |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function remove(ICommand $command) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$total = count($this->commands); |
|
64
|
1 |
|
$removed = false; |
|
65
|
1 |
|
for($i = ($total - 1); $i >= 0; $i--) { |
|
66
|
1 |
|
$current = $this->commands [$i]; |
|
67
|
1 |
|
if ($current === $command) { |
|
68
|
1 |
|
array_splice($this->commands, $i, 1); |
|
69
|
1 |
|
$removed = true; |
|
70
|
1 |
|
} |
|
71
|
1 |
|
} |
|
72
|
1 |
|
return $removed; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* does this contain a command (based on identity ===) |
|
77
|
|
|
* |
|
78
|
|
|
* @param ICommand $command |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function contains(ICommand $command) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$contains = false; |
|
84
|
1 |
|
foreach ($this->commands as $current) { |
|
85
|
1 |
|
if ($current === $command) { |
|
86
|
1 |
|
$contains = true; |
|
87
|
1 |
|
break; |
|
88
|
|
|
} |
|
89
|
1 |
|
} |
|
90
|
1 |
|
return $contains; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* (non-PHPdoc) |
|
95
|
|
|
* |
|
96
|
|
|
* @see \izzum\IComposite::count() |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function count() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return count($this->commands); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
public function toString() |
|
104
|
|
|
{ |
|
105
|
5 |
|
$composition = array(); |
|
106
|
5 |
|
$children = ''; |
|
107
|
5 |
|
foreach ($this->commands as $command) { |
|
108
|
5 |
|
$composition [] = $command->toString(); |
|
109
|
5 |
|
} |
|
110
|
5 |
|
if (count($composition) != 0) { |
|
111
|
5 |
|
$children = " consisting of: [" . implode(", ", $composition) . "]"; |
|
112
|
5 |
|
} |
|
113
|
5 |
|
return get_class($this) . $children; |
|
114
|
|
|
} |
|
115
|
|
|
} |