|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * CLI Built-in web server for API |
| 5 | + * |
| 6 | + * This is an entry point for an API response based application build. |
| 7 | + * |
| 8 | + * Examples: |
| 9 | + * |
| 10 | + * CLI: |
| 11 | + * $ php api.pgp get page://self/ |
| 12 | + * $ php api.pgp get 'page://first/greeting?name=koriym' |
| 13 | + * |
| 14 | + * Built-in web server: |
| 15 | + * |
| 16 | + * $ php -S localhost:8089 api.php |
| 17 | + * |
| 18 | + * @global $context |
| 19 | + */ |
| 20 | +use BEAR\Resource\Exception\Parameter as BadRequest; |
| 21 | +use BEAR\Resource\Exception\ResourceNotFound as NotFound; |
| 22 | + |
| 23 | +// |
| 24 | +// The cache is cleared on each request via the following script. We understand that you may want to debug |
| 25 | +// your application with caching turned on. When doing so just comment out the following. |
| 26 | +// |
| 27 | +require dirname(dirname(__DIR__)) . '/bin/clear.php'; |
| 28 | + |
| 29 | +// |
| 30 | +// Here we get an application instance by setting a $context variable such as (prod, dev, api, stub, test) |
| 31 | +// the dev instance provides debugging tools and defaults to help you the development of your application. |
| 32 | +// |
| 33 | +$context = 'api'; |
| 34 | +$app = require dirname(dirname(__DIR__)) . '/bootstrap/instance.php'; |
| 35 | +/* @var $app \BEAR\Package\Provide\Application\AbstractApp */ |
| 36 | + |
| 37 | +// |
| 38 | +// When using the CLI we set the router arguments needed for CLI use. |
| 39 | +// Otherwise just get the path directly from the globals. |
| 40 | +// |
| 41 | +if (PHP_SAPI === 'cli') { |
| 42 | + $app->router->setArgv($argv); |
| 43 | + $uri = $argv[2]; |
| 44 | + parse_str((isset(parse_url($uri)['query']) ? parse_url($uri)['query'] : ''), $get); |
| 45 | +} else { |
| 46 | + $pathInfo = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : '/index'; |
| 47 | + $uri = 'app://self' . $pathInfo; |
| 48 | + $get = $_GET; |
| 49 | +} |
| 50 | + |
| 51 | +// |
| 52 | +// Get the method from the router and attempt to request the resource and render. |
| 53 | +// On failure trigger the error handler. |
| 54 | +// |
| 55 | +try { |
| 56 | + list($method,) = $app->router->match(); |
| 57 | + $app->page = $app->resource->$method->uri($uri)->withQuery($get)->eager->request(); |
| 58 | +} catch (NotFound $e) { |
| 59 | + $code = 404; |
| 60 | + $body = 'Not Found'; |
| 61 | + goto ERROR; |
| 62 | +} catch (BadRequest $e) { |
| 63 | + $code = 400; |
| 64 | + $body = 'Bad Request'; |
| 65 | + goto ERROR; |
| 66 | +} catch (Exception $e) { |
| 67 | + $code = 503; |
| 68 | + $body = 'Service Unavailable'; |
| 69 | + error_log((string)$e); |
| 70 | + goto ERROR; |
| 71 | +} |
| 72 | + |
| 73 | +// |
| 74 | +// OK: Sets the response resources and renders |
| 75 | +// ERROR: sets the response code and loads error page. |
| 76 | +// |
| 77 | +OK: { |
| 78 | + $app->response->setResource($app->page)->render()->send(); |
| 79 | + exit(0); |
| 80 | +} |
| 81 | + |
| 82 | +ERROR: { |
| 83 | + if (PHP_SAPI === 'cli') { |
| 84 | + $app->exceptionHandler->handle($e); |
| 85 | + exit; |
| 86 | + } |
| 87 | + http_response_code($code); |
| 88 | + echo $body; |
| 89 | + exit(1); |
| 90 | +} |
0 commit comments