PHP code example of rds / jrf

1. Go to this page and download the library: Download rds/jrf 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/ */

    

rds / jrf example snippets



use \jrf\JRF;
$service = new JRF();

$service->controller()->handleMethod('method',
    function($param_1, $param_2, JRF $app, $request_id) {
        return $param_1 + $param_2
    });

$service->listen();


use jrf\http\Input;

class MyInput extends Input
{
    private $input;

    public function __construct($input)
    {
        $this->input = $input;
    }

    public function read()
    {
        return $this->input;
    }
}


// ..... service init
use MyInput;
$input = new MyInput($_POST['json']);
$service->request()->setInputProvider($input);


use \jrf\middleware\Base;
use \jrf\fault\ServerError;

class AccessMiddleware extends Base
{
	private $ip_list=[];
	public function __construct(array $ip_list) {
		$this->ip_list = $ip_list;
	}

	public function call()
	{
		$info = $this->app->request()->info();
		$ip   = $info['ip_address'];

		if (!in_array($ip, $this->ip_list)) {
		    ServerError::raise('access error');
		}

		$this->next->call();
	}
}


// ... service init
use AccessMiddleware;

$list_allowed_ip = ['127.0.0.1'];

$service->add(new AccessMiddleware($list_allowed_ip));


use jrf\controller\Base;
use jrf\fault\MethodNotFound;

class MyControllerFactory extends Base
{
    public function runMethodWithArgs($method, array $args =[], $request_id=0)
    {
        // fall-back if want to use closure-based methods
        if ($this->isHandledMethod($method)) {
            return parent::runMethodWithArgs($method, $args, $request_id);
        }

        // now try to create action class
        if (strpos('.', $method)===false) {
            MethodNotFound::raise();
        }

        list($group, $action) = explode('.', $method);

        $class_name = sprint_f("\my\action\namespace\%s\%s", strtolower($group), ucfirst($action));

        if (!class_exists($class_name)) {
            MethodNotFound::raise();
        }

        $action = new $class_name($this->app());

        if (!is_callable([$action, 'run'])) {
            MethodNotFound::raise();
        }

        return $action->run();
    }
}


// .... service init
use MyControllerFactory;
$factory = new MyControllerFactory;
$service->controller($factory);


use jrf\json\Schema;

$params_definition = [
    'a' => [
        'type' => Schema::TYPE_INT,
        'definition' => Schema::DEFINITION_REQUIRED,
        'expression' => function ($v) { return intval($v) > 0; }
    ],
    'b' => [
        'type' => Schema::TYPE_INT,
        'definition' => Schema::DEFINITION_REQUIRED,
        'expression' => function ($v) { return intval($v) > 0; }
    ],
];

$schema = new Schema($params_definition);
$service->controller()->setSchemaForMethod('test', $schema);


use jrf\JRF;

$options = [
    'app.debug' => true,
    'app.secret' => 'secret'
];

$service = new JRF($options);

// return: null
$val = $service->config('unknown-option');

// return default defined value: 123
$val = $service->config('unknown-option', 123);

// return true
$val = $service->config('app.debug');
$val = $service->config('app.debug', false);
$val = $service->config()->get('app.debug');
$val = $service->config()->{'app.debug'};
$val = $service->config()['app.debug'];
 
$client = new Client("http://json-rpc.server/");

$pipe = $client->createPipe();

$batch = $client->createBatch();
$m1_id = $batch->append($client->method('m1', [1,2,3]));
$m2_id = $batch->append($client->method('m2'));
$m3_id = $batch->append($client->method('m3'));

$pipe->add('batch', $batch);
$pipe->add('news', $client->method('news.search', ['limit'=>50]));
$pipe->add('users', $client->method('users.all', ['limit'=>10]));

$response = $client->executePipe($pipe);

$m1 = $response['batch'][$m2_id];

var_dump($m1->getResult());
var_dump($client->getTotalTime());


// input interface from example
use MyInput;

$payload = ["jsonrpc"=>"2.0", "id"=>1, "method"=>"my.method"];
$input   = new MyInput(json_encode($payload));

// ... defining service

$result_string = $service->handleInputProvider($input);
$result_array  = json_decode($result_string, true);

// if HTTP listener needed
$service->listen();