Awesome
soluble-japha-pjb62-compat
Introduction
*** Work in progress ***
Historically the PHP/Java bridge client didn't support namespaces.
Install this package if you have existing code relying on legacy php-java-bridge and don't want to refactor to the newer implementation provided by soluble/japha.
Features
- Make soluble-japha compatible with the procedural phpjavabridge legacy api
Requirements
- PHP 5.5+, 7.0 or HHVM >= 3.2.
- The PHPJavaBridge server running
Installation
-
PHP installation (client)
Through composer.
$ composer require "soluble/japha-pjb62-compat"
Most modern frameworks will include Composer out of the box, but ensure the following file is included:
<?php // include the Composer autoloader require 'vendor/autoload.php';
-
PHP-Java-bridge server
Refer to the latest documentation provided in the soluble-japha project.
Or as quick install guide use the standalone server :
$ mkdir -p /my/path/pjbserver-tools $ cd /my/path/pjbserver-tools $ composer create-project --no-dev --prefer-dist "belgattitude/pjbserver-tools" $ ./bin/pjbserver-tools pjbserver:start -vvv ./config/pjbserver.config.php.dist
The server will start on default port 8089. If you like to change it, create a local copy of
./config/pjbserver.config.php.dist
and refer it in the above command.Use the commands
pjbserver:stop
,pjbserver:restart
,pjbserver:status
to control or query the server status.For production the recommended way is to deploy the JavaBridge servlet into a J2EE compatible server (Tomcat,...). Have a look to the complete java server installation documentation.
Examples
Connection example
Configure your bridge adapter with the correct driver (currently only Pjb62 is supported) and the PHP-Java-bridge server address.
<?php
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
$ba = new BridgeAdapter([
'driver' => 'Pjb62',
'servlet_address' => 'localhost:8089/servlet.phpjavabridge'
]);
This replace the include('xxx/Java.inc)
used in previous versions.
Basic Java usage
With legacy mode enabled you can use the java* function directly
<?php
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
$ba = new BridgeAdapter([
'driver' => 'Pjb62',
'servlet_address' => 'localhost:8083/servlet.phpjavabridge'
]);
$bigint = new Java("java.math.BigInteger", 1);
$system = java_class('java.lang.System);
java_instanceof($bigint, 'java.math.BigInteger'); // -> true
java_inspect($bigint);
java_values($bigint);
//java_invoke();
API
Refactor constants
Constant | Example |
---|---|
JAVA_HOSTS | define("JAVA_HOSTS", "127.0.0.1:8787") |
JAVA_SERVLET | define("JAVA_SERVLET", "/MyWebApp/servlet.phpjavabridge") |
JAVA_PREFER_VALUES | define("JAVA_PREFER_VALUES", 1) |
JAVA_LOG_LEVEL | define("JAVA_LOG_LEVEL", null) |
JAVA_SEND_SIZE | define("JAVA_SEND_SIZE", 8192) |
JAVA_RECV_SIZE | define("JAVA_RECV_SIZE", 8192) |
JAVA_DISABLE_AUTOLOAD | Not applicable anymore - PSR4 ;) |
Initialization
Old way | New way |
---|---|
include(... /Java.inc) | $ba = new Bridge\Adapter($option); |
API
The following table maps old and new recommended API.
Legacy | Bridge\Adapter ($ba) |
---|---|
new Java($class, $args=null) : Java | $ba->java($class, $args=null) : Interfaces\JavaObject |
java_class($class) : JavaClass | $ba->javaClass($class) Interfaces\JavaClass |
java_instanceof($object, $class) : boolean | $ba->isInstanceOf($object, $class) : boolean |
(under review, soon to be implemented)
Legacy | Bridge\Adapter ($ba) |
---|---|
java_values($object) : mixed | $ba->getValues($object) : mixed |
java_invoke($object, $method, $args=null) : `mixed | null` |
java_inspect($object) : string | $ba->debug()->inspect($object) : string |
getLastException : Exception | $ba->debug()->getLastException() : Exception |
clearLastException | $ba->debug()->clearLastException() |
function java_is_null($value) function java_is_true($value) function java_is_false($value)
Refactoring guidelines
Keep a step by step approach... you can use both API at the same time.
- Try to change intialization sequence