This repository was archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathLambdaHandler.php
More file actions
46 lines (42 loc) · 1.68 KB
/
LambdaHandler.php
File metadata and controls
46 lines (42 loc) · 1.68 KB
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
<?php
class LambdaHandler
{
private $handlerFileName;
private $handlerMethodName;
private $handlerClass;
public function __get($name)
{
if (in_array($name, ['handlerFileName', 'handlerMethodName'])) {
return $this->$name;
} else {
throw new Exception("Cannot access private property {$name}");
}
}
public function __construct($envHandler)
{
$handlerSplit = explode('.', $envHandler);
if (count($handlerSplit) == 2) {
[$this->handlerFileName, $this->handlerMethodName] = $handlerSplit;
} elseif (count($handlerSplit) == 3) {
[$this->handlerFileName, $this->handlerClass, $this->handlerMethodName] = $handlerSplit;
} else {
throw new Exception("Invalid handler {$handlerSplit}, must be of form FILENAME.METHOD or FILENAME.CLASS.METHOD where FILENAME corresponds with an existing PHP source file FILENAME.php, CLASS is an optional module/class namespace and METHOD is a callable method. If using CLASS, METHOD must be a static method.");
}
}
public function callHandler($request, $context)
{
try {
if ($this->handlerClass) {
$fun = "{$this->handlerClass}::{$this->handlerMethodName}";
} else {
$fun = $this->handlerMethodName;
}
$response = call_user_func($fun, $request, $context);
return LambdaMarshaller::marshallResponse($response);
} catch (Error $e) {
throw new LambdaErrors\LambdaHandlerCriticalException($e);
} catch (Exception $e) {
throw new LambdaErrors\LambdaHandlerError($e);
}
}
}