vendor/knplabs/knp-snappy-bundle/src/Snappy/Generator/LoggableGenerator.php line 5

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