PHP code example of ellinaut / ellirpc

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

    

ellinaut / ellirpc example snippets



$definitionLoader = new \Ellinaut\ElliRPC\Definition\Loader\ArrayDefinitionLoader();

$definitionLoader->registerPackage([
    'name' => 'Test',
    'description' => 'Test package to demonstrate configuration.',
    'procedures' => [],
    'schemas' => [],
    'errors' => []
]);


$processorRegistry = new \Ellinaut\ElliRPC\Procedure\Processor\ProcedureProcessorRegistry();

$processorRegistry->register(
    'Test', // package name
    'test', // procedure name
    new TestProcessor() // procedure processor for test procedure in Test package
);

$transactionManager = new \Ellinaut\ElliRPC\Procedure\Transaction\TransactionManager();

$transactionManager->registerListener(
    new CustomTransactionListener()
);

$procedureValidator = new \Ellinaut\ElliRPC\Procedure\Validator\ProcedureValidatorChain();

$procedureValidator->register(
    new CustomValidator()
);

// validator for specific context (standalone/no transaction in this example)
$procedureValidator->register(
    new CustomValidator(),
    \Ellinaut\ElliRPC\Procedure\Validator\ValidatorContext::STANDALONE
);

$errorFactory = new \Ellinaut\ElliRPC\Error\Factory\ErrorFactoryChain();

$errorFactory->register(
    new CustomErrorFactory()
);

$errorTranslator = new \Ellinaut\ElliRPC\Error\Translator\ErrorTranslatorChain();

$errorTranslator->register(
    new CustomErrorTranslator()
);

$contentTypeGuesser = new \Ellinaut\ElliRPC\File\Bridge\SymfonyContentTypeGuesser(
    new \Symfony\Component\Mime\FileinfoMimeTypeGuesser()
);

// with symfony filesystem and without custom fiel logic use: 
$fallbackFilesystem = new \Ellinaut\ElliRPC\File\Bridge\SymfonyFilesystem(
    new \Symfony\Component\Filesystem\Filesystem()
);
// to disable file uploads use `\Ellinaut\ElliRPC\File\UnsupportedFilesystem` instead!

$filesystem = new \Ellinaut\ElliRPC\File\FilesystemChain($fallbackFilesystem);
// use "filesystem->add" to add custom filesystems which have to implement `\Ellinaut\ElliRPC\File\ChainableFilesystem`

$fallbackFileLocator = new \Ellinaut\ElliRPC\File\LocalPathLocator(
    __DIR__.'/../files' // local file storage path
);
// to use the same values for public and storage paths and disable resolving use `\Ellinaut\ElliRPC\File\UnresolvedFileLocator` instead!

$fileLocator = new \Ellinaut\ElliRPC\File\FileLocatorChain($fallbackFileLocator);
// use "$fileLocator->add" to add custom file locators which have to implement `\Ellinaut\ElliRPC\File\ChainableFileLocator`

// initialize the factory (in this case with nyholm/psr7 installed)
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();

// create a request object which can be handled by the library
$nyholmCreator = new \Nyholm\Psr7Server\ServerRequestCreator(
    $psr17Factory, // ServerRequestFactory
    $psr17Factory, // UriFactory
    $psr17Factory, // UploadedFileFactory
    $psr17Factory  // StreamFactory
);

$request = $nyholmCreator->fromGlobals();            

if(substr($request->getUri()->getPath(), 0, 12) === '/definitions' && $request->getMethod() === 'GET'){            
    // creating the definition handler
    $definitionHandler = new \Ellinaut\ElliRPC\DefinitionHandler(
        $psr17Factory,
        $psr17Factory,
        $errorFactory,
        $errorTranslator,
        $definitionLoader,
        'Example Application', // application name
        'Example application contains a test package with test procedure' // description
    );
    
    if($request->getUri()->getPath() === ' /definitions'){
        $response = $definitionHandler->executeGetDocumentation($request);
    }
    
    if(substr($request->getUri()->getPath(), 0, 13) === '/definitions/'){
        $response = $definitionHandler->executeGetPackageDefinition($request);
    }
}

if(substr($request->getUri()->getPath(), 0, 12) === '/procedures/' && $request->getMethod() === 'POST'){
    // creating the rpc handler
    $rpcHandler=  new \Ellinaut\ElliRPC\RPCHandler(
        $psr17Factory,
        $psr17Factory,
        $errorFactory,
        $errorTranslator,
        $procedureValidator,
        $procedureProcessorRegistry,
        $transactionManager
    );
    
    if($request->getUri()->getPath() === ' /procedures/execute'){
        $response = $rpcHandler->executeExecuteProcedure($request);
    }
    
    if($request->getUri()->getPath() === ' /procedures/bulk'){
        $response = $rpcHandler->executeExecuteBulk($request);
    }
    
    if($request->getUri()->getPath() === ' /procedures/transaction'){
        $response = $rpcHandler->executeExecuteTransaction($request);
    }
}

if(substr($request->getUri()->getPath(), 0, 6) === '/files'){
    // creating the file handler
    $fileHandler = new \Ellinaut\ElliRPC\FileHandler(
        $psr17Factory,
        $psr17Factory,
        $errorFactory,
        $errorTranslator,
        $fileLocator,
        $filesystem,
        $contentTypeGuesser
    );

    switch ($request->getMethod()){
        case 'GET':
            $response = $fileHandler->executeGetFile($request);
            break;
        case 'POST':
        case 'PUT':
            $response = $fileHandler->executeUploadFile($request);
            break;
        case 'DELETE':
            $response = $fileHandler->executeDeleteFile($request);
            break;
    }
}

// your application has to send $response to client...