1. Go to this page and download the library: Download didix16/php-interpreter 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/ */
public class APIInterpreter extends Interpreter {
public function __construct(APIParser $parser)
{
parent::__construct($parser, null);
$this
// Allows make GET using an http client or whatever...
->loadFunction(new APIGetFunction(...))
// Allows to interact with DDBB and store values
->loadFunction(new DBFunction(...))
// ...whatever
}
protected function consume(): TokenInterface {
$token = parent::consume();
if(
$token->getValue() !== Lexer::EOF_VALUE &&
$token->getType() === YOUR_TYPE)
//do something
return $token;
}
/**
* Execute this interpreter using the parsed tokens
*/
public function run()
{
// All your tokens processing logic goes here
$token = $this->lookahead();
// do something with GET token and whitespace token, or whatever...
do {
//...
}while(($token = $this->lookahead()));
}
// Write your auxiliar functions here
/**
* this one could parse each starting command line:
* GET, PIPETO, SAVEDB, etc...
*/
public function processCommand()
{
...
}
/**
* This one could resolve some service from PIPETO command...
*/
public function processService()
{
...
}
}
composer
// APILexer.php
use didix16\Grammar\Lexer;
use didix16\Grammar\Token;
use didix16\Interpreter\Interpreter;
// Imported an APIParser
use ...\APIParser;
public class APIGetFunction extends InterpreterFunction {
public function __construct()
{
parent::__construct("GET");
}
public function run(...$args)
{
/**
* $url = $args[0];
* return request()->get($url)
* or whatever
*/
}
}
public class DBFunction extends InterpreterFunction {
public function __construct()
{
parent::__construct("SAVEDB");
}
public function run(...$args)
{
/**
* Get the data to be stored to DDBB.
* Preprocess the data if you need it
*
* return DDBB::connection($args[0])->store($data)
*/
}
}
public class APIInterpreter extends Interpreter {
/**
* Hold API response result, for example or result from executed function
*/
private $currentResult;
public function __construct(APIParser $parser)
{
parent::__construct($parser, null);
$this
// Allows make GET using an http client or whatever...
->loadFunction(new APIGetFunction(...))
// Allows to interact with DDBB and store values
->loadFunction(new DBFunction(...))
// ...whatever
}
/**
* Execute this interpreter using the parsed tokens
*/
public function run()
{
// All your tokens processing logic goes here
$token = $this->lookahead();
do {
switch($token->getType()){
case APILexer::T_ACTION:
$this->processCommand();
break;
default:
// ignore whitespaces and newlines
// arguments should never be at start of a line
$this->consume();
}
}while(($token = $this->lookahead()));
// Returns something meaningful to understand that your interpreter has been finished ok
// Or return some data if needs to
}
// Write your auxiliar functions here
/**
* this one could parse each starting command line:
* GET, PIPETO, SAVEDB, etc...
*/
public function processCommand()
{
$command = $this->consume()->getValue();
$args = [];
while($this->lookahead()->getType() !== APILexer::T_NEWLINE)
{
if($this->lookahead()->getType() === APILexer::T_WHITESPACE)
$this->consume(); // ' '
else
$args[] = $this->processCommandArg();
}
$this->consume(); // consume new line
$this->currentResult = $this->executeFunction($command, $args);
}
public function processCommandArg()
{
$value = $this->consume()->getValue();
// make some processing of the argument if you need...
//transform($value);
/**
* if $value === $mySuperService then
* import superService or whatever
*/
return $value;
}
/**
* This one could resolve some service from PIPETO command...
*/
public function processService()
{
...
// call the imported service and do something with previous result
// Note that services array must be defined
$this->services[$mySuperService]->doSomething($this->currentResult);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.