1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Penny; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use Penny\Config\Loader; |
8
|
|
|
use Penny\Event\PennyEventInterface; |
9
|
|
|
use Penny\Route\RouteInfoInterface; |
10
|
|
|
use Interop\Container\ContainerInterface; |
11
|
|
|
use Zend\EventManager\EventManager; |
12
|
|
|
|
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
|
36 |
|
public function __construct(ContainerInterface $container = null) |
30
|
|
|
{ |
31
|
36 |
|
if ($container === null) { |
32
|
4 |
|
$container = Container\PHPDiFactory::buildContainer(Loader::load()); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
36 |
|
if ($container->has('router') === false) { |
36
|
2 |
|
throw new Exception('Define router config'); |
37
|
|
|
} |
38
|
|
|
|
39
|
36 |
|
$this->container = $container; |
40
|
36 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Container getter. |
44
|
|
|
* |
45
|
|
|
* @return ContainerInterface |
46
|
|
|
*/ |
47
|
36 |
|
public function getContainer() |
48
|
|
|
{ |
49
|
36 |
|
return $this->container; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Penny dispatcher getter. |
54
|
|
|
* |
55
|
|
|
* @return Dispatcher |
56
|
|
|
*/ |
57
|
30 |
|
private function getDispatcher() |
58
|
|
|
{ |
59
|
30 |
|
$dispatcher = $this->container->get('dispatcher'); |
60
|
30 |
|
if (!is_callable($dispatcher)) { |
61
|
2 |
|
throw new \RuntimeException('Dispatcher must be a callable'); |
62
|
|
|
} |
63
|
|
|
|
64
|
28 |
|
return $dispatcher; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Penny HTTP flow event getter. |
69
|
|
|
* |
70
|
|
|
* @return EventManager |
71
|
|
|
*/ |
72
|
28 |
|
private function getEventManager() |
73
|
|
|
{ |
74
|
28 |
|
return $this->container->get('event_manager'); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Application execution. |
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
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
32 |
|
public function run($request = null, $response = null) |
88
|
|
|
{ |
89
|
32 |
|
$event = $this->getContainer()->get('http_flow_event'); |
90
|
32 |
|
if (!($event instanceof PennyEventInterface)) { |
91
|
2 |
|
throw new RuntimeException('This event did not supported'); |
92
|
|
|
} |
93
|
30 |
|
if ($request !== null) { |
94
|
30 |
|
$event->setRequest($request); |
95
|
15 |
|
} |
96
|
30 |
|
if ($response !== null) { |
97
|
30 |
|
$event->setResponse($response); |
98
|
15 |
|
} |
99
|
|
|
|
100
|
30 |
|
$dispatcher = $this->getDispatcher(); |
101
|
28 |
|
$eventManager = $this->getEventManager(); |
102
|
|
|
|
103
|
|
|
try { |
104
|
28 |
|
$routeInfo = call_user_func($dispatcher, $event->getRequest()); |
105
|
|
|
|
106
|
22 |
|
if (!($routeInfo instanceof RouteInfoInterface)) { |
107
|
2 |
|
throw new RuntimeException('Dispatch does not return RouteInfo object'); |
108
|
|
|
} |
109
|
|
|
|
110
|
20 |
|
$event->setRouteInfo($routeInfo); |
111
|
20 |
|
$event->setName($routeInfo->getName()); |
112
|
18 |
|
} catch (Exception $exception) { |
113
|
8 |
|
return $this->triggerWithException($eventManager, $event, 'dispatch_error', $exception) |
|
|
|
|
114
|
8 |
|
->getResponse(); |
115
|
|
|
} |
116
|
|
|
|
117
|
20 |
|
$eventManager->attach($event->getName(), function ($event) use ($routeInfo) { |
118
|
18 |
|
$event->setResponse(call_user_func_array( |
119
|
18 |
|
$routeInfo->getCallable(), |
120
|
18 |
|
[$event->getRequest(), $event->getResponse()] + $routeInfo->getParams() |
121
|
9 |
|
)); |
122
|
20 |
|
}, 0); |
123
|
|
|
|
124
|
|
|
try { |
125
|
20 |
|
$eventManager->trigger($event); |
|
|
|
|
126
|
11 |
|
} catch (Exception $exception) { |
127
|
2 |
|
$this->triggerWithException($eventManager, $event, $routeInfo->getName().'_error', $exception); |
128
|
|
|
} |
129
|
|
|
|
130
|
18 |
|
return $event->getResponse(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Event Manager trigger with exception |
135
|
|
|
* |
136
|
|
|
* @param \Zend\EventManager\EventManager|\Cake\Event\EventManager $eventManager |
137
|
|
|
* @param PennyEventInterface|\Zend\EventManager\EventInterface|\Cake\Event\Event $event |
138
|
|
|
* @param string $name |
139
|
|
|
* @param Exception $exception |
140
|
|
|
* |
141
|
|
|
* @return PennyEventInterface|\Zend\EventManager\EventInterface|\Cake\Event\Event |
142
|
|
|
*/ |
143
|
10 |
|
private function triggerWithException($eventManager, $event, $name, Exception $exception) |
144
|
|
|
{ |
145
|
10 |
|
$event->setName($name); |
|
|
|
|
146
|
10 |
|
$event->setException($exception); |
|
|
|
|
147
|
10 |
|
$eventManager->trigger($event); |
|
|
|
|
148
|
|
|
|
149
|
8 |
|
return $event; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: