-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebRunner.class.php
executable file
·88 lines (81 loc) · 2.63 KB
/
WebRunner.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php namespace xp\web;
use lang\ClassLoader;
use web\Environment;
use web\Error;
use web\InternalServerError;
use web\Request;
use web\Response;
use web\Status;
/**
* Entry point for web-main.php
*
* Set the following environment variables (e.g. via Apache SetEnv):
*
* - WEB_SOURCE - the source: an application file or class name
* - WEB_CONFIG - to the config file paths separated by the path separator char
* - WEB_ARGS - optional, to any arguments you want to pass to environment
* - WEB_LOG - where to write logfile to, defaults to no logfile
*
* The source may contain filters, e.g. `application[+filter[,filter[,...]]]`
*/
class WebRunner {
/**
* Sends an error
*
* @param web.Request $response
* @param web.Response $response
* @param web.Environment $env
* @param ?web.Error $error
* @return void
*/
private static function error($request, $response, $env, $error) {
if ($response->flushed()) {
error_log($error->toString(), 4); // 4 = SAPI error logger
} else {
$loader= ClassLoader::getDefault();
$message= Status::message($error->status());
$response->answer($error->status(), $message);
foreach (['web/error-'.$env->profile().'.html', 'web/error.html'] as $variant) {
if (!$loader->providesResource($variant)) continue;
$response->send(sprintf(
$loader->getResource($variant),
$error->status(),
htmlspecialchars($message),
htmlspecialchars($error->getMessage()),
htmlspecialchars($error->toString())
));
break;
}
}
$env->logging()->log($request, $response, $error);
}
/** @param string[] $args */
public static function main($args) {
$env= new Environment(
$args[2],
$args[0],
$args[1],
explode(PATH_SEPARATOR, getenv('WEB_CONFIG')),
explode('|', getenv('WEB_ARGS')),
getenv('WEB_LOG') ?: null
);
$sapi= new SAPI();
$request= new Request($sapi);
$response= new Response($sapi);
$response->header('Date', gmdate('D, d M Y H:i:s T'));
$response->header('Host', $request->header('Host'));
try {
$application= (new Source(getenv('WEB_SOURCE'), $env))->application();
$application->service($request, $response);
$env->logging()->log($request, $response);
} catch (Error $e) {
self::error($request, $response, $env, $e);
} catch (\Throwable $e) { // PHP7
self::error($request, $response, $env, new InternalServerError($e));
} catch (\Exception $e) { // PHP5
self::error($request, $response, $env, new InternalServerError($e));
} finally {
$response->end();
}
}
}