<?php
namespace Engine\GenDocument\TextProcessor; class DefaultTextHandler { private $variablesSpace = null; public $stats = []; protected $resets = 0; private $functionsClass = '\App\GenDocument\TextProcessor\DefaultFunctions'; public $lastCall = []; public $thisCall = []; public $firstCall = []; public $calls = []; function __construct(){ } function __reset(){ $this->firstCall = []; $this->lastCall = []; $this->thisCall = []; $this->calls = []; if(!$this->resets){ global $CFG; if($CFG['DTM']['textProcessor']['functionsClass']){ $this->functionsClass = $CFG['DTM']['textProcessor']['functionsClass']; } $this->resets++; } } function __customCall($input, $method, $params, $functionIndex){ if(!$this->resets){ $this->__reset(); } $processed = false; if(!$method){ return $input; } $this->thisCall = []; $this->thisCall['name'] = $method; $this->thisCall['functionIndex'] = $functionIndex; if(!$processed ){ if(!isset($input)){ $input = new $this->functionsClass($input); $this->thisCall['firstInChain'] = true; } if(is_callable([&$input, $method])){ foreach($params as $k=>$value){ if($this->variablesSpace->exists("{$value}")){ $params[$k] = $this->variablesSpace->getValue($value); } } $fctResult = call_user_func_array([&$input, $method], $params); $processed = true; $this->thisCall['isFunction'] = true; } } if(!$processed && $this->variablesSpace->exists($method)){ $fctResult= new $this->functionsClass($this->variablesSpace->getValue($method)); $processed = true; $this->thisCall['isVariable'] = true; } if(!$processed){ throw new Exception\InvalidFunction("Trying to call invalid function or variable : '{$method}'", $method, $params, $functionIndex); } $this->calls[] = $this->thisCall; $this->lastCall = $this->thisCall; if(!$this->firstCall){ $this->firstCall = $this->thisCall; } return $fctResult; } function loadVariables($variablesSpace){ $this->variablesSpace = $variablesSpace; } public function getVariables() { return $this->variablesSpace; } function getStats(){ return [ 'firstCall' => $this->firstCall, 'lastCall' => $this->lastCall, 'thisCall' => $this->thisCall, 'calls' => $this->calls, ]; } } 