项目作者: TbhT

项目描述 :
An async framework in php, like koa in nodejs
高级语言: PHP
项目地址: git://github.com/TbhT/press.git
创建时间: 2018-06-06T14:16:00Z
项目社区:https://github.com/TbhT/press

开源协议:Apache License 2.0

下载


Press

PHP Composer
codecov

This project is a translation of koa framework of nodejs.

Installation

$ composer require tbht/press

Hello Press

  1. use Press\Application;
  2. use Press\Context;
  3. $app = new Application();
  4. $app->use(function (Context $ctx, callable $next) {
  5. $ctx->body = 'Hello Press';
  6. });
  7. $app->listen();

Middleware

Here is an example of logger middleware :

  1. use Press\Context;
  2. use Press\Application;
  3. $app = new Application();
  4. $app->use(function (Context $ctx, callable $next) {
  5. $start = time();
  6. return $next()
  7. ->then(function () use($start, $ctx) {
  8. $ms = time() - $start;
  9. $method = $ctx->method;
  10. $url = $ctx->url;
  11. print_r("{$method} {$url} - {$ms}ms");
  12. });
  13. });

Document

Application

hello world

  1. use Press\Application;
  2. use Press\Context;
  3. $app = new Application();
  4. $app->use(function (Context $ctx, callable $next) {
  5. $ctx->body = 'Hello World';
  6. });
  7. $app->listen(function () {
  8. var_dump('final var dump');
  9. });
  10. // or
  11. $app->listen(['port' => 8080]);

Cascading

  1. use Press\Application;
  2. use Press\Context;
  3. $app = new Application();
  4. // logger
  5. $app->use(function (Context $ctx, callable $next) {
  6. return $next()
  7. ->then(function () use ($ctx) {
  8. $rt = $ctx->response->get('x-response-time');
  9. $method = $ctx->method;
  10. $url = $ctx->url;
  11. echo "{$method} {$url} - {$rt}";
  12. });
  13. });
  14. // x-response-time
  15. $app->use(function (Context $ctx, callable $next) {
  16. $start = time();
  17. return $next()
  18. ->then(function () use ($ctx,$start) {
  19. $ms = time() - $start;
  20. $ctx->set('x-response-time', "{$ms}ms");
  21. });
  22. });
  23. // response
  24. $app->use(function (Context $ctx, callable $next) {
  25. $ctx->body = 'Hello World';
  26. });

Settings

  • $app->env default to the ‘development’

  • $app->proxy when true proxy header fields will be trusted

  • $app->subdomainOffset offset of .subdomains to ignore [2]

$app->listen(…)

  1. use Press\Application;
  2. $app = new Application();
  3. $app->listen([
  4. "host" => "127.0.0.1",
  5. "port" => 8080
  6. ]);
  7. // or
  8. $app->listen(function () {
  9. echo "call back run";
  10. });

$app->callback()

return a callback function suitable for the following http server request.

  1. use React\EventLoop\Factory;
  2. $loop = Factory::create();
  3. $server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
  4. // ...
  5. });

$app->use()

see Middlware part

$app->context

$app->context is the prototype from which ctx is created.

  1. $app->use(function ($ctx) {
  2. $ctx->db = new DB();
  3. });

Error handling

  1. $app->on("error", function () {
  2. echo "this is an error";
  3. });

Context