|
4 | 4 |
|
5 | 5 | class Manifest |
6 | 6 | { |
| 7 | + public $compress = false; |
| 8 | + |
| 9 | + public $files; |
| 10 | + public $assets; |
| 11 | + |
| 12 | + protected $environment; |
| 13 | + protected $directory; |
| 14 | + |
| 15 | + # Path to manifest file |
7 | 16 | protected $manifest; |
8 | 17 |
|
9 | | - function __construct() |
| 18 | + function __construct(Environment $env, $manifest, $dir = '') |
10 | 19 | { |
11 | | - $this->manifest = new \StdClass; |
| 20 | + $this->files = new \StdClass; |
| 21 | + $this->assets = new \StdClass; |
| 22 | + |
| 23 | + $this->environment = $env; |
| 24 | + |
| 25 | + if (empty($dir)) { |
| 26 | + $this->directory = dirname($manifest); |
| 27 | + } else { |
| 28 | + $this->directory = $dir; |
| 29 | + } |
| 30 | + |
| 31 | + $this->manifest = $manifest; |
12 | 32 | } |
13 | 33 |
|
14 | | - function add($asset) |
| 34 | + function read() |
15 | 35 | { |
16 | | - $this->manifest->{$asset->logicalPath} = $asset->getDigestName(); |
17 | | - return $this; |
| 36 | + if (!realpath($this->manifest)) { |
| 37 | + throw new \UnexpectedValueException(sprintf( |
| 38 | + 'Manifest file "%s" not found', $this->manifest |
| 39 | + )); |
| 40 | + } |
| 41 | + |
| 42 | + $m = json_decode(file_get_contents($this->manifest)); |
| 43 | + |
| 44 | + if (isset($m->files)) { |
| 45 | + foreach ($m->files as $file => $info) { |
| 46 | + $this->files->{$file} = $info; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (isset($m->assets)) { |
| 51 | + foreach ($m->assets as $logicalPath => $digestName) { |
| 52 | + $this->assets->{$logicalPath} = $digestName; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + # Compiles one or more assets (by logical path) and writes the manifest. |
| 58 | + # |
| 59 | + # Example |
| 60 | + # |
| 61 | + # $manifest = new Manifest($env, 'manifest.json', '.'); |
| 62 | + # $manifest->compile('boo.js'); |
| 63 | + # $manifest->compile(array('foo.js', 'bar.js')); |
| 64 | + # |
| 65 | + function compile($assets) |
| 66 | + { |
| 67 | + $env = $this->environment; |
| 68 | + |
| 69 | + if (!is_dir($this->directory)) { |
| 70 | + mkdir($this->directory, 0700, true); |
| 71 | + } |
| 72 | + |
| 73 | + $assets = array_filter(array_map(function($path) use ($env) { |
| 74 | + return $env->find($path, array('bundled' => true)); |
| 75 | + }, (array) $assets)); |
| 76 | + |
| 77 | + foreach ($assets as $asset) { |
| 78 | + $this->files->{$asset->getDigestName()} = array( |
| 79 | + 'logical_path' => $asset->getLogicalPath(), |
| 80 | + 'mtime' => date(DATE_ISO8601, $asset->getLastModified()), |
| 81 | + 'size' => strlen($asset->getBody()), |
| 82 | + 'digest' => $asset->getDigest() |
| 83 | + ); |
| 84 | + |
| 85 | + $this->assets->{$asset->getLogicalPath()} = $asset->getDigestName(); |
| 86 | + |
| 87 | + $asset->write(array( |
| 88 | + 'dir' => $this->directory, |
| 89 | + 'compress' => false, |
| 90 | + 'include_digest' => true |
| 91 | + )); |
| 92 | + |
| 93 | + if ($this->compress) { |
| 94 | + $asset->write(array( |
| 95 | + "dir" => $this->directory, |
| 96 | + "compress" => true, |
| 97 | + "include_digest" => true |
| 98 | + )); |
| 99 | + } |
| 100 | + |
| 101 | + $this->save(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + function save() |
| 106 | + { |
| 107 | + return file_put_contents($this->manifest, $this->toJSON()); |
18 | 108 | } |
19 | 109 |
|
20 | 110 | function toJSON() |
21 | 111 | { |
22 | | - return json_encode($this->manifest); |
| 112 | + return json_encode(array( |
| 113 | + 'assets' => $this->assets, |
| 114 | + 'files' => $this->files, |
| 115 | + )); |
23 | 116 | } |
24 | 117 | } |
| 118 | + |
0 commit comments