[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; |
---|
| 13 | |
---|
| 14 | use Assetic\Util\PathUtils; |
---|
| 15 | |
---|
| 16 | use Assetic\Asset\AssetInterface; |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Writes assets to the filesystem. |
---|
| 20 | * |
---|
| 21 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
| 22 | * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
---|
| 23 | */ |
---|
| 24 | class AssetWriter |
---|
| 25 | { |
---|
| 26 | private $dir; |
---|
| 27 | private $varValues; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Constructor. |
---|
| 31 | * |
---|
| 32 | * @param string $dir The base web directory |
---|
| 33 | * @param array $varValues |
---|
| 34 | * |
---|
| 35 | * @throws \InvalidArgumentException if a variable value is not a string |
---|
| 36 | */ |
---|
| 37 | public function __construct($dir, array $varValues = array()) |
---|
| 38 | { |
---|
| 39 | foreach ($varValues as $var => $values) { |
---|
| 40 | foreach ($values as $value) { |
---|
| 41 | if (!is_string($value)) { |
---|
| 42 | throw new \InvalidArgumentException(sprintf('All variable values must be strings, but got %s for variable "%s".', json_encode($value), $var)); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | $this->dir = $dir; |
---|
| 48 | $this->varValues = $varValues; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public function writeManagerAssets(AssetManager $am) |
---|
| 52 | { |
---|
| 53 | foreach ($am->getNames() as $name) { |
---|
| 54 | $this->writeAsset($am->get($name)); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public function writeAsset(AssetInterface $asset) |
---|
| 59 | { |
---|
| 60 | foreach ($this->getCombinations($asset->getVars()) as $combination) { |
---|
| 61 | $asset->setValues($combination); |
---|
| 62 | |
---|
| 63 | static::write($this->dir.'/'.PathUtils::resolvePath( |
---|
| 64 | $asset->getTargetPath(), $asset->getVars(), $asset->getValues()), |
---|
| 65 | $asset->dump()); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | private function getCombinations(array $vars) |
---|
| 70 | { |
---|
| 71 | if (!$vars) { |
---|
| 72 | return array(array()); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | $combinations = array(); |
---|
| 76 | $nbValues = array(); |
---|
| 77 | foreach ($this->varValues as $var => $values) { |
---|
| 78 | if (!in_array($var, $vars, true)) { |
---|
| 79 | continue; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | $nbValues[$var] = count($values); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | for ($i=array_product($nbValues),$c=$i*2; $i<$c; $i++) { |
---|
| 86 | $k = $i; |
---|
| 87 | $combination = array(); |
---|
| 88 | |
---|
| 89 | foreach ($vars as $var) { |
---|
| 90 | $combination[$var] = $this->varValues[$var][$k % $nbValues[$var]]; |
---|
| 91 | $k = intval($k/$nbValues[$var]); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | $combinations[] = $combination; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | return $combinations; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | protected static function write($path, $contents) |
---|
| 101 | { |
---|
| 102 | if (!is_dir($dir = dirname($path)) && false === @mkdir($dir, 0777, true)) { |
---|
| 103 | throw new \RuntimeException('Unable to create directory '.$dir); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (false === @file_put_contents($path, $contents)) { |
---|
| 107 | throw new \RuntimeException('Unable to write file '.$path); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } |
---|