CssInliner.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Css\PreProcessor\Adapter;
  7. use Magento\Framework\App\State;
  8. use Pelago\Emogrifier;
  9. /**
  10. * This class will inline the css of an html to each tag to be used for applications such as a styled email.
  11. */
  12. class CssInliner
  13. {
  14. /**
  15. * @var Emogrifier
  16. */
  17. private $emogrifier;
  18. /**
  19. * @param State $appState
  20. */
  21. public function __construct(State $appState)
  22. {
  23. $this->emogrifier = new Emogrifier();
  24. $this->emogrifier->setDebug($appState->getMode() === State::MODE_DEVELOPER);
  25. }
  26. /**
  27. * Sets the HTML to be used with the css. This method should be used with setCss.
  28. *
  29. * @param string $html
  30. * @return void
  31. */
  32. public function setHtml($html)
  33. {
  34. $this->emogrifier->setHtml($html);
  35. }
  36. /**
  37. * Sets the CSS to be merged with the HTML. This method should be used with setHtml.
  38. *
  39. * @param string $css
  40. * @return void
  41. */
  42. public function setCss($css)
  43. {
  44. $this->emogrifier->setCss($css);
  45. }
  46. /**
  47. * Disables the parsing of <style> blocks.
  48. *
  49. * @return void
  50. */
  51. public function disableStyleBlocksParsing()
  52. {
  53. $this->emogrifier->disableStyleBlocksParsing();
  54. }
  55. /**
  56. * Processes the html by placing the css inline. Set first the css by using setCss and html by using setHtml.
  57. *
  58. * @return string
  59. * @throws \BadMethodCallException
  60. */
  61. public function process()
  62. {
  63. return $this->emogrifier->emogrify();
  64. }
  65. }