|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 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\Asset\AssetInterface; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Writes assets to the filesystem. |
|
|
18 |
* |
|
|
19 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
20 |
*/ |
|
|
21 |
class AssetWriter |
|
|
22 |
{ |
|
|
23 |
private $dir; |
|
|
24 |
|
|
|
25 |
/** |
|
|
26 |
* Constructor. |
|
|
27 |
* |
|
|
28 |
* @param string $dir The base web directory |
|
|
29 |
*/ |
|
|
30 |
public function __construct($dir) |
|
|
31 |
{ |
|
|
32 |
$this->dir = $dir; |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
public function writeManagerAssets(AssetManager $am) |
|
|
36 |
{ |
|
|
37 |
foreach ($am->getNames() as $name) { |
|
|
38 |
$this->writeAsset($am->get($name)); |
|
|
39 |
} |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
public function writeAsset(AssetInterface $asset) |
|
|
43 |
{ |
|
|
44 |
static::write($this->dir . '/' . $asset->getTargetPath(), $asset->dump()); |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
static protected function write($path, $contents) |
|
|
48 |
{ |
|
|
49 |
if (!is_dir($dir = dirname($path)) && false === @mkdir($dir, 0777, true)) { |
|
|
50 |
throw new \RuntimeException('Unable to create directory '.$dir); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
if (false === @file_put_contents($path, $contents)) { |
|
|
54 |
throw new \RuntimeException('Unable to write file '.$path); |
|
|
55 |
} |
|
|
56 |
} |
|
|
57 |
} |