PHP code example of tobento / service-responser

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

    

tobento / service-responser example snippets


use Tobento\Service\Responser\Responser;
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Responser\RendererInterface;
use Tobento\Service\Responser\StorageInterface;
use Tobento\Service\Message\MessagesInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();

$responser = new Responser(
    responseFactory: $psr17Factory, // Any PSR-17 ResponseFactoryInterface
    streamFactory: $psr17Factory, // Any PSR-17 StreamFactoryInterface
    renderer: null, // null|RendererInterface
    storage: null, // null|StorageInterface
    messages: null, // null|MessagesInterface
);

var_dump($responser instanceof ResponserInterface);
// bool(true)

use Tobento\Service\Responser\RendererInterface;
use Tobento\Service\Responser\ViewRenderer;
use Tobento\Service\View\View;
use Tobento\Service\View\PhpRenderer;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$view = new View(
    new PhpRenderer(
        new Dirs(
            new Dir('/private/views/'),
        )
    )
);

$renderer = new ViewRenderer($view);

var_dump($renderer instanceof RendererInterface);
// bool(true)

use Tobento\Service\Responser\StorageInterface;
use Tobento\Service\Responser\SessionStorage;
use Tobento\Service\Session\Session;

$session = new Session('name');

$storage = new SessionStorage($session);

var_dump($storage instanceof StorageInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;

$response = $responser->html(
    html: 'html',
    code: 200, // is default
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;

$response = $responser->json(
    data: ['key' => 'value'],
    code: 200, // is default
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;

$response = $responser->render(
    view: 'shop/products',
    data: ['products' => []],
    code: 200, // is default
    contentType: 'text/html; charset=utf-8', // is default
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;

$response = $responser->write(
    data: 'data', // mixed
    code: 200, // is default
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Stringable;

$response = $responser->redirect(
    uri: 'uri', // string|Stringable|UriInterface
    code: 302, // is default
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Tobento\Service\Message\MessagesInterface;

var_dump($responser->messages() instanceof MessagesInterface);
// bool(true)

$responser->messages()->add('success', 'Success message');

$response = $responser->render(
    view: 'shop/products',
    data: [
        'products' => [],
        'messages' => $responser->messages(),
    ],
);

 foreach($messages as $message) { 

$responser->messages()->add('error', 'Error message');

$response = $responser->redirect('uri');

$response = $responser->render(
    view: 'shop/products',
    data: [
        'products' => [],
        'messages' => $responser->messages(),
    ],
);

$response = $responser
    ->withInput(['key' => 'value'])
    ->redirect('uri');

$input = $responser->getInput();

use Psr\Http\Message\ResponseInterface;

$response = $responser->create(200);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\StreamFactoryInterface;

$streamFactory = $responser->streamFactory();

var_dump($streamFactory instanceof StreamFactoryInterface);
// bool(true)

use Tobento\Service\Responser\FileResponserInterface;

$file = $responser->file();

var_dump($file instanceof FileResponserInterface);
// bool(true)

use Tobento\Service\Responser\ResponseInfo;

$info = $responser->info($responser->create(403));

var_dump($info instanceof ResponseInfo);
// bool(true)

use Tobento\Service\Responser\FileResponser;
use Tobento\Service\Responser\FileResponserInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();

$fileResponser = new FileResponser(
    responseFactory: $psr17Factory, // Any PSR-17 ResponseFactoryInterface
    streamFactory: $psr17Factory, // Any PSR-17 StreamFactoryInterface
);

var_dump($fileResponser instanceof FileResponserInterface);
// bool(true)

use Tobento\Service\Filesystem\File;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;

$response = $fileResponser->render(
    file: 'file.jpg', // string|File|StreamInterface|resource
    name: 'File', // string
    contentType: 'image/jpeg', // null|string
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Tobento\Service\Filesystem\File;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;

$response = $fileResponser->download(
    file: 'file.jpg' // string|File|StreamInterface|resource
    name: 'File', // string
    contentType: 'image/jpeg', // null|string    
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\ResponseInterface;

$response = $fileResponser->create(200);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Psr\Http\Message\StreamFactoryInterface;

$streamFactory = $fileResponser->streamFactory();

var_dump($streamFactory instanceof StreamFactoryInterface);
// bool(true)

use Tobento\Service\Responser\ResponseInfo;

$info = $fileResponser->info($fileResponser->create(403));

var_dump($info instanceof ResponseInfo);
// bool(true)

use Tobento\Service\Responser\ResponseInfo;
use Psr\Http\Message\ResponseInterface;

$responseInfo = new ResponseInfo(
    response: $response // ResponseInterface
);

var_dump($responseInfo->isInformational());
// bool(true)

var_dump($responseInfo->isSuccessful());
// bool(true)

var_dump($responseInfo->isRedirection());
// bool(true)

var_dump($responseInfo->isClientError());
// bool(true)

var_dump($responseInfo->isServerError());
// bool(true)

var_dump($responseInfo->isOk());
// bool(true)

var_dump($responseInfo->isForbidden());
// bool(true)

var_dump($responseInfo->isNotFound());
// bool(true)

var_dump($responseInfo->isCode(403, 404));
// bool(true)

use Tobento\Service\Responser\Responser;
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Responser\Middleware;
use Tobento\Service\Middleware\MiddlewareDispatcher;
use Tobento\Service\Middleware\AutowiringMiddlewareFactory;
use Tobento\Service\Middleware\FallbackHandler;
use Tobento\Service\Container\Container;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();

// create middleware dispatcher.
$dispatcher = new MiddlewareDispatcher(
    new FallbackHandler($psr17Factory->createResponse(404)),
    new AutowiringMiddlewareFactory(new Container()) // any PSR-11 container
);

$dispatcher->add(new Middleware\Responser(
    new Responser($psr17Factory, $psr17Factory)
));

$dispatcher->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
    
    $responser = $request->getAttribute(ResponserInterface::class);
    
    var_dump($responser instanceof ResponserInterface);
    // bool(true)
    
    return $handler->handle($request);
});

$request = $psr17Factory->createServerRequest('GET', 'https://example.com');

$response = $dispatcher->handle($request);

use Tobento\Service\Responser\Responser;
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Responser\Middleware;
use Tobento\Service\Middleware\MiddlewareDispatcher;
use Tobento\Service\Middleware\AutowiringMiddlewareFactory;
use Tobento\Service\Middleware\FallbackHandler;
use Tobento\Service\Container\Container;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$psr17Factory = new Psr17Factory();

$container = new Container();
$container->set(ResponserInterface::class, Responser::class)->construct($psr17Factory, $psr17Factory);

// create middleware dispatcher.
$dispatcher = new MiddlewareDispatcher(
    new FallbackHandler($psr17Factory->createResponse(404)),
    new AutowiringMiddlewareFactory($container) // any PSR-11 container
);

// Simulating Previous request
$dispatcher->add(Middleware\Responser::class);

$dispatcher->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
    
    $responser = $request->getAttribute(ResponserInterface::class);
    
    $responser->withInput(['key' => 'value']);
    
    return $handler->handle($request);
});

// Current request
$dispatcher->add(Middleware\ResponserMergeInput::class);

$dispatcher->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
    
    var_dump($request->getQueryParams()['key']);
    // string(5) "value"
    
    return $handler->handle($request);
});

$request = $psr17Factory->createServerRequest('GET', 'https://example.com');

$response = $dispatcher->handle($request);