protected function BaseCompressorFilter::compress

Compresses a string.

Parameters

string $content The content to compress:

string $type The type of content, either "js" or "css":

array $options An indexed array of additional options:

Return value

string The compressed content

2 calls to BaseCompressorFilter::compress()
CssCompressorFilter::filterDump in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/Yui/CssCompressorFilter.php
Filters an asset just before it's dumped.
JsCompressorFilter::filterDump in drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/Yui/JsCompressorFilter.php
Filters an asset just before it's dumped.

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/Yui/BaseCompressorFilter.php, line 61

Class

BaseCompressorFilter
Base YUI compressor filter.

Namespace

Assetic\Filter\Yui

Code

protected function compress($content, $type, $options = array()) {
  $pb = new ProcessBuilder(array(
    $this->javaPath,
    '-jar',
    $this->jarPath,
  ));
  foreach ($options as $option) {
    $pb
      ->add($option);
  }
  if (null !== $this->charset) {
    $pb
      ->add('--charset')
      ->add($this->charset);
  }
  if (null !== $this->lineBreak) {
    $pb
      ->add('--line-break')
      ->add($this->lineBreak);
  }

  // input and output files
  $tempDir = realpath(sys_get_temp_dir());
  $input = tempnam($tempDir, 'YUI-IN-');
  $output = tempnam($tempDir, 'YUI-OUT-');
  file_put_contents($input, $content);
  $pb
    ->add('-o')
    ->add($output)
    ->add('--type')
    ->add($type)
    ->add($input);
  $proc = $pb
    ->getProcess();
  $code = $proc
    ->run();
  unlink($input);
  if (0 < $code) {
    if (file_exists($output)) {
      unlink($output);
    }
    throw FilterException::fromProcess($proc)
      ->setInput($content);
  }
  elseif (!file_exists($output)) {
    throw new \RuntimeException('Error creating output file.');
  }
  $retval = file_get_contents($output);
  unlink($output);
  return $retval;
}