1 | <?php |
||
13 | class App |
||
14 | { |
||
15 | /** |
||
16 | * Dependency Injection container. |
||
17 | * |
||
18 | * @var ContainerInterface |
||
19 | */ |
||
20 | private $container; |
||
21 | |||
22 | /** |
||
23 | * Application initialization. |
||
24 | * |
||
25 | * @param ContainerInterface $container Dependency Injection container. |
||
26 | * |
||
27 | * @throws Exception If no router is defined. |
||
28 | */ |
||
29 | 19 | public function __construct(ContainerInterface $container = null) |
|
30 | { |
||
31 | 19 | if ($container === null) { |
|
32 | 2 | $container = Container\PHPDiFactory::buildContainer(Loader::load()); |
|
33 | 2 | } |
|
34 | |||
35 | 19 | if ($container->has('router') === false) { |
|
36 | 1 | throw new Exception('Define router config'); |
|
37 | } |
||
38 | |||
39 | 19 | $this->container = $container; |
|
40 | 19 | } |
|
41 | |||
42 | /** |
||
43 | * Container getter. |
||
44 | * |
||
45 | * @return ContainerInterface |
||
46 | */ |
||
47 | 17 | public function getContainer() |
|
51 | |||
52 | /** |
||
53 | * Penny dispatcher getter. |
||
54 | * |
||
55 | * @return Dispatcher |
||
56 | */ |
||
57 | 16 | private function getDispatcher() |
|
58 | { |
||
59 | 16 | $dispatcher = $this->container->get('dispatcher'); |
|
60 | 16 | if (!is_callable($dispatcher)) { |
|
61 | 1 | throw new \RuntimeException('Dispatcher must be a callable'); |
|
62 | } |
||
63 | |||
64 | 15 | return $dispatcher; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Penny HTTP flow event getter. |
||
69 | * |
||
70 | * @return EventManagerInterface |
||
71 | */ |
||
72 | 15 | private function getEventManager() |
|
76 | |||
77 | /** |
||
78 | * Setup event with Request and Response provided |
||
79 | * |
||
80 | * @param mixed|null $request Representation of an outgoing, |
||
81 | * client-side request. |
||
82 | * @param mixed|null $response Representation of an incoming, |
||
83 | * server-side response. |
||
84 | * |
||
85 | * @throws RuntimeException if event did not supported. |
||
86 | */ |
||
87 | 17 | private function setUpEventWithRequestResponse($request, $response) |
|
103 | |||
104 | /** |
||
105 | * Application execution. |
||
106 | * |
||
107 | * @param mixed|null $request Representation of an outgoing, |
||
108 | * client-side request. |
||
109 | * @param mixed|null $response Representation of an incoming, |
||
110 | * server-side response. |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | 17 | public function run($request = null, $response = null) |
|
134 | |||
135 | /** |
||
136 | * Handle Route. |
||
137 | * |
||
138 | * @param mixed $routeInfo |
||
139 | * @param EventInterface $event |
||
140 | * |
||
141 | * @throws RuntimeException if dispatch does not return RouteInfo object. |
||
142 | */ |
||
143 | 12 | private function handleRoute($routeInfo, EventInterface $event) |
|
152 | |||
153 | /** |
||
154 | * Handle Response. |
||
155 | * |
||
156 | * @param EventManagerInterface $eventManager |
||
157 | * @param EventInterface $event |
||
158 | * @param RouteInfoInterface $routeInfo |
||
159 | */ |
||
160 | 11 | private function handleResponse( |
|
179 | |||
180 | /** |
||
181 | * Event Manager trigger with exception |
||
182 | * |
||
183 | * @param EventManagerInterface $eventManager |
||
184 | * @param EventInterface $event |
||
185 | * @param string $name |
||
186 | * @param Exception $exception |
||
187 | * |
||
188 | * @return EventInterface |
||
189 | */ |
||
190 | 5 | private function triggerWithException( |
|
202 | } |
||
203 |