1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-17 12:10:45 +02:00

Fix issue processwire/processwire-issues#60 where multi-language fields weren't working correctly with multi-instance

This commit is contained in:
Ryan Cramer
2016-10-31 09:30:26 -04:00
parent 0529bcc495
commit ce037f06da
3 changed files with 40 additions and 1 deletions

View File

@@ -504,6 +504,22 @@ class Modules extends WireArray {
$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());
} catch(\Exception $e) {
@@ -511,6 +527,7 @@ class Modules extends WireArray {
$module = null;
}
if($this->debug) $this->debugTimerStop($debugKey);
if($wire1) ProcessWire::setCurrentInstance($wire1);
return $module;
}

View File

@@ -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
*

View File

@@ -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;
}
/**