1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\SocketIoBundle\Service; |
4
|
|
|
|
5
|
|
|
use SfCod\SocketIoBundle\Events\EventInterface; |
6
|
|
|
use SfCod\SocketIoBundle\Exception\EventDuplicateException; |
7
|
|
|
use SfCod\SocketIoBundle\Exception\EventNotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EventManager. |
11
|
|
|
* |
12
|
|
|
* @author Virchenko Maksim <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @package SfCod\SocketIoBundle\Service |
15
|
|
|
*/ |
16
|
|
|
class EventManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* List with all events. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $list = []; |
24
|
|
|
/** |
25
|
|
|
* Array of events. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $namespaces; |
30
|
|
|
/** |
31
|
|
|
* Project root directory. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $rootDir; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* EventManager constructor. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(string $rootDir, array $namespaces = []) |
41
|
|
|
{ |
42
|
|
|
$this->rootDir = $rootDir; |
43
|
|
|
$this->namespaces = $namespaces; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get events list. |
48
|
|
|
*/ |
49
|
|
|
public function getList(): array |
50
|
|
|
{ |
51
|
|
|
//@todo remove this, move to extension using tags |
52
|
|
|
if (empty(self::$list)) { |
53
|
|
|
foreach ($this->namespaces as $key => $namespace) { |
54
|
|
|
$alias = $this->rootDir . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\')); |
55
|
|
|
|
56
|
|
|
foreach (glob(sprintf('%s/**.php', $alias)) as $file) { |
57
|
|
|
$className = sprintf('%s\%s', $namespace, basename($file, '.php')); |
58
|
|
|
if (method_exists($className, 'name')) { |
59
|
|
|
$this->list[$className::name()] = $className; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->list; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Resolve the given class. |
70
|
|
|
*/ |
71
|
|
|
public function resolve(string $name): EventInterface |
72
|
|
|
{ |
73
|
|
|
if (isset($this->list[$name])) { |
74
|
|
|
return $this->list[$name]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new EventNotFoundException("Event handler '$name' not found."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $id |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function addEvent(EventInterface $event) |
84
|
|
|
{ |
85
|
|
|
if (isset($this->list[$event::name()])) { |
86
|
|
|
throw new EventDuplicateException("Event '{$event::name()}' already exists."); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->list[$event::name()] = $event; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.