PHP code example of timostamm / symfony-twirp-handler
1. Go to this page and download the library: Download timostamm/symfony-twirp-handler 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/ */
timostamm / symfony-twirp-handler example snippets
// SearchServiceController.php
/**
* @Route("/twirp/SearchService")
*/
class SearchServiceController
{
use TwirpControllerTrait;
/**
* @Route("/MakeHat")
*/
public function makeHat(Request $request): Response
{
/** @var SearchRequest $input */
$input = $this->readTwirp($request, SearchRequest::class);
// ...
$output = new SearchResponse();
// ...
return $this->writeTwirp($request, $output);
}
}
// SearchService.php
class SearchService implements SearchServiceInterface
{
public function search(SearchRequest $request)
{
$response = new SearchResponse();
$response->setHits(['a', 'b', 'c']);
return $response;
}
}
// TwirpController.php
/**
* @Route( path="twirp/{serviceName}/{methodName}" )
*/
public function execute(RequestInterface $request, string $serviceName, string $methodName): Response
{
$resolver = new ServiceResolver();
$resolver->registerInstance(
SearchServiceInterface::class, // the interface generated by protoc
new SearchService() // your implementation of the interface
);
// alternatively, you can register a factory
// $resolver->registerFactory(SearchServiceInterface::class, function() {
// return new SearchService();
// });
// .. or a PSR container with $resolver->registerContainer()
$handler = new TwirpHandler($resolver);
return $handler->handle($serviceName, $methodName, $request);
}