Skip to content

Instantly share code, notes, and snippets.

@AdilHoumadi
Created September 28, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdilHoumadi/fbf28fe1361c4afc63564f16cdcc3220 to your computer and use it in GitHub Desktop.
Save AdilHoumadi/fbf28fe1361c4afc63564f16cdcc3220 to your computer and use it in GitHub Desktop.
BugCache
<?php
namespace Common\Application\Server\Bootstrap;
use Common\Application\Server\Mvc\Application;
use PHPPM\Bootstraps\BootstrapInterface;
use Zend\Stdlib\ArrayUtils;
class ApiAdapter implements BootstrapInterface
{
/**
* Create middleware router
*/
public function getApplication()
{
$appConfig = include './config/application.config.php';
if (file_exists( './config/development.config.php')) {
$appConfig = ArrayUtils::merge($appConfig, include './config/development.config.php');
}
return Application::init($appConfig);
}
/**
* Static directory - only used if frontend served via ppm httpd
*/
public function getStaticDirectory()
{
return './public';
}
}
<?php
namespace Common\Application\Server\Bridge;
use Common\Application\Server\Bootstrap\ApiAdapter;
use Common\Application\Server\Mvc\Application;
use Common\Application\Server\Mvc\Request;
use Common\Application\Server\Mvc\Response;
use PHPPM\Bridges\BridgeInterface;
use PHPPM\React\HttpResponse;
use React\EventLoop\LoopInterface;
use React\Http\Request as ReactRequest;
class ApiBridge implements BridgeInterface
{
/**
* @var $application Application
*/
protected $application;
/**
* @var $request Request
*/
protected $request;
/**
* @var $response Response
*/
protected $response;
/**
* @param string $appBootstrap
* @param string $appEnv
*/
public function bootstrap($appBootstrap, $appEnv, $debug, LoopInterface $loop)
{
/* @var $bootstrap ApiAdapter */
$bootstrap = new ApiAdapter($appEnv);
$this->application = $bootstrap->getApplication();
}
/**
* Returns the repository which is used as root for the static file serving.
*
* @return string
*/
public function getStaticDirectory()
{
return APPLICATION_PATH . '/public';
}
/**
* Handle a request using Zend\Mvc\Application.
*
* @param ReactRequest $request
* @param HttpResponse $response
*/
public function onRequest(ReactRequest $request, HttpResponse $response)
{
$app = $this->application;
if (null === ($app)) {
return;
}
/* @var $sm \Zend\ServiceManager\ServiceManager */
$sm = $app->getServiceManager();
$this->request = new Request();
$this->request->setContent($request->getBody());
$this->request->setReactRequest($request);
$this->response = new Response();
$this->response->setReactResponse($response);
// set to sm manager
$sm->setAllowOverride(true);
$sm->setService('Request', $this->request);
$sm->setService('Response', $this->response);
$sm->setAllowOverride(false);
$event = $app->getMvcEvent();
$event->setError(null);
$event->setRequest($this->request);
$event->setResponse($this->response);
// start buffering the output, so cgi is not sending any http headers
// this is necessary because it would break session handling since
// headers_sent() returns true if any unbuffered output reaches cgi stdout.
ob_start();
try {
$app->run($this->request, $this->response);
} catch (\Exception $exception) {
$response->writeHead(500); // internal server error
$response->end();
// end buffering if we need to throw
@ob_end_clean();
return;
}
@ob_end_clean();
/** @var Response $zendResponse */
$zendResponse = $event->getResponse();
$zendResponse->send();
$event->stopPropagation();
$response->writeHead($zendResponse->getStatusCode(), $zendResponse->getHeaders()->toArray());
$response->write($zendResponse->getContent());
$response->end();
}
}
<?php
namespace Common\Application\Server\Mvc;
use Zend\ServiceManager\ServiceManager;
class Application extends \ZF\Apigility\Application
{
public function __construct(ServiceManager $serviceManager)
{
parent::__construct(
$serviceManager,
null,
new Request(),
new Response()
);
}
/**
* @param \Zend\Http\PhpEnvironment\Request $request
* @param \Zend\Http\PhpEnvironment\Response $response
*/
public function run()
{
$this->request = func_get_arg(0);
$this->response = func_get_arg(1);
return parent::run();
}
}
root@php:/var/www/data# curl localhost:8080/api/common/time
{"rand":74,"current_time":1506585617}
root@php:/var/www/data# curl localhost:8080/api/common/time
{"rand":74,"current_time":1506585617}
root@php:/var/www/data# curl localhost:8080/api/common/time
{"rand":74,"current_time":1506585617}
root@php:/var/www/data# curl localhost:8080/api/common/get-languages
{"rand":74,"current_time":1506585617}
<?php
namespace Common\Application\Server\Mvc;
use \Zend\Http\PhpEnvironment\Request as ZendRequest;
use \React\Http\Request as ReactRequest;
use Zend\Stdlib\Parameters;
class Request extends ZendRequest
{
/**
* @var ReactRequest
*/
protected $reactRequest;
/**
* Set value of ReactRequest
*
* @param \React\Http\Request $reactRequest
*/
public function setReactRequest($reactRequest)
{
$this->reactRequest = $reactRequest;
$this->setUri($reactRequest->getPath());
$this->setRequestUri($reactRequest->getPath());
$this->getHeaders()->addHeaders($reactRequest->getHeaders());
$this->setMethod($reactRequest->getMethod());
$this->setQuery(new Parameters($reactRequest->getQuery()));
$server = $this->getServer();
$server->set('REQUEST_URI', $reactRequest->getPath());
$server->set('SERVER_NAME', $this->getHeader('Host'));
}
/**
* @param string $value
*
* @return \Zend\Stdlib\Message
*/
public function setContent($value)
{
parse_str($value, $arr);
if (is_array($arr)) {
$this->setPost(new Parameters($arr));
}
return parent::setContent($value);
}
/**
* Return value of ReactRequest
*
* @return \React\Http\Request
*/
public function getReactRequest()
{
return $this->reactRequest;
}
}
<?php
namespace Common\Application\Server\Mvc;
use \Zend\Http\PhpEnvironment\Response as ZendResponse;
use \React\Http\Response as ReactResponse;
class Response extends ZendResponse
{
/**
* @var ReactResponse
*/
protected $reactResponse;
/**
* @return ZendResponse
*/
public function sendHeaders()
{
$this->getReactResponse()->writeHead($this->getStatusCode(), $this->getHeaders()->toArray());
return $this;
}
/**
* @return ZendResponse
*/
public function sendContent()
{
$this->getReactResponse()->end($this->getContent());
return $this;
}
/**
* Set value of ReactResponse
*
* @param \React\Http\Response $reactResponse
*/
public function setReactResponse($reactResponse)
{
$this->reactResponse = $reactResponse;
}
/**
* Return value of ReactResponse
*
* @return \React\Http\Response
*/
public function getReactResponse()
{
return $this->reactResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment