1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Console\Handler; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\EventBus\Event\EventInterface; |
15
|
|
|
use Cubiche\Core\EventBus\Event\EventSubscriberInterface; |
16
|
|
|
use Cubiche\Core\EventDispatcher\PostDispatchEvent; |
17
|
|
|
use Cubiche\Core\EventDispatcher\PreDispatchEvent; |
18
|
|
|
use Webmozart\Console\Api\IO\IO; |
19
|
|
|
use Webmozart\Console\UI\Component\Table; |
20
|
|
|
use Webmozart\Console\UI\Style\TableStyle; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DefaultEventHandler class. |
24
|
|
|
* |
25
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class DefaultEventHandler implements EventSubscriberInterface, EventHandlerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var callback |
31
|
|
|
*/ |
32
|
|
|
protected $preDispatchHandler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callback |
36
|
|
|
*/ |
37
|
|
|
protected $postDispatchHandler; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var IO |
41
|
|
|
*/ |
42
|
|
|
protected $io; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param IO $io |
46
|
|
|
*/ |
47
|
|
|
public function setIo(IO $io) |
48
|
|
|
{ |
49
|
|
|
$this->io = $io; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param callable $handler |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function addPreDispatchEventHandler($handler) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (!is_callable($handler)) { |
60
|
|
|
throw new \InvalidArgumentException(sprintf( |
61
|
|
|
'Expected a callable. Got: %s', |
62
|
|
|
is_object($handler) ? get_class($handler) : gettype($handler) |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->preDispatchHandler = $handler; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param callable $handler |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function addPostDispatchEventHandler($handler) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (!is_callable($handler)) { |
79
|
|
|
throw new \InvalidArgumentException(sprintf( |
80
|
|
|
'Expected a callable. Got: %s', |
81
|
|
|
is_object($handler) ? get_class($handler) : gettype($handler) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->postDispatchHandler = $handler; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function clearEventHandlers() |
94
|
|
|
{ |
95
|
|
|
$this->preDispatchHandler = null; |
96
|
|
|
$this->postDispatchHandler = null; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param PreDispatchEvent $preDispatchEvent |
103
|
|
|
*/ |
104
|
|
|
public function onPreDispatchEvent(PreDispatchEvent $preDispatchEvent) |
105
|
|
|
{ |
106
|
|
|
if ($this->preDispatchHandler) { |
107
|
|
|
call_user_func($this->preDispatchHandler, $preDispatchEvent->event(), $this->io); |
108
|
|
|
} else { |
109
|
|
|
$this->io->writeLine('<c1>------------------------------------------------------------------------</c1>'); |
110
|
|
|
$this->io->writeLine('Dispatching <c1>'.$this->eventToString($preDispatchEvent->event()).'</c1> with:'); |
|
|
|
|
111
|
|
|
$this->io->writeLine(''); |
112
|
|
|
|
113
|
|
|
$properties = $this->objectToPropertyArray($preDispatchEvent->event()); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$table = new Table(TableStyle::asciiBorder()); |
116
|
|
|
$table->setHeaderRow(array_keys($properties)); |
117
|
|
|
$table->addRow(array_values($properties)); |
118
|
|
|
|
119
|
|
|
$table->render($this->io); |
120
|
|
|
$this->io->writeLine(''); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param PostDispatchEvent $postDispatchEvent |
126
|
|
|
*/ |
127
|
|
|
public function onPostDispatchEvent(PostDispatchEvent $postDispatchEvent) |
128
|
|
|
{ |
129
|
|
|
if ($this->postDispatchHandler) { |
130
|
|
|
call_user_func($this->postDispatchHandler, $postDispatchEvent->event(), $this->io); |
131
|
|
|
} else { |
132
|
|
|
$this->io->writeLine('<c1>'.$this->eventToString($postDispatchEvent->event()).'</c1> success!!'); |
|
|
|
|
133
|
|
|
$this->io->writeLine(''); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param EventInterface $event |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function objectToPropertyArray(EventInterface $event) |
143
|
|
|
{ |
144
|
|
|
$properties = array(); |
145
|
|
|
$reflection = new \ReflectionClass(get_class($event)); |
146
|
|
|
|
147
|
|
|
/** @var \ReflectionProperty $property */ |
148
|
|
|
foreach ($reflection->getProperties() as $property) { |
149
|
|
|
$property->setAccessible(true); |
150
|
|
|
|
151
|
|
|
if (!empty($property->getValue($event))) { |
152
|
|
|
$value = $property->getValue($event); |
153
|
|
|
if ($value instanceof \DateTime) { |
154
|
|
|
$value = $value->format(\DateTime::ISO8601); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$properties[$property->getName()] = $value; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $properties; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param EventInterface $event |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function eventToString(EventInterface $event) |
170
|
|
|
{ |
171
|
|
|
$pos = strrpos($event->eventName(), '\\'); |
172
|
|
|
if ($pos !== false) { |
173
|
|
|
$className = substr($event->eventName(), strrpos($event->eventName(), '\\') + 1); |
174
|
|
|
} else { |
175
|
|
|
$className = $event->eventName(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return strtolower(trim(preg_replace('([A-Z])', ' $0', $className))); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public static function getSubscribedEvents() |
185
|
|
|
{ |
186
|
|
|
return array( |
187
|
|
|
PreDispatchEvent::class => 'onPreDispatchEvent', |
188
|
|
|
PostDispatchEvent::class => 'onPostDispatchEvent', |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.