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