BaseCompressorFilter.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Yui;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Filter\FilterInterface;
  13. use Assetic\Util\ProcessBuilder;
  14. /**
  15. * Base YUI compressor filter.
  16. *
  17. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  18. */
  19. abstract class BaseCompressorFilter implements FilterInterface
  20. {
  21. private $jarPath;
  22. private $javaPath;
  23. private $charset;
  24. private $lineBreak;
  25. public function __construct($jarPath, $javaPath = '/usr/bin/java')
  26. {
  27. $this->jarPath = $jarPath;
  28. $this->javaPath = $javaPath;
  29. }
  30. public function setCharset($charset)
  31. {
  32. $this->charset = $charset;
  33. }
  34. public function setLineBreak($lineBreak)
  35. {
  36. $this->lineBreak = $lineBreak;
  37. }
  38. public function filterLoad(AssetInterface $asset)
  39. {
  40. }
  41. /**
  42. * Compresses a string.
  43. *
  44. * @param string $content The content to compress
  45. * @param string $type The type of content, either "js" or "css"
  46. * @param array $options An indexed array of additional options
  47. *
  48. * @return string The compressed content
  49. */
  50. protected function compress($content, $type, $options = array())
  51. {
  52. $pb = new ProcessBuilder();
  53. $pb
  54. ->inheritEnvironmentVariables()
  55. ->add($this->javaPath)
  56. ->add('-jar')
  57. ->add($this->jarPath)
  58. ;
  59. foreach ($options as $option) {
  60. $pb->add($option);
  61. }
  62. if (null !== $this->charset) {
  63. $pb->add('--charset')->add($this->charset);
  64. }
  65. if (null !== $this->lineBreak) {
  66. $pb->add('--line-break')->add($this->lineBreak);
  67. }
  68. // input and output files
  69. $tempDir = realpath(sys_get_temp_dir());
  70. $hash = substr(sha1(time().rand(11111, 99999)), 0, 7);
  71. $input = $tempDir.DIRECTORY_SEPARATOR.$hash.'.'.$type;
  72. $output = $tempDir.DIRECTORY_SEPARATOR.$hash.'-min.'.$type;
  73. file_put_contents($input, $content);
  74. $pb->add('-o')->add($output)->add($input);
  75. $proc = $pb->getProcess();
  76. $code = $proc->run();
  77. unlink($input);
  78. if (0 < $code) {
  79. if (file_exists($output)) {
  80. unlink($output);
  81. }
  82. throw new \RuntimeException($proc->getErrorOutput());
  83. } elseif (!file_exists($output)) {
  84. throw new \RuntimeException('Error creating output file.');
  85. }
  86. $retval = file_get_contents($output);
  87. unlink($output);
  88. return $retval;
  89. }
  90. }