1: <?
2: namespace RM\AssetsCollector\Compilers;
3:
4: use Nette\FileNotFoundException,
5: Nette\InvalidArgumentException,
6: \RM\AssetsCollector;
7:
8: 9: 10: 11: 12: 13: 14:
15: class ImageReplacer extends BaseCssAssetsCompiler implements IAssetsCompiler
16: {
17: 18: 19: 20: 21:
22: public function compile($input,$dir=null)
23: {
24: $this->checkRequirements();
25: $this->input = $this->output = $input;
26: $images = $this->getImages();
27: if ($images) {
28: foreach($images as $img) {
29: if ($img[0]!=="/") {
30: $source_file = AssetsCollector::findFile($img,array($dir,$this->cssPath,$this->wwwDir));
31: $hash = md5_file($source_file);
32: $f = explode(".",$source_file);
33: $ext = array_pop($f);
34: $output_file = $this->webTemp."/".$hash.".".$ext;
35: if (!file_exists($output_file))
36: copy($source_file,$output_file);
37: $this->output = str_replace($img,substr($output_file,strlen(WWW_DIR)),$this->output);
38: }
39: }
40: }
41: return $this->output;
42: }
43:
44: 45: 46:
47: private function checkRequirements()
48: {
49: if (is_null($this->webTemp))
50: throw new InvalidArgumentException("Directory for temporary files is not defined.");
51: if (!is_dir($this->webTemp))
52: throw new FileNotFoundException($this->webTemp." is not directory.");
53: if (!is_writable($this->webTemp))
54: throw new InvalidArgumentException("Directory '".$this->webTemp."' is not writeable.");
55: }
56: }
57: