-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMini.php
More file actions
189 lines (157 loc) · 3.7 KB
/
Copy pathMini.php
File metadata and controls
189 lines (157 loc) · 3.7 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
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Mini - a micro PHP 5 framework
*
* @author tooreht <tooreht@gmail.com>
* @copyright 2014 tooreht
* @link http://www.miniframework.com
* @license http://www.miniframework.com/license
* @version 0.0.1
* @package Mini
*/
namespace mini;
use mini\Environment;
use mini\Route;
use mini\http\HttpKernelInterface;
use mini\http\Response;
use mini\http\Request;
use mini\http\StackBuilder;
use mini\utils\Container;
class Mini implements HttpKernelInterface
{
/**
* @var \mini\utils\Container
*/
public $container;
public function __construct()
{
$this->container = new Container();
// environment
$this->container->singleton('environment', function ($c) {
return Environment::getInstance();
});
// request
$this->container->singleton('request', function ($c) {
return new Request($c['environment']);
});
// response
$this->container->singleton('response', function ($c) {
return new Response($c['environment']);
});
// router
$this->container->singleton('router', function ($c) {
return new Router();
});
// middleware
$this->container->singleton('middleware', function ($c) {
return new StackBuilder();
});
}
public function __get($name)
{
return $this->container[$name];
}
public function __set($name, $value)
{
$this->container[$name] = $value;
}
public function __isset($name)
{
return isset($this->container[$name]);
}
public function __unset($name)
{
unset($this->container[$name]);
}
public function map($args)
{
$pattern = array_shift($args);
$callable = array_pop($args);
$route = new Route($pattern, $callable);
$this->router->map($route);
foreach ($args as $md) {
$route->middleware->push($md);
}
return $route;
}
/**
* GET request
*/
public function get()
{
if (func_num_args() < 2) {
throw new \InvalidArgumentException("Missing argument(s) when calling get");
}
$args = func_get_args();
return $this->map($args)->via(Request::METHOD_GET, Request::METHOD_HEAD);
}
/**
* POST request
*/
public function post()
{
if (func_num_args() < 2) {
throw new \InvalidArgumentException("Missing argument(s) when calling post");
}
$args = func_get_args();
return $this->map($args)->via(Request::METHOD_POST);
}
/**
* PUT request
*/
public function put()
{
if (func_num_args() < 2) {
throw new \InvalidArgumentException("Missing argument(s) when calling put");
}
$args = func_get_args();
return $this->map($args)->via(Request::METHOD_PUT);
}
/**
* PATCH request
*/
public function patch()
{
if (func_num_args() < 2) {
throw new \InvalidArgumentException("Missing argument(s) when calling patch");
}
$args = func_get_args();
return $this->map($args)->via(Request::METHOD_PATCH);
}
/**
* DELETE request
*/
public function delete()
{
if (func_num_args() < 2) {
throw new \InvalidArgumentException("Missing argument(s) when calling delete");
}
$args = func_get_args();
return $this->map($args)->via(Request::METHOD_DELETE);
}
/**
* Run app
*/
public function run()
{
$app = $this->middleware->resolve($this);
$app->handle($this->request);
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$method = $request->getMethod();
$resourceUri = $request->getResourceUri();
$routes = $this->router->matchRoutes($method, $resourceUri);
ob_start();
if ($routes) {
foreach ($routes as $route) {
echo call_user_func_array($route->getCallable(), array_values($route->getParams()));
}
}
else
{
$this->response->notFound();
}
ob_end_flush();
}
}