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\Filter; |
---|
13 | |
---|
14 | use Assetic\Asset\AssetInterface; |
---|
15 | |
---|
16 | /** |
---|
17 | * Runs assets through Packager. |
---|
18 | * |
---|
19 | * @link https://github.com/kamicane/packager |
---|
20 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
21 | */ |
---|
22 | class PackagerFilter implements FilterInterface |
---|
23 | { |
---|
24 | private $packages; |
---|
25 | |
---|
26 | public function __construct(array $packages = array()) |
---|
27 | { |
---|
28 | $this->packages = $packages; |
---|
29 | } |
---|
30 | |
---|
31 | public function addPackage($package) |
---|
32 | { |
---|
33 | $this->packages[] = $package; |
---|
34 | } |
---|
35 | |
---|
36 | public function filterLoad(AssetInterface $asset) |
---|
37 | { |
---|
38 | static $manifest = <<<EOF |
---|
39 | name: Application%s |
---|
40 | sources: [source.js] |
---|
41 | |
---|
42 | EOF; |
---|
43 | |
---|
44 | $hash = substr(sha1(time().rand(11111, 99999)), 0, 7); |
---|
45 | $package = sys_get_temp_dir().'/assetic_packager_'.$hash; |
---|
46 | |
---|
47 | mkdir($package); |
---|
48 | file_put_contents($package.'/package.yml', sprintf($manifest, $hash)); |
---|
49 | file_put_contents($package.'/source.js', $asset->getContent()); |
---|
50 | |
---|
51 | $packager = new \Packager(array_merge(array($package), $this->packages)); |
---|
52 | $content = $packager->build(array(), array(), array('Application'.$hash)); |
---|
53 | |
---|
54 | unlink($package.'/package.yml'); |
---|
55 | unlink($package.'/source.js'); |
---|
56 | rmdir($package); |
---|
57 | |
---|
58 | $asset->setContent($content); |
---|
59 | } |
---|
60 | |
---|
61 | public function filterDump(AssetInterface $asset) |
---|
62 | { |
---|
63 | } |
---|
64 | } |
---|