1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\Bundle\SnappyBundle\Snappy\Generator; |
4
|
|
|
|
5
|
|
|
@trigger_error('Logging capability is now directly integrated in Snappy. You should call setLogger on your generator rather than using this decorator.', E_USER_DEPRECATED); |
|
|
|
|
6
|
|
|
|
7
|
|
|
use Knp\Snappy\GeneratorInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Wraps a GeneratorInterface instance to log the media generations using the |
12
|
|
|
* configured logger. |
13
|
|
|
* |
14
|
|
|
* @deprecated Logging capability is now directly integrated in Snappy. You should use it rather than this Decorator. |
15
|
|
|
*/ |
16
|
|
|
class LoggableGenerator implements GeneratorInterface |
17
|
|
|
{ |
18
|
|
|
private $generator; |
19
|
|
|
private $logger; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param GeneratorInterface $generator |
25
|
|
|
* @param LoggerInterface $logger |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct(GeneratorInterface $generator, LoggerInterface $logger = null) |
28
|
|
|
{ |
29
|
|
|
$this->generator = $generator; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the underlying generator instance. |
35
|
|
|
* |
36
|
|
|
* @return GeneratorInterface |
37
|
|
|
*/ |
38
|
|
|
public function getInternalGenerator() |
39
|
|
|
{ |
40
|
|
|
return $this->generator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function generate($input, $output, array $options = [], $overwrite = false) |
47
|
|
|
{ |
48
|
|
|
if (is_array($input)) { |
49
|
|
|
$debug_input = implode(', ', $input); |
50
|
|
|
} else { |
51
|
|
|
$debug_input = $input; |
52
|
|
|
} |
53
|
|
|
$this->logDebug(sprintf('Generate from file (%s) to file (%s).', $debug_input, $output)); |
54
|
|
|
|
55
|
|
|
$this->generator->generate($input, $output, $options, $overwrite); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function generateFromHtml($html, $output, array $options = [], $overwrite = false) |
62
|
|
|
{ |
63
|
|
|
$debugHtml = is_array($html) ? implode(', ', $html) : $html; |
64
|
|
|
|
65
|
|
|
$this->logDebug(sprintf('Generate from HTML (%s) to file (%s).', substr($debugHtml, 0, 100), $output)); |
66
|
|
|
|
67
|
|
|
$this->generator->generateFromHtml($html, $output, $options, $overwrite); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getOutput($input, array $options = []) |
74
|
|
|
{ |
75
|
|
|
if (is_array($input)) { |
76
|
|
|
$debug_input = implode(', ', $input); |
77
|
|
|
} else { |
78
|
|
|
$debug_input = $input; |
79
|
|
|
} |
80
|
|
|
$this->logDebug(sprintf('Output from file (%s).', $debug_input)); |
81
|
|
|
|
82
|
|
|
return $this->generator->getOutput($input, $options); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getOutputFromHtml($html, array $options = []) |
89
|
|
|
{ |
90
|
|
|
$debugHtml = is_array($html) ? implode(', ', $html) : $html; |
91
|
|
|
|
92
|
|
|
$this->logDebug(sprintf('Output from HTML (%s).', substr($debugHtml, 0, 100))); |
93
|
|
|
|
94
|
|
|
return $this->generator->getOutputFromHtml($html, $options); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function setOption($name, $value) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->logDebug(sprintf('Set option %s = %s.', $name, var_export($value, true))); |
103
|
|
|
|
104
|
|
|
return $this->generator->setOption($name, $value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Logs the given debug message if the logger is configured or do nothing |
109
|
|
|
* otherwise. |
110
|
|
|
* |
111
|
|
|
* @param string $message |
112
|
|
|
*/ |
113
|
|
|
private function logDebug($message) |
114
|
|
|
{ |
115
|
|
|
if (null === $this->logger) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->logger->debug($message); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: