Skip to content

Commit 0d35e69

Browse files
committed
Defer requiring modules until class loading time
See xp-runners/reference#85
1 parent 8204b6b commit 0d35e69

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main/php/xp/runtime/Code.class.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ private function importsIn($use) {
146146
* @throws lang.Throwable
147147
*/
148148
public function run($argv= []) {
149-
$this->modules->require($this->namespace);
149+
150+
// Defer requiring modules until class loading time - if XP runners
151+
// have already done this for us, we'll not do this twice. See this
152+
// pull request: https://github.com/xp-runners/reference/pull/85
153+
spl_autoload_register($defer= function($class) use(&$defer) {
154+
$this->modules->require($this->namespace);
155+
156+
spl_autoload_unregister($defer);
157+
spl_autoload_call($class);
158+
});
150159

151160
Script::$code[$this->name]= '<?php '.$this->head().str_repeat("\n", $this->line).$this->fragment."\nreturn null;";
152161
try {
@@ -155,6 +164,7 @@ public function run($argv= []) {
155164
} catch (\Throwable $t) {
156165
throw Throwable::wrap($t);
157166
} finally {
167+
spl_autoload_unregister($defer);
158168
unset(Script::$code[$this->name]);
159169
}
160170
}

0 commit comments

Comments
 (0)