1<?php 2 3namespace TableRoller\Table; 4 5class Manager 6{ 7 static private $embedCalls = 0; 8 9 public function __construct( 10 private \helper_plugin_sqlite $db 11 ){} 12 13 private static array $tables; 14 15 public function rollOn(string $name): string 16 { 17 $table = $this->getTable($name); 18 $result = $table->rollOnce(); 19 return preg_replace_callback('/{{roll on:\s*([A-Za-z0-9_]+)\s*}}/i', [$this, 'embedResult'], $result); 20 } 21 22 private function embedResult($matches): string 23 { 24 self::$embedCalls++; 25 if (self::$embedCalls < 20) { 26 $table = $this->getTable($matches[1]); 27 $result = $table->rollOnce(); 28 return preg_replace_callback('/{{roll on:\s*([A-Za-z0-9_]+)\s*}}/i', [$this, 'embedResult'], $result); 29 } 30 31 return "Too many recursive calls"; 32 } 33 34 private function getTable(string $name): TableInterface 35 { 36 if (!isset(self::$tables[$name])) { 37 $result = $this->db->query('SELECT * FROM rtables WHERE id=? LIMIT 1', $name); 38 $row = $this->db->res_fetch_assoc($result); 39 if ($row['rows']) { 40 self::$tables[$name] = new DokuwikiJsonTable($row['rows']); 41 } else { 42 self::$tables[$name] = new BaseTable(); 43 } 44 } 45 46 return self::$tables[$name]; 47 } 48} 49