PHP code example of maplephp / emitron

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

    

maplephp / emitron example snippets


use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use MaplePHP\Emitron\RequestHandler;
use MaplePHP\Emitron\Emitters\HttpEmitter;
use MaplePHP\Http\Environment;
use MaplePHP\Http\ServerRequest;
use MaplePHP\Http\Uri;
use MaplePHP\Emitron\Middlewares\{
    ContentLengthMiddleware,
    GzipMiddleware,
    HeadRequestMiddleware
};
use App\Controllers\MyController;

// Use MaplePHP HTTP library or any other PSR-7 implementation
$env = new Environment();
$request = new ServerRequest(new Uri($env->getUriParts($parts)), $env);

// Add your middlewares — Emitron ships with several ready-to-use
$middlewares = [
    new OutputBufferMiddleware(),   // Inject ob_get_clean() into stream
    new GzipMiddleware(),           // Compress body, update Content-Encoding
    new ContentLengthMiddleware(),  // Recalculate body size
    new HeadRequestMiddleware(),    // Optionally blank body
    new EmitterMiddleware(),        // Emit headers + body
];

// Run the middleware stack


$factory = new ResponseFactory($bodyStream);
// $finalHandler = new ControllerRequestHandler($factory, [MyController::class, "index"]);
$finalHandler = new ControllerRequestHandler($factory, function(RequestInterface $request, ResponseInterface $response) {
    $response->getBody()->write("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
    return $response;
});

$handler = new RequestHandler($middlewares, $finalHandler);
$response = $handler->handle($request);

// Emit the execute headers and response correctly 
//$emit = new CliEmitter($response, $request);
$emit = new HttpEmitter();
$emit->emit($response, $request);


use MaplePHP\Emitron\Kernel;use MaplePHP\Http\Environment;use MaplePHP\Http\ServerRequest;use MaplePHP\Http\Uri;

$env = new Environment();
$request = new ServerRequest(new Uri($env->getUriParts($parts)), $env);

$kernel = new Kernel(new Container(), [
    new OutputBufferMiddleware(),
    new GzipMiddleware(),
    new ContentLengthMiddleware(),
    new HeadRequestMiddleware(),
    new EmitterMiddleware()
]);

$kernel->run($request);

$kernel = new Kernel(
    Psr\Container\ContainerInterface, 
    [Psr\Http\Server\MiddlewareInterface::class], 
    ?MaplePHP\Emitron\Contracts\DispatchConfigInterface
);

use MaplePHP\Emitron\DispatchConfig;use MaplePHP\Emitron\Kernel;

$config = new DispatchConfig(__DIR__ . '/config/app.php');

$kernel = new Kernel(new Container(), [
    new ContentLengthMiddleware(),
    new HeadRequestMiddleware(),
], $config);
// You can also return the path in you app if you want with:
// $configPath = Kernel::getConfigFilePath();



return [
    'app_name' => 'MyApp',
    'debug' => true,
    'timezone' => 'Europe/Stockholm',
];