-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCallableHttpKernel.php
More file actions
48 lines (37 loc) · 1.53 KB
/
CallableHttpKernel.php
File metadata and controls
48 lines (37 loc) · 1.53 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
47
48
<?php
namespace Stack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
class CallableHttpKernel implements HttpKernelInterface, TerminableInterface
{
private $handleCallable;
private $terminateCallable;
public function __construct($handleCallable, $terminateCallable = null)
{
if (!is_callable($handleCallable)) {
throw new \InvalidArgumentException('Invalid handleCallable passed to CallableHttpKernel::__construct().');
}
if ($terminateCallable && !is_callable($terminateCallable)) {
throw new \InvalidArgumentException('Invalid terminateCallable passed to CallableHttpKernel::__construct().');
}
$this->handleCallable = $handleCallable;
$this->terminateCallable = $terminateCallable;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$response = call_user_func($this->handleCallable, $request, $type, $catch);
if (!$response instanceof Response) {
throw new \UnexpectedValueException('Kernel function did not return an object of type Response');
}
return $response;
}
public function terminate(Request $request, Response $response)
{
if (!$this->terminateCallable) {
return;
}
call_user_func($this->terminateCallable, $request, $response);
}
}