PHP code example of guide42 / ochenta

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

    

guide42 / ochenta example snippets


use ochenta\ServerRequest;
use function ochenta\{emit, responder_of};

emit(new ServerRequest, responder_of('Hello World'));

$req = new ServerRequest;

$req = new ServerRequest($_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, fopen('php://input', 'rb'));

function hola(ServerRequest $req, callable $open) {
    $name = $req->getQuery()['name'] ?? 'World';
    $open(200, ['Content-Language' => ['en', 'es']]);
    yield "Hola $name";
}

function hola(ServerRequest $req, callable $open) {
    $name = $req->getQuery()['name'] ?? 'World';
    $res = new Response(200, ['Content-Language' => ['en', 'es']], "Hola $name");
    return responder_of($response)($req, $open);
}

emit(new ServerRequest, @hola);

function timeit(callable $handler): callable {
    return function(ServerRequest $req, callable $open) use($handler) {
        $time = -microtime(TRUE);
        $res = yield from $handler($req, $open);
        $time += microtime(TRUE);
        yield sprintf("<address>%.7F secs</address>", $time);
        return $res;
    };
}

$app = @hola;
$app = timeit($app);

emit(new ServerRequest, $app);

function add_header(string $name, string $value): callable {
    return function(callable $handler) use($name, $value): callable {
        return function(ServerRequest $req, callable $open) use($name, $value, $handler) {
            return $handler($req, function(int $status, array $headers) use($name, $value, $open) {
                $headers[$name] = [$value];
                $open($status, $headers);
            });
        };
    };
}

$app = add_header('X-Frame-Options', 'SAMEORIGIN')($app);

$app = stack(@hola, [
    add_header('X-Xss-Protection', '1; mode=block'),
    add_header('X-Frame-Options', 'SAMEORIGIN'),
    @timeit,
]);

responder_of(Response $resource)                             // creates a responder from a Response
responder_of(resource $resource)                             // ... from a resource
responder_of(scalar $resource)                               // ... from content

emit(ServerRequest $req, callable $handler)                  // emits a responder

stack(callable $responder, array $stack)                     // expects stack items to be a function(callable $next)
stack(callable $responder, callable $resolver, array $stack) // ... use resolver as function(callable $prev, $handler)

// MIDDLEWARES

header(string $name, array $values)                          // adds a header to responder
header(string $name, string $value)                          // ... with single value

cookie(Cookie $cookie)                                       // sets cookie into responder

append(string $content)                                      // adds content before body
append(string $content, string $tag)                         // ... before every given tag

// RESPONDERS

redirect(string $uri)                                        // redirect to the given url
redirect(string $uri, int $statusCode)                       // ... with given status code

// CONTENT NEGOTATION

accept\mediatypes(Request $req, array $available)            // negotiate media types
accept\charsets(Request $req, array $available)              // ... charsets
accept\encodings(Request $req, array $available)             // ... encodings
accept\languages(Request $req, array $available)             // ... languages

// HELPERS

stream_of(scalar $resource)                                  // creates tmp file with $resouce content