goplasmatic/datalogic — PHP binding for datalogic-rs
PHP FFI wrapper over the shared bindings/c C ABI. Requires
PHP 8.4+ with ext-ffi enabled (the floor tracks composer.json — the
test suite uses PHPUnit 13, which itself requires PHP 8.4+).
composer require goplasmatic/datalogicThe composer package ships platform binaries under lib/<os>-<arch>/;
the FFI loader (Goplasmatic\Datalogic\Internal\Native) picks the right
one at runtime.
use Goplasmatic\Datalogic\Engine;
$engine = new Engine();
echo $engine->apply('{"+":[1,2]}', '{}'); // "3"Reusing a compiled rule:
$engine = new Engine();
$rule = $engine->compile('{"var":"x"}');
foreach ([1, 2, 3] as $x) {
echo $rule->evaluate(json_encode(['x' => $x])), "\n";
}Hot-loop session (arena reuse):
$session = $engine->openSession();
foreach ($inputs as $data) {
$result = $session->evaluate($rule, $data);
}Traced evaluation:
$session = $engine->openTracedSession();
$run = $session->evaluate('{"+":[{"var":"x"},1]}', '{"x":41}');
echo $run->result; // 42
echo count($run->steps); // executed node countCustom operator:
$engine = Engine::builder()
->addOperator('double', function (string $argsJson): string {
$args = json_decode($argsJson, true);
return (string) ((int) $args[0] * 2);
})
->build();
echo $engine->apply('{"double":[21]}', '{}'); // "42"The Native loader searches for the cdylib in this order:
DATALOGIC_NATIVE_LIBenv var (absolute path).bindings/php/lib/<os>-<arch>/lib...— populated by the release workflow.bindings/c/target/release/lib...— for in-tree dev.- The OS's default loader paths (
LD_LIBRARY_PATH/DYLD_LIBRARY_PATH/PATH).
So a fresh clone needs the C ABI built once:
cd ../c && cargo build --release
cd ../php
composer install
vendor/bin/phpunit- PHP is single-threaded per request —
Engine,Rule,Session,TracedSessionare all safe in that model. - The native handles are released by PHP's destructor when the wrapper
object goes out of scope; explicit
close()is also available for early release. - Custom operators use PHP FFI's auto-coercion of PHP callables to C function pointers. The builder retains the callable for the engine's lifetime; releasing the engine releases the pin.
bindings/php/
├── composer.json
├── phpunit.xml
├── src/
│ ├── Engine.php
│ ├── EngineBuilder.php
│ ├── Rule.php
│ ├── Session.php
│ ├── TracedSession.php
│ ├── TracedRun.php
│ ├── Exception/
│ │ ├── DatalogicException.php
│ │ ├── ParseException.php
│ │ └── EvaluateException.php
│ └── Internal/
│ └── Native.php # FFI::cdef loader + signature list
├── lib/ # populated at release time
│ └── <os>-<arch>/...
└── tests/
└── EngineTest.php # PHPUnit