CompilerJarFilter.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\GoogleClosure;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Util\ProcessBuilder;
  13. /**
  14. * Filter for the Google Closure Compiler JAR.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class CompilerJarFilter extends BaseCompilerFilter
  19. {
  20. private $jarPath;
  21. private $javaPath;
  22. public function __construct($jarPath, $javaPath = '/usr/bin/java')
  23. {
  24. $this->jarPath = $jarPath;
  25. $this->javaPath = $javaPath;
  26. }
  27. public function filterDump(AssetInterface $asset)
  28. {
  29. $cleanup = array();
  30. $pb = new ProcessBuilder();
  31. $pb
  32. ->inheritEnvironmentVariables()
  33. ->add($this->javaPath)
  34. ->add('-jar')
  35. ->add($this->jarPath)
  36. ;
  37. if (null !== $this->compilationLevel) {
  38. $pb->add('--compilation_level')->add($this->compilationLevel);
  39. }
  40. if (null !== $this->jsExterns) {
  41. $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
  42. file_put_contents($externs, $this->jsExterns);
  43. $pb->add('--externs')->add($externs);
  44. }
  45. if (null !== $this->externsUrl) {
  46. $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler');
  47. file_put_contents($externs, file_get_contents($this->externsUrl));
  48. $pb->add('--externs')->add($externs);
  49. }
  50. if (null !== $this->excludeDefaultExterns) {
  51. $pb->add('--use_only_custom_externs');
  52. }
  53. if (null !== $this->formatting) {
  54. $pb->add('--formatting')->add($this->formatting);
  55. }
  56. if (null !== $this->useClosureLibrary) {
  57. $pb->add('--manage_closure_dependencies');
  58. }
  59. if (null !== $this->warningLevel) {
  60. $pb->add('--warning_level')->add($this->warningLevel);
  61. }
  62. $pb->add('--js')->add($cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'));
  63. file_put_contents($input, $asset->getContent());
  64. $proc = $pb->getProcess();
  65. $code = $proc->run();
  66. array_map('unlink', $cleanup);
  67. if (0 < $code) {
  68. throw new \RuntimeException($proc->getErrorOutput());
  69. }
  70. $asset->setContent($proc->getOutput());
  71. }
  72. }