Giter Club home page Giter Club logo

php-openwhisk-runtime's Introduction

OpenWhisk runtime tools for PHP

This library provide tools for more clean handling responses for PHP apps running inside OpenWhisk runtime.

In raw OpenWhisk action you need to return clearly defined Array - that's missing type control and it's liitle dirty.

This library provides tools to build responses for most of usually cases (HTTP, HTML, JSON responses, etc.).

Install

composer require jakubboucek/openwhisk-runtime

Usage

Sending OpenWhisk runtime's response without this library

<?php
function main(array $args) : array
{
    $name = $args['name'] ?? 'stranger';
    $greeting = "Hello $name!";
    return ['body' => $greeting, 'headers' => ['Content-Type' => 'text/html; charset=utf-8']];
}

Raw Response (just only for back compatibility with OpenWhisk's raw)

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
function main(array $args) : array
{
    $name = $args['name'] ?? 'stranger';
    $greeting = "Hello $name!";
    return new Response\RawResponse(['body' => $greeting, 'headers' => ['Content-Type' => 'text/html; charset=utf-8']]);
}

Raw JSON Response (just only for back compatibility with OpenWhisk's raw)

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
function main(array $args) : array
{
    $name = $args['name'] ?? 'stranger';
    $greeting = "Hello $name!";
    return new Response\RawJsonResponse(['message' => $greeting]);
}

HTTP Response

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
function main(array $args) : array
{
    if (!isset($args['name'])) {
        return (new Response\HttpResponse("Error: Missing required field 'name'."))
            ->setStatusCode(400)
            ->setContentType(Response\HttpHeader::PlainContentType);
    }

    $greeting = "Hello {$args['name']}!";
    return (new Response\HttpResponse($greeting))
        ->setContentType(Response\HttpHeader::PlainContentType);
}

HTML Response

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
function main(array $args) : array
{
    $name = $args['name'] ?? 'stranger';
    $greeting = sprintf('Hello <strong>%s</strong>!', htmlspecialchars($name));
    return new Response\HtmlResponse($greeting);
}

JSON Response

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
function main(array $args) : array
{
    if (!isset($args['name'])) {
        return (new Response\JsonResponse(['error' =>"Error: Missing required field 'name'."))
            ->setStatusCode(400);
    }

    $greeting = "Hello {$args['name']}!";
    return new Response\JsonResponse(['message' => $greeting]);
}

Catching standard Output Buffer Response

<?php
use JakubBoucek\OpenWhisk\Runtime\Response;
use JakubBoucek\OpenWhisk\Runtime\Source;
function main(array $args) : array
{
    $response = new Response\DynamicResponse(new Source\OutputBuffer());   

    echo 'Dumping $args variable:' . "\n";
    var_dump($args);   
   
    return $response;
}

php-openwhisk-runtime's People

Contributors

jakubboucek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.