CssEmbedFilter.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Filter\FilterInterface;
  13. use Assetic\Util\ProcessBuilder;
  14. /**
  15. * CSSEmbed filter
  16. *
  17. * @author Maxime Thirouin <maxime.thirouin@gmail.com>
  18. */
  19. class CssEmbedFilter implements FilterInterface
  20. {
  21. private $jarPath;
  22. private $javaPath;
  23. private $charset;
  24. private $mhtml; // Enable MHTML mode.
  25. private $mhtmlRoot; // Use <root> as the MHTML root for the file.
  26. private $root; // Prepends <root> to all relative URLs.
  27. private $skipMissing; // Don't throw an error for missing image files.
  28. private $maxUriLength; // Maximum length for a data URI. Defaults to 32768.
  29. private $maxImageSize; // Maximum image size (in bytes) to convert.
  30. public function __construct($jarPath, $javaPath = '/usr/bin/java')
  31. {
  32. $this->jarPath = $jarPath;
  33. $this->javaPath = $javaPath;
  34. }
  35. public function setCharset($charset)
  36. {
  37. $this->charset = $charset;
  38. }
  39. public function setMhtml($mhtml)
  40. {
  41. $this->mhtml = $mhtml;
  42. }
  43. public function setMhtmlRoot($mhtmlRoot)
  44. {
  45. $this->mhtmlRoot = $mhtmlRoot;
  46. }
  47. public function setRoot($root)
  48. {
  49. $this->root = $root;
  50. }
  51. public function setSkipMissing($skipMissing)
  52. {
  53. $this->skipMissing = $skipMissing;
  54. }
  55. public function setMaxUriLength($maxUriLength)
  56. {
  57. $this->maxUriLength = $maxUriLength;
  58. }
  59. public function setMaxImageSize($maxImageSize)
  60. {
  61. $this->maxImageSize = $maxImageSize;
  62. }
  63. public function filterLoad(AssetInterface $asset)
  64. {
  65. }
  66. public function filterDump(AssetInterface $asset)
  67. {
  68. $pb = new ProcessBuilder(array(
  69. $this->javaPath,
  70. '-jar',
  71. $this->jarPath,
  72. ));
  73. if (null !== $this->charset) {
  74. $pb->add('--charset')->add($this->charset);
  75. }
  76. if ($this->mhtml) {
  77. $pb->add('--mhtml');
  78. }
  79. if (null !== $this->mhtmlRoot) {
  80. $pb->add('--mhtmlroot')->add($this->mhtmlRoot);
  81. }
  82. // automatically define root if not already defined
  83. if (null === $this->root) {
  84. $root = $asset->getSourceRoot();
  85. $path = $asset->getSourcePath();
  86. if ($root && $path) {
  87. $pb->add('--root')->add(dirname($root.'/'.$path));
  88. }
  89. } else {
  90. $pb->add('--root')->add($this->root);
  91. }
  92. if ($this->skipMissing) {
  93. $pb->add('--skip-missing');
  94. }
  95. if (null !== $this->maxUriLength) {
  96. $pb->add('--max-uri-length')->add($this->maxUriLength);
  97. }
  98. if (null !== $this->maxImageSize) {
  99. $pb->add('--max-image-size')->add($this->maxImageSize);
  100. }
  101. // input
  102. $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_cssembed'));
  103. file_put_contents($input, $asset->getContent());
  104. $proc = $pb->getProcess();
  105. $code = $proc->run();
  106. unlink($input);
  107. if (0 < $code) {
  108. throw new \RuntimeException($proc->getErrorOutput());
  109. }
  110. $asset->setContent($proc->getOutput());
  111. }
  112. }