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\Asset; 13 14use Assetic\AssetManager; 15use Assetic\Filter\FilterInterface; 16 17/** 18 * A reference to an asset in the asset manager. 19 * 20 * @author Kris Wallsmith <kris.wallsmith@gmail.com> 21 */ 22class AssetReference implements AssetInterface 23{ 24 private $am; 25 private $name; 26 private $filters = array(); 27 private $clone = false; 28 private $asset; 29 30 public function __construct(AssetManager $am, $name) 31 { 32 $this->am = $am; 33 $this->name = $name; 34 } 35 36 public function __clone() 37 { 38 $this->clone = true; 39 40 if ($this->asset) { 41 $this->asset = clone $this->asset; 42 } 43 } 44 45 public function ensureFilter(FilterInterface $filter) 46 { 47 $this->filters[] = $filter; 48 } 49 50 public function getFilters() 51 { 52 $this->flushFilters(); 53 54 return $this->callAsset(__FUNCTION__); 55 } 56 57 public function clearFilters() 58 { 59 $this->filters = array(); 60 $this->callAsset(__FUNCTION__); 61 } 62 63 public function load(FilterInterface $additionalFilter = null) 64 { 65 $this->flushFilters(); 66 67 return $this->callAsset(__FUNCTION__, array($additionalFilter)); 68 } 69 70 public function dump(FilterInterface $additionalFilter = null) 71 { 72 $this->flushFilters(); 73 74 return $this->callAsset(__FUNCTION__, array($additionalFilter)); 75 } 76 77 public function getContent() 78 { 79 return $this->callAsset(__FUNCTION__); 80 } 81 82 public function setContent($content) 83 { 84 $this->callAsset(__FUNCTION__, array($content)); 85 } 86 87 public function getSourceRoot() 88 { 89 return $this->callAsset(__FUNCTION__); 90 } 91 92 public function getSourcePath() 93 { 94 return $this->callAsset(__FUNCTION__); 95 } 96 97 public function getSourceDirectory() 98 { 99 return $this->callAsset(__FUNCTION__); 100 } 101 102 public function getTargetPath() 103 { 104 return $this->callAsset(__FUNCTION__); 105 } 106 107 public function setTargetPath($targetPath) 108 { 109 $this->callAsset(__FUNCTION__, array($targetPath)); 110 } 111 112 public function getLastModified() 113 { 114 return $this->callAsset(__FUNCTION__); 115 } 116 117 public function getVars() 118 { 119 return $this->callAsset(__FUNCTION__); 120 } 121 122 public function getValues() 123 { 124 return $this->callAsset(__FUNCTION__); 125 } 126 127 public function setValues(array $values) 128 { 129 $this->callAsset(__FUNCTION__, array($values)); 130 } 131 132 // private 133 134 private function callAsset($method, $arguments = array()) 135 { 136 $asset = $this->resolve(); 137 138 return call_user_func_array(array($asset, $method), $arguments); 139 } 140 141 private function flushFilters() 142 { 143 $asset = $this->resolve(); 144 145 while ($filter = array_shift($this->filters)) { 146 $asset->ensureFilter($filter); 147 } 148 } 149 150 private function resolve() 151 { 152 if ($this->asset) { 153 return $this->asset; 154 } 155 156 $asset = $this->am->get($this->name); 157 158 if ($this->clone) { 159 $asset = $this->asset = clone $asset; 160 } 161 162 return $asset; 163 } 164} 165