diff --git a/wire/core/Modules.php b/wire/core/Modules.php index 99ea3d7f..18d9dd07 100644 --- a/wire/core/Modules.php +++ b/wire/core/Modules.php @@ -503,6 +503,22 @@ class Modules extends WireArray { // attempt 2.x module in dedicated namespace or root namespace $className = $this->getModuleNamespace($moduleName) . $moduleName; } + + if(ProcessWire::getNumInstances() > 1) { + // in a multi-instance environment, ensures that anything happening during + // the module __construct is using the right instance. necessary because the + // construct method runs before the wire instance is set to the module + $wire1 = ProcessWire::getCurrentInstance(); + $wire2 = $this->wire(); + if($wire1 !== $wire2) { + ProcessWire::setCurrentInstance($wire2); + } else { + $wire1 = null; + } + } else { + $wire1 = null; + $wire2 = null; + } try { $module = $this->wire(new $className()); @@ -511,6 +527,7 @@ class Modules extends WireArray { $module = null; } if($this->debug) $this->debugTimerStop($debugKey); + if($wire1) ProcessWire::setCurrentInstance($wire1); return $module; } diff --git a/wire/core/ProcessWire.php b/wire/core/ProcessWire.php index 27022290..12d019df 100644 --- a/wire/core/ProcessWire.php +++ b/wire/core/ProcessWire.php @@ -560,6 +560,16 @@ class ProcessWire extends Wire { return self::$instances; } + /** + * Return number of instances + * + * @return int + * + */ + public static function getNumInstances() { + return count(self::$instances); + } + /** * Get a ProcessWire instance by ID * diff --git a/wire/core/WireFileTools.php b/wire/core/WireFileTools.php index f7a3ecb0..72f235bc 100644 --- a/wire/core/WireFileTools.php +++ b/wire/core/WireFileTools.php @@ -765,8 +765,20 @@ class WireFileTools extends Wire { * */ public function compile($file, array $options = array()) { + static $compiled = array(); + if(strpos($file, '/modules/')) { + // for multi-instance support, use the same compiled version + // otherwise, require_once() statements in a file may not work as intended + // applied just to site/modules for the moment, but may need to do site/templates too + $f = str_replace($this->wire('config')->paths->root, '', $file); + if(isset($compiled[$f])) return $compiled[$f]; + } else { + $f = ''; + } $compiler = new FileCompiler(dirname($file), $options); - return $compiler->compile(basename($file)); + $compiledFile = $compiler->compile(basename($file)); + if($f) $compiled[$f] = $compiledFile; + return $compiledFile; } /**