|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of cloak. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace cloak\reporter; |
|
13
|
|
|
|
|
14
|
|
|
use PhpCollection\Map; |
|
15
|
|
|
use \ReflectionClass; |
|
16
|
|
|
use \ReflectionException; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ReporterFactory |
|
21
|
|
|
* @package cloak\reporter |
|
22
|
|
|
*/ |
|
23
|
|
|
class ReporterFactory |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
const REPORTER_NAMESPACE = 'cloak\\reporter\\'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ReflectionClass |
|
30
|
|
|
*/ |
|
31
|
|
|
private $reflection; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ReflectionClass $reflection |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ReflectionClass $reflection) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->reflection = $reflection; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $className |
|
44
|
|
|
* @return ReporterFactory |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function fromClassName($className) |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$reflection = new ReflectionClass($className); |
|
50
|
|
|
} catch (ReflectionException $exception) { |
|
51
|
|
|
throw new ReporterNotFoundException($exception); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return new self($reflection); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @return ReporterFactory |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function fromName($name) |
|
62
|
|
|
{ |
|
63
|
|
|
$className = ucfirst($name) . 'Reporter'; |
|
64
|
|
|
$fullClassName = static::REPORTER_NAMESPACE . $className; |
|
65
|
|
|
|
|
66
|
|
|
return static::fromClassName($fullClassName); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $arguments |
|
71
|
|
|
* @return \cloak\reporter\Reporter |
|
72
|
|
|
*/ |
|
73
|
|
|
public function createWithArguments(array $arguments) |
|
74
|
|
|
{ |
|
75
|
|
|
$assignValues = new Map($arguments); |
|
76
|
|
|
$assmebleArguments = $this->assmebleArguments($assignValues); |
|
77
|
|
|
return $this->reflection->newInstanceArgs($assmebleArguments); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Map $assignValues |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
private function assmebleArguments(Map $assignValues) |
|
85
|
|
|
{ |
|
86
|
|
|
$assembleArguments = []; |
|
87
|
|
|
$constructorArguments = $this->getConstructorArguments(); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($constructorArguments as $orderNumber => $constructorArgument) { |
|
90
|
|
|
$argumentName = $constructorArgument->name; |
|
91
|
|
|
$assignValue = $assignValues->get($argumentName); |
|
92
|
|
|
|
|
93
|
|
|
if ($assignValue->isEmpty()) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$assembleArguments[$orderNumber] = $assignValue->get(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $assembleArguments; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return \ReflectionParameter[] |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getConstructorArguments() |
|
107
|
|
|
{ |
|
108
|
|
|
$constructor = $this->reflection->getConstructor(); |
|
109
|
|
|
|
|
110
|
|
|
if ($constructor === null) { |
|
111
|
|
|
return []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$parameters = $constructor->getParameters(); |
|
115
|
|
|
|
|
116
|
|
|
return $parameters; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|