A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pachico/slim-swoole below:

pachico/slim-swoole: Convenient library to run SlimPHP applications with Swoole

This is a brige library to run Slim framework Slim framework applications using Swoole engine.

The main purpose of this library is to easily run your already existing SlimPHP applications using Swoole Framework. It requires you to bootstrap your application only once when you start Swoole HTTP server and, thanks to its event driven design, it will process each request reusing your already started application for better performance.

The execution sequence is as follows:

  1. You bootstrap your SlimPHP application as you would normally do.
  2. You instantiate the BrigeManager passing to it your SlimPHP application.
  3. You start Swoole's HTTP server.
  4. You bind to the on('request') event handler the BridgeManager instance which will:
    1. Transform the Swoole request to a SlimPHP based on server and request attributes.
    2. Process your request through SlimPHP's application stack (including middlewares)
    3. Merge SlimPHP Response to Swoole Response
    4. End the request. All this is done under the hood, so you will just need to call:
$bridgeManager->process($swooleRequest, $swooleResponse)->end();

(See usage paragraph for a complete example.)

Caution: it is still in development so any contribution and test will be more than welcome.

Via Composer

$ composer require pachico/slim-swoole
<?php

use Pachico\SlimSwoole\BridgeManager;
use Slim\Http;

require __DIR__ . '/../vendor/autoload.php';

/**
 * This is how you would normally bootstrap your Slim application
 * For the sake of demonstration, we also add a simple middleware
 * to check that the entire app stack is being setup and executed
 * properly.
 */
$app = new \Slim\App();
$app->any('/foo[/{myArg}]', function (Http\Request $request, Http\Response $response, array $args) {
    $data = [
        'args' => $args,
        'body' => (string) $request->getBody(),
        'parsedBody' => $request->getParsedBody(),
        'params' => $request->getParams(),
        'headers' => $request->getHeaders(),
        'uploadedFiles' => $request->getUploadedFiles()
    ];

    return $response->withJson($data);
})->add(function (Http\Request $request, Http\Response $response, callable $next) {

    $response->getBody()->write('BEFORE' . PHP_EOL);
    $response = $next($request, $response);
    $response->getBody()->write(PHP_EOL . 'AFTER');

    return $response;
});

/**
 * We instanciate the BridgeManager (this library)
 */
$bridgeManager = new BridgeManager($app);

/**
 * We start the Swoole server
 */
$http = new swoole_http_server("0.0.0.0", 8081);

/**
 * We register the on "start" event
 */
$http->on("start", function (\swoole_http_server $server) {
    echo sprintf('Swoole http server is started at http://%s:%s', $server->host, $server->port), PHP_EOL;
});

/**
 * We register the on "request event, which will use the BridgeManager to transform request, process it
 * as a Slim request and merge back the response
 *
 */
$http->on(
    "request",
    function (swoole_http_request $swooleRequest, swoole_http_response $swooleResponse) use ($bridgeManager) {
        $bridgeManager->process($swooleRequest, $swooleResponse)->end();
    }
);

$http->start();

Please see CHANGELOG for more information on what has changed recently.

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

If you discover any security related issues, please email pachicodev@gmail.com instead of using the issue tracker.

The MIT License (MIT). Please see License File for more information.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4