You are here

private function JavaScriptPacker::_bootStrap in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_js_minify/jspacker.inc \JavaScriptPacker::_bootStrap()
  2. 8.3 advagg_js_minify/jspacker.inc \JavaScriptPacker::_bootStrap()
  3. 6 advagg_js_compress/jspacker.inc \JavaScriptPacker::_bootStrap()
  4. 7.2 advagg_js_compress/jspacker.inc \JavaScriptPacker::_bootStrap()
  5. 7 advagg_js_compress/jspacker.inc \JavaScriptPacker::_bootStrap()
1 call to JavaScriptPacker::_bootStrap()
JavaScriptPacker::_encodeKeywords in advagg_js_minify/jspacker.inc

File

advagg_js_minify/jspacker.inc, line 307

Class

JavaScriptPacker

Code

private function _bootStrap($packed, $keywords) {
  $ENCODE = $this
    ->_safeRegExp('$encode\\($count\\)');

  // $packed: the packed script
  $packed = "'" . $this
    ->_escape($packed) . "'";

  // $ascii: base for encoding
  $ascii = min(count($keywords['sorted']), $this->_encoding);
  if ($ascii == 0) {
    $ascii = 1;
  }

  // $count: number of words contained in the script
  $count = count($keywords['sorted']);

  // $keywords: list of words contained in the script
  foreach ($keywords['protected'] as $i => $value) {
    $keywords['sorted'][$i] = '';
  }

  // convert from a string to an array
  ksort($keywords['sorted']);
  $keywords = "'" . implode('|', $keywords['sorted']) . "'.split('|')";
  $encode = $this->_encoding > 62 ? '_encode95' : $this
    ->_getEncoder($ascii);
  $encode = $this
    ->_getJSFunction($encode);
  $encode = preg_replace('/_encoding/', '$ascii', $encode);
  $encode = preg_replace('/arguments\\.callee/', '$encode', $encode);
  $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');

  // $decode: code snippet to speed up decoding
  if ($this->_fastDecode) {

    // create the decoder
    $decode = $this
      ->_getJSFunction('_decodeBody');
    if ($this->_encoding > 62) {
      $decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
    }
    elseif ($ascii < 36) {
      $decode = preg_replace($ENCODE, $inline, $decode);
    }

    // special case: when $count==0 there are no keywords. I want to keep
    //  the basic shape of the unpacking funcion so i'll frig the code...
    if ($count == 0) {
      $decode = preg_replace($this
        ->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
    }
  }

  // boot function
  $unpack = $this
    ->_getJSFunction('_unpack');
  if ($this->_fastDecode) {

    // insert the decoder
    $this->buffer = $decode;
    $unpack = preg_replace_callback('/\\{/', array(
      &$this,
      '_insertFastDecode',
    ), $unpack, 1);
  }
  $unpack = preg_replace('/"/', "'", $unpack);
  if ($this->_encoding > 62) {

    // high-ascii
    // get rid of the word-boundaries for regexp matches
    $unpack = preg_replace('/\'\\\\\\\\b\'\\s*\\+|\\+\\s*\'\\\\\\\\b\'/', '', $unpack);
  }
  if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {

    // insert the encode function
    $this->buffer = $encode;
    $unpack = preg_replace_callback('/\\{/', array(
      &$this,
      '_insertFastEncode',
    ), $unpack, 1);
  }
  else {

    // perform the encoding inline
    $unpack = preg_replace($ENCODE, $inline, $unpack);
  }

  // pack the boot function too
  $unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
  $unpack = $unpackPacker
    ->pack();

  // arguments
  $params = array(
    $packed,
    $ascii,
    $count,
    $keywords,
  );
  if ($this->_fastDecode) {
    $params[] = 0;
    $params[] = '{}';
  }
  $params = implode(',', $params);

  // the whole thing
  return 'eval(' . $unpack . '(' . $params . "))\n";
}