1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\Debug\Formatter; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\TransferException; |
6
|
|
|
use GuzzleHttp\Message\RequestInterface; |
7
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Emil Kilhage |
11
|
|
|
*/ |
12
|
|
|
class FormatterFactory implements FormatterFactoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private static $defaultFormatter = PrintrFormatter::class; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $defaultFormatter |
21
|
|
|
*/ |
22
|
|
|
public static function setDefaultFormatter($defaultFormatter) |
23
|
|
|
{ |
24
|
|
|
self::$defaultFormatter = $defaultFormatter; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $classMap = [ |
31
|
|
|
\SimpleXMLElement::class => SimpleXMLElementFormatter::class, |
32
|
|
|
TransferException::class => Guzzle\GuzzleExceptionFormatter::class, |
33
|
|
|
\GuzzleHttp\Stream\Stream::class => StringFactoryFormatter::class, |
34
|
|
|
ResponseInterface::class => Guzzle\ResponseFormatter::class, |
35
|
|
|
RequestInterface::class => Guzzle\RequestFormatter::class, |
36
|
|
|
\Exception::class => ExceptionFormatter::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function factory($output) |
43
|
|
|
{ |
44
|
|
|
if (is_string($output) || is_int($output)) { |
45
|
|
|
return $this->setup(StringFactoryFormatter::class); |
46
|
|
|
} elseif (is_object($output)) { |
47
|
|
|
foreach (self::$classMap as $className => $formatterClassName) { |
48
|
|
|
if (is_a($output, $className)) { |
49
|
|
|
return $this->setup($formatterClassName); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->setup(self::$defaultFormatter); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $className |
59
|
|
|
* @return FormatterInterface |
60
|
|
|
*/ |
61
|
|
|
private function setup($className) |
62
|
|
|
{ |
63
|
|
|
$formatter = new $className(); |
64
|
|
|
return $formatter; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|