1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ramro; |
4
|
|
|
|
5
|
|
|
use League\Container\Container; |
6
|
|
|
use League\Container\ContainerAwareInterface; |
7
|
|
|
use League\Container\ContainerInterface; |
8
|
|
|
use League\Container\ReflectionContainer; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Ramro\Providers\HttpMessageServiceProvider; |
12
|
|
|
use Ramro\Providers\RouteServiceProvider; |
13
|
|
|
use Zend\Diactoros\Response\EmitterInterface; |
14
|
|
|
|
15
|
|
|
class Application implements ContainerAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \League\Container\ContainerInterface |
19
|
|
|
*/ |
20
|
|
|
protected $container; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->boot(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set a container |
29
|
|
|
* |
30
|
|
|
* @param \League\Container\ContainerInterface $container |
31
|
|
|
*/ |
32
|
|
|
public function setContainer(ContainerInterface $container) |
33
|
|
|
{ |
34
|
|
|
$this->container = $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the container |
39
|
|
|
* |
40
|
|
|
* @return \League\Container\ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
public function getContainer() |
43
|
|
|
{ |
44
|
|
|
if (isset($this->container)) { |
45
|
|
|
return $this->container; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$container = new Container; |
49
|
|
|
$container->delegate(new ReflectionContainer()); |
50
|
|
|
$this->setContainer($container); |
51
|
|
|
|
52
|
|
|
return $container; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function boot() |
56
|
|
|
{ |
57
|
|
|
$container = $this->getContainer(); |
58
|
|
|
$container->addServiceProvider(HttpMessageServiceProvider::class); |
59
|
|
|
$container->addServiceProvider(RouteServiceProvider::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function run() |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$response = $this->container->get('route')->dispatch( |
66
|
|
|
$this->container->get(ServerRequestInterface::class), |
67
|
|
|
$this->container->get(ResponseInterface::class) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $this->container->get(EmitterInterface::class)->emit($response); |
71
|
|
|
|
72
|
|
|
} catch (\Exception $exception) { |
73
|
|
|
// register exception handler which returns PSR-7 Response |
74
|
|
|
throw $exception; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|