psx / v8
Helper library for the php-v8 extension
Installs: 10 945
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.0
- phpv8/php-v8-stubs: ^0.2.2
README
About
This is a small helper library which simplifies working with the PHP V8 extension (https://github.com/pinepain/php-v8). It adds small wrapper classes which help with the type juggling between PHP and the V8 environment.
Usage
<?php $script = <<<JS var message = 'Hello ' + console.foo; resp = { message: console.log(message), bar: function(data){ return 'Foo ' + data; }, foo: 'bar' }; JS; $environment = new \PSX\V8\Environment(); $environment->set('console', [ 'foo' => 'foo', 'log' => function($value){ return $value . ' and bar'; } ]); $environment->run($script); $resp = $environment->get('resp'); echo $resp->get('message') . "\n"; // Hello foo and bar echo $resp->get('bar')(['test']) . "\n"; // Foo test echo $resp->get('foo') . "\n"; // bar
Objects
It is also possible to expose complete PHP classes to the V8 engine. Therefor
the class has to implement the PSX\V8\ObjectInterface
interface. There is also
a PSX\V8\ReflectionObject
object which automatically exposes all public
properties and methods. The javascript defined above would also work with the
following environment:
<?php class Console { public $foo; public function __construct() { $this->foo = 'foo'; } public function log($value) { return $value . ' and bar'; } } $console = new Console(); $environment = new \PSX\V8\Environment(); $environment->set('console', new \PSX\V8\Object\ReflectionObject($console)); $environment->run($script); $resp = $environment->get('resp');