| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Iterator; |
| 7 |
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
|
| 10 |
class LuaDataProvider implements Iterator { |
| 11 |
|
| 12 |
protected $engine = null; |
| 13 |
|
| 14 |
protected $exports = null; |
| 15 |
|
| 16 |
protected $key = 1; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function __construct( $engine, $moduleName ) { |
| 23 |
$this->engine = $engine; |
| 24 |
$this->key = 1; |
| 25 |
$module = $engine->fetchModuleFromParser( |
| 26 |
Title::makeTitle( NS_MODULE, $moduleName ) |
| 27 |
); |
| 28 |
if ( $module === null ) { |
| 29 |
throw new Exception( "Failed to load module $moduleName" ); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
$this->exports = $engine->executeModule( $module->getInitChunk(), null, null ); |
| 35 |
} |
| 36 |
|
| 37 |
public function destroy() { |
| 38 |
$this->engine = null; |
| 39 |
$this->exports = null; |
| 40 |
} |
| 41 |
|
| 42 |
public function rewind(): void { |
| 43 |
$this->key = 1; |
| 44 |
} |
| 45 |
|
| 46 |
public function valid(): bool { |
| 47 |
return $this->key <= $this->exports['count']; |
| 48 |
} |
| 49 |
|
| 50 |
#[\ReturnTypeWillChange] |
| 51 |
public function key() { |
| 52 |
return $this->key; |
| 53 |
} |
| 54 |
|
| 55 |
public function next(): void { |
| 56 |
$this->key++; |
| 57 |
} |
| 58 |
|
| 59 |
#[\ReturnTypeWillChange] |
| 60 |
public function current() { |
| 61 |
return $this->engine->getInterpreter()->callFunction( $this->exports['provide'], $this->key ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function run( $key ) { |
| 69 |
[ $ret ] = $this->engine->getInterpreter()->callFunction( $this->exports['run'], $key ); |
| 70 |
return $ret; |
| 71 |
} |
| 72 |
} |