1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Tools\Benchmark; |
8
|
|
|
use AlecRabbit\Tools\Counter; |
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
10
|
|
|
use AlecRabbit\Tools\Profiler; |
11
|
|
|
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface; |
12
|
|
|
use AlecRabbit\Tools\Reports\Contracts\ReportInterface; |
13
|
|
|
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionFormatter; |
14
|
|
|
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter; |
15
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\Formatter; |
16
|
|
|
use AlecRabbit\Tools\Reports\Formatters\CounterReportFormatter; |
17
|
|
|
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter; |
18
|
|
|
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter; |
19
|
|
|
use AlecRabbit\Tools\Timer; |
20
|
|
|
use function AlecRabbit\typeOf; |
21
|
|
|
|
22
|
|
|
class Factory |
23
|
|
|
{ |
24
|
|
|
/** @var TimerReportFormatter */ |
25
|
|
|
protected static $timerReportFormatter; |
26
|
|
|
|
27
|
|
|
/** @var CounterReportFormatter */ |
28
|
|
|
protected static $counterReportFormatter; |
29
|
|
|
|
30
|
|
|
/** @var ProfilerReportFormatter */ |
31
|
|
|
protected static $profilerReportFormatter; |
32
|
|
|
|
33
|
|
|
/** @var BenchmarkReportFormatter */ |
34
|
|
|
protected static $benchmarkReportFormatter; |
35
|
|
|
|
36
|
|
|
/** @var BenchmarkFunctionFormatter */ |
37
|
|
|
protected static $benchmarkFunctionFormatter; |
38
|
|
|
|
39
|
|
|
/** @codeCoverageIgnore */ |
40
|
|
|
private function __construct() |
41
|
|
|
{ |
42
|
|
|
// Static class |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ReportableInterface $reportable |
47
|
|
|
* @return ReportInterface |
48
|
|
|
*/ |
49
|
15 |
|
public static function makeReport(ReportableInterface $reportable): ReportInterface |
50
|
|
|
{ |
51
|
15 |
|
if ($reportable instanceof Timer) { |
52
|
|
|
return |
53
|
12 |
|
new TimerReport($reportable); |
54
|
|
|
} |
55
|
10 |
|
if ($reportable instanceof Counter) { |
56
|
|
|
return |
57
|
9 |
|
new CounterReport($reportable); |
58
|
|
|
} |
59
|
8 |
|
if ($reportable instanceof Profiler) { |
60
|
|
|
return |
61
|
7 |
|
new ProfilerReport($reportable); |
62
|
|
|
} |
63
|
6 |
|
if ($reportable instanceof Benchmark) { |
64
|
|
|
return |
65
|
5 |
|
new BenchmarkReport($reportable); |
66
|
|
|
} |
67
|
1 |
|
throw new \RuntimeException('Attempt to create unimplemented report for: ' . typeOf($reportable)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ReportInterface $report |
72
|
|
|
* @return Formatter |
73
|
|
|
*/ |
74
|
15 |
|
public static function makeFormatter(ReportInterface $report): Formatter |
75
|
|
|
{ |
76
|
15 |
|
if ($report instanceof TimerReport) { |
77
|
|
|
return |
78
|
12 |
|
self::getTimerReportFormatter($report); |
79
|
|
|
} |
80
|
10 |
|
if ($report instanceof CounterReport) { |
81
|
|
|
return |
82
|
9 |
|
self::getCounterReportFormatter($report); |
83
|
|
|
} |
84
|
8 |
|
if ($report instanceof ProfilerReport) { |
85
|
|
|
return |
86
|
7 |
|
self::getProfilerReportFormatter($report); |
87
|
|
|
} |
88
|
6 |
|
if ($report instanceof BenchmarkReport) { |
89
|
|
|
return |
90
|
5 |
|
self::getBenchmarkReportFormatter($report); |
91
|
|
|
} |
92
|
1 |
|
throw new \RuntimeException('Attempt to create unimplemented formatter for: ' . typeOf($report)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param TimerReport $report |
97
|
|
|
* @return TimerReportFormatter |
98
|
|
|
*/ |
99
|
12 |
|
protected static function getTimerReportFormatter(TimerReport $report): TimerReportFormatter |
100
|
|
|
{ |
101
|
|
|
// if (null === static::$timerReportFormatter) { |
102
|
|
|
// static::$timerReportFormatter = new TimerReportFormatter($report); |
103
|
|
|
// } |
104
|
|
|
return |
105
|
12 |
|
new TimerReportFormatter($report); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param CounterReport $report |
110
|
|
|
* @return CounterReportFormatter |
111
|
|
|
*/ |
112
|
9 |
|
protected static function getCounterReportFormatter(CounterReport $report): CounterReportFormatter |
113
|
|
|
{ |
114
|
|
|
// if (null === static::$counterReportFormatter) { |
115
|
|
|
// static::$counterReportFormatter = new CounterReportFormatter($report); |
116
|
|
|
// } |
117
|
|
|
return |
118
|
9 |
|
new CounterReportFormatter($report); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param ProfilerReport $report |
123
|
|
|
* @return ProfilerReportFormatter |
124
|
|
|
*/ |
125
|
7 |
|
protected static function getProfilerReportFormatter(ProfilerReport $report): ProfilerReportFormatter |
126
|
|
|
{ |
127
|
|
|
// if (null === static::$profilerReportFormatter) { |
128
|
|
|
// static::$profilerReportFormatter = new ProfilerReportFormatter($report); |
129
|
|
|
// } |
130
|
|
|
return |
131
|
7 |
|
new ProfilerReportFormatter($report); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param BenchmarkReport $report |
136
|
|
|
* @return BenchmarkReportFormatter |
137
|
|
|
*/ |
138
|
5 |
|
protected static function getBenchmarkReportFormatter(BenchmarkReport $report): BenchmarkReportFormatter |
139
|
|
|
{ |
140
|
|
|
// if (null === static::$benchmarkReportFormatter) { |
141
|
|
|
// static::$benchmarkReportFormatter = new BenchmarkReportFormatter($report); |
142
|
|
|
// } |
143
|
|
|
return |
144
|
5 |
|
new BenchmarkReportFormatter($report); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param BenchmarkFunction $function |
149
|
|
|
* @return BenchmarkFunctionFormatter |
150
|
|
|
*/ |
151
|
4 |
|
public static function getBenchmarkFunctionFormatter(BenchmarkFunction $function): BenchmarkFunctionFormatter |
152
|
|
|
{ |
153
|
|
|
// if (null === static::$benchmarkFunctionFormatter) { |
154
|
|
|
// static::$benchmarkFunctionFormatter = new BenchmarkFunctionFormatter($function); |
155
|
|
|
// } |
156
|
4 |
|
return new BenchmarkFunctionFormatter($function); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|