Last active
December 26, 2019 13:24
-
-
Save maulanasatyaadi/552fbd54ff7e23b3b8ffb6adf1562a6f to your computer and use it in GitHub Desktop.
This is a script that fuse a Phalcon and Swoole frameworks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Author: Maulana Satya Adi | |
* Email: maulanasatyaadi@gmail.com | |
* Description: | |
* This is a sample script of Phalcon framework that running on top of Swoole HTTP Server | |
* | |
* License: | |
* | |
* Copyright (c) 2019 Maulana Satya Adi. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms are permitted | |
* provided that the above copyright notice and this paragraph are | |
* duplicated in all such forms and that any documentation, | |
* advertising materials, and other materials related to such | |
* distribution and use acknowledge that the software was developed | |
* by the Maulana Satya Adi. The name of the | |
* Maulana Satya Adi may not be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
*/ | |
use Swoole\Http\Server; | |
use Phalcon\Mvc\Micro; | |
use Phalcon\Di\FactoryDefault; | |
// Load factory default dependencies | |
$di = new FactoryDefault(); | |
// Load application based on dependency injection | |
$app = new Micro($di); | |
// Set response while path not found | |
$app->notFound(function() { | |
return ['not found']; | |
}); | |
// Set response for path '/'. This is default response (like index.php) | |
$app->get('/', function() { | |
return ['Hello world!']; | |
}); | |
// Set the webserver IP and port | |
$server = new Server('0.0.0.0', 9501); | |
// Using HTTP2 protocol | |
$server->set( | |
[ | |
'open_http2_protocol' => true | |
]); | |
// Set callback on start event on Swoole HTTP server | |
$server->on('start', function($server) use ($app) { | |
// This will print on your console | |
echo 'Server has started!'; | |
}); | |
// Set callback on request event on Swoole HTTP server | |
$server->on('request', function($request, $response) use ($app) { | |
$response->header('Content-type', 'application/json'); | |
$app->header = $request->header; | |
// Convert Swoole HTTP request to regular PHP request | |
foreach ($request->server as $key => $value) { | |
$_SERVER[strtoupper($key)] = $value; | |
} | |
$_GET = $request->get; | |
$_POST = $request->post; | |
$_COOKIE = $request->cookie; | |
$_FILES = $request->files; | |
$_GET['_url'] = $request->server['request_uri']; | |
$_REQUEST = array_merge( | |
(array)$request->get, | |
(array)$request->post, | |
(array)$request->cookie | |
); | |
// Send response as JSON string | |
$response->end(json_encode(['message' => $app->handle()])); | |
}); | |
// Start Swoole HTTP server | |
$server->start(); | |
// If you any questions, let's join our group that discusses the great PHP framework! https://facebook.com/groups/491117421646930/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment