Home

Awesome

soluble-japha-pjb62-compat

PHP Version Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

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

Requirements

Installation

  1. 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';
    
  2. 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

ConstantExample
JAVA_HOSTSdefine("JAVA_HOSTS", "127.0.0.1:8787")
JAVA_SERVLETdefine("JAVA_SERVLET", "/MyWebApp/servlet.phpjavabridge")
JAVA_PREFER_VALUESdefine("JAVA_PREFER_VALUES", 1)
JAVA_LOG_LEVELdefine("JAVA_LOG_LEVEL", null)
JAVA_SEND_SIZEdefine("JAVA_SEND_SIZE", 8192)
JAVA_RECV_SIZEdefine("JAVA_RECV_SIZE", 8192)
JAVA_DISABLE_AUTOLOADNot applicable anymore - PSR4 ;)

Initialization

Old wayNew way
include(... /Java.inc)$ba = new Bridge\Adapter($option);

API

The following table maps old and new recommended API.

LegacyBridge\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)

LegacyBridge\Adapter ($ba)
java_values($object) : mixed$ba->getValues($object) : mixed
java_invoke($object, $method, $args=null) : `mixednull`
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.

  1. Try to change intialization sequence

Coding standards