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