all files / src/ index.js

71.74% Statements 33/46
16.67% Branches 2/12
37.5% Functions 3/8
71.74% Lines 33/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132              1× 1× 1× 1× 1×           1×   1×   1×   1×                               1×     1× 1× 1×   1× 1× 1× 1×       1×           1×             1×             1× 2×             2×   2× 2× 2×                           2×           1× 2×           1×             1×             1×       1×  
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-transpiler/graphs/contributors
 * @url http://glayzzle.com
 */
'use strict';
 
var extend = require('extend');
var Core = require('./core');
var Stream = require('./stream');
var Context = require('./context/index');
var phpArray = require('./array');
 
 
/**
 * @constructor Runtime
 */
var Runtime = function(options) {
  // the core engine
  this.core = new Core();
  // the context
  this.context = new Context();
  // custom / hack options
  Eif (options) {
    /**
    Sample :
{
  core: {
    config: {
      // PRODUCTION SETUP :
      error_log: 'syslog',
      display_errors: false,
      display_startup_errors: false,
      // CUSTOM EXTENSIONS :
      extension: ['glayzzle-common', 'glayzzle-mysqlnd', 'glayzzle-gd2']
    }
  }
}
    **/
    extend(true, options, this);
  }
  /** initialize streams **/
  this.stdout = new Stream();
  this.stderr = new Stream();
  this.context.isCoreLoading = true;
  /** registers core functions **/
  require('./ext/constants')(this);
  require('./ext/errors')(this);
  require('./ext/math')(this);
  require('./ext/datetime')(this);
  /** @todo starts extensions **/
 
  /** ready to run **/
  this.context.isCoreLoading = false;
};
 
/**
 * Run an eval over the specified PHP code
 */
Runtime.prototype.eval = function(code) {
  return this.core.eval(code)(this);
};
 
/**
 * Creates a new array instance
 */
Runtime.prototype.array = function() {
  return new phpArray(arguments);
};
 
/**
 * Run an inclusion over the specified filename
 */
Runtime.prototype.module = function(filename, once, required) {
  Iif (once) {
    // resolves filename before check
    filename = this.core.resolveFilename(filename);
    if (filename in this.context.includes) {
      return null;
    }
  }
  var result;
  // try to run the include
  try {
    result = this.core.include(filename);
    this.context.includes[result.filename] = true;
  } catch(e) {
    // generate an error
    var fnName = required ? 'require' : 'include';
    if (once) {
      fnName += '_once';
    }
    this.trigger_error(
      fnName + '(): Failed opening \'' + filename + '\' for inclusion (' +
      'include_path=\'' + this.core.config.include_path.join(':') + '\')',
      this.constant(required ? 'E_ERROR' : 'E_WARNING')
    );
    return null;
  }
  return result.fn(this);
};
 
/**
 * run a eval over the specified PHP code
 */
Runtime.prototype.include = function(filename) {
  return this.module(filename, false, false);
};
 
/**
 * includes the specified script only one time
 */
Runtime.prototype.include_once = function(filename) {
  return this.module(filename, true, false);
};
 
/**
 * requires the specified script and throws an error if its not found
 */
Runtime.prototype.require = function(filename) {
  return this.module(filename, false, true);
};
 
/**
 * require_once
 */
Runtime.prototype.require_once = function(filename) {
  return this.module(filename, true, true);
};
 
module.exports = Runtime;