1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ListenerProviders\Traits; |
4
|
|
|
|
5
|
|
|
use Fig\EventDispatcher\ParameterDeriverTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait ProviderUtilitiesTrait |
9
|
|
|
* @package ByTIC\EventDispatcher\ListenerProvider |
10
|
|
|
*/ |
11
|
|
|
trait ProviderUtilitiesTrait |
12
|
|
|
{ |
13
|
|
|
use ParameterDeriverTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Derives a predictable ID from the listener if possible. |
17
|
|
|
* |
18
|
|
|
* @param callable $listener |
19
|
|
|
* The listener for which to derive an ID. |
20
|
|
|
* @return string|null |
21
|
|
|
* The derived ID if possible or null if no reasonable ID could be derived. |
22
|
|
|
* @todo If we add support for annotations or similar for identifying listeners that logic would go here. |
23
|
|
|
* |
24
|
|
|
* It's OK for this method to return null, as OrderedCollection will generate a random |
25
|
|
|
* ID if necessary. It will also handle duplicates for us. This method is just a |
26
|
|
|
* suggestion. |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
protected function getListenerId(callable $listener): ?string |
30
|
|
|
{ |
31
|
|
|
if ($this->isFunctionCallable($listener)) { |
|
|
|
|
32
|
|
|
// Function callables are strings, so use that directly. |
33
|
|
|
return (string)$listener; |
34
|
|
|
} |
35
|
|
|
if ($this->isClassCallable($listener)) { |
36
|
|
|
return $listener[0] . '::' . $listener[1]; |
37
|
|
|
} |
38
|
|
|
if ($this->isObjectCallable($listener)) { |
|
|
|
|
39
|
|
|
return get_class($listener[0]) . '::' . $listener[1]; |
40
|
|
|
} |
41
|
|
|
// Anything else we can't derive an ID for logically. |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $event |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function getEventType($event) |
50
|
|
|
{ |
51
|
|
|
if (is_object($event)) { |
52
|
|
|
if (method_exists($event, 'getName')) { |
53
|
|
|
return $event->getName(); |
54
|
|
|
} |
55
|
|
|
return get_class($event); |
56
|
|
|
} |
57
|
|
|
return $event; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.