PHP code example of issadicko / kodi-script
1. Go to this page and download the library: Download issadicko/kodi-script library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
issadicko / kodi-script example snippets
use KodiScript\KodiScript;
// Simple evaluation
$result = KodiScript::eval('2 + 3 * 4');
echo $result; // 14
// With variables
$result = KodiScript::run('greeting + ", " + name + "!"', [
'greeting' => 'Hello',
'name' => 'World',
]);
echo $result->value; // "Hello, World!"
// Capture output
$result = KodiScript::run('
let items = ["apple", "banana", "cherry"]
for (item in items) {
print(item)
}
');
print_r($result->output); // ["apple", "banana", "cherry"]
$result = KodiScript::builder('greet("PHP")')
->registerFunction('greet', fn($name) => "Hello, $name!")
->execute();
echo $result->value; // "Hello, PHP!"
class ScriptService
{
public function __construct(
private UserRepository $userRepo,
private NotificationService $notifications
) {}
public function execute(string $script, array $context): ScriptResult
{
return KodiScript::builder($script)
->withVariables($context)
->registerFunction('fetchUser', fn($id) => $this->userRepo->find($id))
->registerFunction('sendEmail', fn($to, $subject, $body) =>
$this->notifications->sendEmail($to, $subject, $body))
->registerFunction('calculatePrice', fn($qty, $price, $discount = 0) =>
$qty * $price * (1 - $discount / 100))
->execute();
}
}