PackagerFilter.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. /**
  13. * Runs assets through Packager.
  14. *
  15. * @link https://github.com/kamicane/packager
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class PackagerFilter implements FilterInterface
  19. {
  20. private $packages;
  21. public function __construct(array $packages = array())
  22. {
  23. $this->packages = $packages;
  24. }
  25. public function addPackage($package)
  26. {
  27. $this->packages[] = $package;
  28. }
  29. public function filterLoad(AssetInterface $asset)
  30. {
  31. static $manifest = <<<EOF
  32. name: Application%s
  33. sources: [source.js]
  34. EOF;
  35. $hash = substr(sha1(time().rand(11111, 99999)), 0, 7);
  36. $package = sys_get_temp_dir().'/assetic_packager_'.$hash;
  37. mkdir($package);
  38. file_put_contents($package.'/package.yml', sprintf($manifest, $hash));
  39. file_put_contents($package.'/source.js', $asset->getContent());
  40. $packager = new \Packager(array_merge(array($package), $this->packages));
  41. $content = $packager->build(array(), array(), array('Application'.$hash));
  42. unlink($package.'/package.yml');
  43. unlink($package.'/source.js');
  44. rmdir($package);
  45. $asset->setContent($content);
  46. }
  47. public function filterDump(AssetInterface $asset)
  48. {
  49. }
  50. }