class CompilerApiFilter

Filter for the Google Closure Compiler API.

@link https://developers.google.com/closure/compiler/ @author Kris Wallsmith <kris.wallsmith@gmail.com>

Hierarchy

Expanded class hierarchy of CompilerApiFilter

1 file declares its use of CompilerApiFilter
CompilerApiFilterTest.php in drupal/core/vendor/kriswallsmith/assetic/tests/Assetic/Test/Filter/GoogleClosure/CompilerApiFilterTest.php

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/GoogleClosure/CompilerApiFilter.php, line 22

Namespace

Assetic\Filter\GoogleClosure
View source
class CompilerApiFilter extends BaseCompilerFilter {
  public function filterDump(AssetInterface $asset) {
    $query = array(
      'js_code' => $asset
        ->getContent(),
      'output_format' => 'json',
      'output_info' => 'compiled_code',
    );
    if (null !== $this->compilationLevel) {
      $query['compilation_level'] = $this->compilationLevel;
    }
    if (null !== $this->jsExterns) {
      $query['js_externs'] = $this->jsExterns;
    }
    if (null !== $this->externsUrl) {
      $query['externs_url'] = $this->externsUrl;
    }
    if (null !== $this->excludeDefaultExterns) {
      $query['exclude_default_externs'] = $this->excludeDefaultExterns ? 'true' : 'false';
    }
    if (null !== $this->formatting) {
      $query['formatting'] = $this->formatting;
    }
    if (null !== $this->useClosureLibrary) {
      $query['use_closure_library'] = $this->useClosureLibrary ? 'true' : 'false';
    }
    if (null !== $this->warningLevel) {
      $query['warning_level'] = $this->warningLevel;
    }
    if (null !== $this->language) {
      $query['language'] = $this->language;
    }
    if (preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'))) {
      $context = stream_context_create(array(
        'http' => array(
          'method' => 'POST',
          'header' => 'Content-Type: application/x-www-form-urlencoded',
          'content' => http_build_query($query),
        ),
      ));
      $response = file_get_contents('http://closure-compiler.appspot.com/compile', false, $context);
      $data = json_decode($response);
    }
    elseif (defined('CURLOPT_POST') && !in_array('curl_init', explode(',', ini_get('disable_functions')))) {
      $ch = curl_init('http://closure-compiler.appspot.com/compile');
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-type: application/x-www-form-urlencoded',
      ));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
      $response = curl_exec($ch);
      curl_close($ch);
      $data = json_decode($response);
    }
    else {
      throw new \RuntimeException("There is no known way to contact closure compiler available");
    }
    if (isset($data->serverErrors) && 0 < count($data->serverErrors)) {

      // @codeCoverageIgnoreStart
      throw new \RuntimeException(sprintf('The Google Closure Compiler API threw some server errors: ' . print_r($data->serverErrors, true)));

      // @codeCoverageIgnoreEnd
    }
    if (isset($data->errors) && 0 < count($data->errors)) {

      // @codeCoverageIgnoreStart
      throw new \RuntimeException(sprintf('The Google Closure Compiler API threw some errors: ' . print_r($data->errors, true)));

      // @codeCoverageIgnoreEnd
    }
    $asset
      ->setContent($data->compiledCode);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseCompilerFilter::$compilationLevel protected property
BaseCompilerFilter::$excludeDefaultExterns protected property
BaseCompilerFilter::$externsUrl protected property
BaseCompilerFilter::$formatting protected property
BaseCompilerFilter::$jsExterns protected property
BaseCompilerFilter::$language protected property
BaseCompilerFilter::$useClosureLibrary protected property
BaseCompilerFilter::$warningLevel protected property
BaseCompilerFilter::COMPILE_ADVANCED_OPTIMIZATIONS constant
BaseCompilerFilter::COMPILE_SIMPLE_OPTIMIZATIONS constant
BaseCompilerFilter::COMPILE_WHITESPACE_ONLY constant
BaseCompilerFilter::filterLoad public function Filters an asset after it has been loaded. Overrides FilterInterface::filterLoad
BaseCompilerFilter::FORMAT_PRETTY_PRINT constant
BaseCompilerFilter::FORMAT_PRINT_INPUT_DELIMITER constant
BaseCompilerFilter::LANGUAGE_ECMASCRIPT3 constant
BaseCompilerFilter::LANGUAGE_ECMASCRIPT5 constant
BaseCompilerFilter::LANGUAGE_ECMASCRIPT5_STRICT constant
BaseCompilerFilter::LEVEL_DEFAULT constant
BaseCompilerFilter::LEVEL_QUIET constant
BaseCompilerFilter::LEVEL_VERBOSE constant
BaseCompilerFilter::setCompilationLevel public function
BaseCompilerFilter::setExcludeDefaultExterns public function
BaseCompilerFilter::setExternsUrl public function
BaseCompilerFilter::setFormatting public function
BaseCompilerFilter::setJsExterns public function
BaseCompilerFilter::setLanguage public function
BaseCompilerFilter::setUseClosureLibrary public function
BaseCompilerFilter::setWarningLevel public function
CompilerApiFilter::filterDump public function Filters an asset just before it's dumped. Overrides FilterInterface::filterDump