Skip to content

Commit

Permalink
add setLoader method
Browse files Browse the repository at this point in the history
  • Loading branch information
chenos committed Feb 7, 2018
1 parent 67ba6e3 commit ce2d324
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ services:
context: ./docker/
dockerfile: ${DOCKERFILE:-Dockerfile-71}
environment:
# TRAVIS: ${TRAVIS:-''}
# TRAVIS_JOB_ID: ${TRAVIS_JOB_ID:-''}
COVERALLS_RUN_LOCALLY: 1
COVERALLS_REPO_TOKEN: gfDOr1cMqeqyzMmFgu54HpUcJvsZJf6yV
TRAVIS: ${TRAVIS:-''}
TRAVIS_JOB_ID: ${TRAVIS_JOB_ID:-''}
working_dir: /var/www/html
volumes:
- ./:/var/www/html
Expand Down
25 changes: 14 additions & 11 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ class Context

protected $variables = [];

public function __construct($v8name = 'PHP')
public function __construct($v8name = 'PHP', ModuleLoader $loader = null)
{
$this->v8name = $v8name;
$this->loader = new ModuleLoader();
$this->v8 = new V8Js($v8name);

$this->v8->setModuleNormaliser([$this->loader, 'normaliseIdentifier']);
$this->v8->setModuleLoader([$this->loader, 'loadModule']);
$this->v8->executeString("global.requireDefault = function (m) {
$this->setLoader($loader ?: new ModuleLoader());
$this->eval("global.requireDefault = function (m) {
var o = require(m);return o && o.__esModule ? o.default : o;};");
}

Expand Down Expand Up @@ -59,21 +56,20 @@ public function load($file, int $flags = V8Js::FLAG_NONE, int $timeLimit = 0, in

public function require($module, $identifier = null)
{
if (is_null($identifier)) {
return $this->eval("requireDefault('{$module}')");
}

if (is_string($identifier)) {
return $this->eval("var $identifier = requireDefault('{$module}'); $identifier");
}

if (is_array($identifier) && ! empty($identifier)) {
if (is_array($identifier)) {
foreach ($identifier as $key => $value) {
$this->eval(sprintf('var %s = require(\'%s\').%s;',
$value, $module, is_string($key) ? $key : $value));
}

return $this->eval(sprintf('{%s}', implode(', ', $identifier)));
}

return $this->eval("requireDefault('{$module}')");
}

public function set($key, $value, $global = false)
Expand Down Expand Up @@ -107,4 +103,11 @@ public function getLoader()
{
return $this->loader;
}

public function setLoader(ModuleLoader $loader)
{
$this->loader = $loader;
$this->v8->setModuleNormaliser([$this->loader, 'normaliseIdentifier']);
$this->v8->setModuleLoader([$this->loader, 'loadModule']);
}
}

0 comments on commit ce2d324

Please sign in to comment.