<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
christianjbrown / cloud-run-function-lib example snippets
use ChristianBrown\CloudRunFunction\DataProviderInterface;
use Psr\Http\Message\ServerRequestInterface;
final class MyDataProvider implements DataProviderInterface
{
/**
* @return mixed[]
*/
public function getData(ServerRequestInterface $request): array
{
// Your business logic. Throw a UserFriendlyExceptionInterface to return
// a specific message to the client; any other Throwable is hidden unless DEBUG is on.
return ['hello' => 'world'];
}
}
use ChristianBrown\CloudRunFunction\CloudRunFunctionFactory;
$cloudFunction = (new CloudRunFunctionFactory())->createFromEnvironment(new MyDataProvider(), $_ENV);
$response = $cloudFunction->run($request); // ChristianBrown\CloudRunFunction\ResponseInterface (PSR-7)
// 1.x
$config = (new FunctionConfigTransformer())->transform($_ENV);
$cloudFunction = new CloudRunFunction($dataProvider, $config);
// 2.0
$cloudFunction = (new CloudRunFunctionFactory())->createFromEnvironment($dataProvider, $_ENV);
// 2.0, when you hold the config yourself
$factory = new CloudRunFunctionFactory();
$config = $factory->createConfigTransformer()->transform($_ENV);
$cloudFunction = $factory->create($dataProvider, $config);
// 1.x
$config = (new FunctionConfig('rev'))->setDebug(true)->setUseCacheTtl(60);
// 2.0: with-ers return a new instance, assign the result
$config = (new FunctionConfig('rev'))->withDebug(true)->withUseCacheTtl(60);
// 1.x
new JsonSuccessResponse($config, $data, 200, $origin);
new JsonErrorResponse($config, 'message', 500, $origin);
// 2.0
$responses = new JsonResponseFactory(new ResponseBodyBuilder(), new CorsHeaderBuilder(new AllowOriginResolver()), new CacheHeaderBuilder(), new NativeClock());
$responses->success($config, $data, 200, $origin);
$responses->error($config, 'message', 500, $origin);