Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LuaDataProvider.php

raw symbols SCIP
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 /** @var LuaEngine|null */
12 protected $engine = null;
13 /** @var mixed|null */
14 protected $exports = null;
15 /** @var int */
16 protected $key = 1;
17
18 /**
19 * @param LuaEngine $engine
20 * @param string $moduleName
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 // Calling executeModule with null isn't the best idea, since it brings
32 // the whole export table into PHP and throws away metatables and such,
33 // but for this use case, we don't have anything like that to worry about
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 * @param string $key Test to run
66 * @return mixed Test result
67 */
68 public function run( $key ) {
69 [ $ret ] = $this->engine->getInterpreter()->callFunction( $this->exports['run'], $key );
70 return $ret;
71 }
72 }