Zend Diactoros

PSR-15 Zend Diactoros HTTP response emitter middleware for BitFrame microframework

Releases

{
    "require": {
		"designcise/bitframe-diactoros": "^1.0.0"
	}
}

Install

Add the following as a dependency in your project's composer.json:

"designcise/bitframe-diactoros": "^1.0.0"

Getting Started →

use \BitFrame\Message\DiactorosResponseEmitter;

require 'vendor/autoload.php';

$app = new \BitFrame\Application;

// instantiation with default options
$emitter = new DiactorosResponseEmitter;// lazy instantiation with default options
$emitter = DiactorosResponseEmitter::class;/* instantiate with custom options  */
$emitter = new DiactorosResponseEmitter($emitter);

$app->run([
    /*  */
    $emitter
]);

Getting Started

You can create a new instance of \BitFrame\Message\DiactorosResponseEmitter simply by using the new keyword.

You can also do lazy instantiation, if it's supported by your middleware dispatcher, by just providing the class name with its namespace, or simply by appending ::class to an object instance or class name.

You can specify the following argument when creating a new instance of the object:

  • emitter (EmitterInterface|null, optional): (default: \Zend\Diactoros\Response\SapiEmitter) If null, the default emitter is used. If using a custom emitter, it must implement \Zend\Diactoros\Response\EmitterInterface.

← Install Force Emit →

require 'vendor/autoload.php';

$app = new \BitFrame\Application;
$emitter = new \BitFrame\Message\DiactorosResponseEmitter;

$emitter->setForceEmit(false);

$app->run([
    function($request, $response, $next) {
        $response->getBody()->write('Run & Emit Response');
		
        return $response;
    }
]);

$app->run([
    $emitter,
    function($request, $response, $next) {
        // output has been emitted previously, so \RuntimeException is thrown
        $response->getBody()->write('Should result in error');

        return $response;
    }
]);

Force Emit

The normal behavior for Diactoros is that once the output is emitted (either when headers have already been sent or if output is present in the output buffer) an exception would be thrown. We can overcome this limitation by forcing emit which would output the response without sending any additional headers.

Setter & Getter
  • setForceEmit: This option is enabled by default and can be enabled/disabled by calling the setForceEmit method on the Diactoros object instance.
  • isForceEmit: You can use isForceEmit method to check what the value of force emit is for that specific object instance.

If force emit option is set to true and output is from the same \Bitframe\Application instance, then emitter should be used once only (either in first addMiddleware() call or last run([]) call) to avoid duplicates. To avoid this behavior, you can create two (or more) distinct application instances with their own respective http responses.

← Getting Started About →

/**
 * Zend Diactoros Wrapper (PSR-15 / PSR-7 compatible)
 *
 * @author    
 * @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com)
 *
 * @author    Zend Framework
 * @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc.
 *
 * @license   MIT License and New BSD License
 */

About

Zend Diactoros contains classes implementing the accepted PSR-7 HTTP message interfaces; among various uses, it helps emit the HTTP response.

Diactoros Official GitHub

← Force Emit API →

Comments

Let us know if you have something to say or add

Changelog

Latest version 1.0.0 released on Feb 14, 2018

Version 1.0.0

  • Force emit option added
  • PSR-15 & PSR-7 support
  • Support to specify emitter object
  • Added exceptions when not force emitting, and headers already sent or if output is present in the output buffer