Handle Context
Table of contents
Allow the interaction with context
Besides the global php functions you can provide Context Variables as context to the engine. Therefore the end-user can code with the given variables. The main reason for this is providing context to teh script with service class instances or definition values like collections of items or access to the current stack and executing php code with much more functionality built-in and not accessible directly by the end-user.
For example think of having these classes in your application code:
class LoginStats
{
public int $countLogins = 42;
public function count(): int
{
return $this->countLogins;
}
public function increment(): void
{
$this->countLogins++;
}
}
class User
{
public string $name = 'Administrator';
public LoginStats $logins;
public function __construct()
{
$this->logins = new LoginStats;
}
public function login()
{
$this->logins->increment();
}
public function hasPermission(string $perm): bool
{
return $perm === 'admin';
}
}
Now you can provide variables as context to the engine:
use PhpScript\Core\Engine;
$engine = new Engine;
$engine->set('user', new \User, 'User instance')
->set('app_version', '1.0.0', 'Application version')
->set('users_list', ['Alice', 'Bob', 'Charlie'], 'List of users');
And then you can run the engine and execute a given script:
try {
$echoResult = $engine->execute($phpScript);
} catch (\PhpScript\Exceptions\EngineException $exception) {
// log or fetch the cause of the error
}