1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Container; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract entry provider that provides the most basic provider initialization functionality. |
7
|
|
|
* |
8
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
10
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractEntryProvider implements EntryProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns a new instance of the entry provider. |
16
|
|
|
* @return EntryProvider New entry provider instance initialized with default constructor |
17
|
|
|
*/ |
18
|
2 |
|
public static function initialize(): EntryProvider |
19
|
|
|
{ |
20
|
2 |
|
return new static(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns provided list of all classes returned by the container methods. |
25
|
|
|
* @return string[] Provided list of all classes returned by the container methods |
26
|
|
|
*/ |
27
|
1 |
|
public function getMethods(): array |
28
|
|
|
{ |
29
|
1 |
|
$methods = []; |
30
|
1 |
|
$reflection = new \ReflectionClass($this); |
31
|
|
|
|
32
|
1 |
|
foreach ($reflection->getMethods() as $method) { |
33
|
1 |
|
$identifier = $this->getMethodIdentifier($method); |
34
|
|
|
|
35
|
1 |
|
if ($identifier !== null) { |
36
|
1 |
|
$methods[$identifier] = $method->getName(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $methods; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Tells the identifier to use for the given provider method. |
45
|
|
|
* @param \ReflectionMethod $method The provider method |
46
|
|
|
* @return string|null The identifier for the method or null if not applicable |
47
|
|
|
*/ |
48
|
1 |
|
private function getMethodIdentifier(\ReflectionMethod $method): ?string |
49
|
|
|
{ |
50
|
1 |
|
if (!$method->isPublic() || $method->isStatic()) { |
51
|
1 |
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
if (strncmp($method->getName(), '__', 2) === 0) { |
55
|
1 |
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$type = $method->getReturnType(); |
59
|
|
|
|
60
|
1 |
|
if ($type === null || $type->isBuiltin()) { |
61
|
1 |
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $type->getName(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|