-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
179 lines (154 loc) · 4.2 KB
/
App.php
File metadata and controls
179 lines (154 loc) · 4.2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Example;
use Illuminate\Contracts\Container\Container as ContainerInterface;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcherInterface;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Events\Dispatcher as EventsDispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use MultiRouting\Adapters\Main\Adapter as MainAdapter;
use MultiRouting\Adapters\Main\Adapter;
use MultiRouting\Adapters\Soap\Adapter as SoapAdapter;
use MultiRouting\Adapters\JsonRpc\Adapter as JsonRpcAdapter;
use MultiRouting\AdapterService;
use MultiRouting\Router as MultiRouter;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class App
{
/**
* The application IoC container
*
* @var Container
*/
protected $container;
/**
* @var EventsDispatcherInterface
*/
protected $eventsDispatcher;
/**
* The application router
*
* @var MultiRouter
*/
protected $router;
/**
* @return static
*/
public static function build()
{
$container = new Container();
$eventsDispatcher = new EventsDispatcher();
$adapterService = new AdapterService($container);
$baseRouter = new Router($eventsDispatcher, $container);
$multiRouter = new MultiRouter($baseRouter, $adapterService);
return new static(
$container,
$eventsDispatcher,
$multiRouter
);
}
/**
* App constructor.
*
* @param ContainerInterface $container
* @param Dispatcher $eventsDispatcher
* @param MultiRouter $multiRouter
*
* @throws \Exception
*/
public function __construct(
ContainerInterface $container,
EventsDispatcherInterface $eventsDispatcher,
MultiRouter $multiRouter
) {
$this->setContainer($container);
$this->setEventsDispatcher($eventsDispatcher);
$this->setRouter($multiRouter);
$this->initRoutes();
}
/**
* @return Container
*/
public function getContainer()
{
return $this->container;
}
/**
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @return EventsDispatcherInterface
*/
public function getEventsDispatcher()
{
return $this->eventsDispatcher;
}
/**
* @param EventsDispatcherInterface $eventsDispatcher
*/
protected function setEventsDispatcher(EventsDispatcherInterface $eventsDispatcher)
{
$this->eventsDispatcher = $eventsDispatcher;
}
/**
* @return MultiRouter
*/
public function getRouter()
{
return $this->router;
}
/**
* Set the application router
*
* @param MultiRouter $multiRouter
*
* @throws \Exception
*/
protected function setRouter(MultiRouter $multiRouter)
{
$this->router = $multiRouter;
$this->router->allowAdapters([
MainAdapter::name,
JsonRpcAdapter::name,
SoapAdapter::name,
'Rest'
]);
}
protected function initRoutes()
{
require __DIR__ . '/resources/routes.php';
}
/**
* @return \Illuminate\Http\Response
*/
public function run()
{
$request = Request::createFromBase(Request::createFromGlobals());
return $this->handle($request);
}
/**
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function handle(Request $request)
{
try {
$response = $this->router->dispatch($request);
} catch (NotFoundHttpException $e) {
// parse request to return an error according to protocol
$response = new Response('Page not found', 404);
} catch (MethodNotAllowedHttpException $e) {
$response = new Response('Page not found', 404);
}
$response->sendHeaders();
$response->send();
return $response;
}
}