-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicroKernel.php
More file actions
175 lines (154 loc) · 6.17 KB
/
MicroKernel.php
File metadata and controls
175 lines (154 loc) · 6.17 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
<?php
namespace Bundle\MicroKernelBundle;
use Symfony\Foundation\Kernel;
use Symfony\Components\HttpKernel\Request;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\Routing\RouteCollection;
use Symfony\Components\Routing\Route;
use Symfony\Components\Routing\FileResource;
use Symfony\Components\EventDispatcher\Event;
class MicroKernel {
const DEFAULT_ROUTE_NAME = 'route';
protected $kernel;
protected $container;
protected $request;
protected $routeCollection;
protected $router;
protected $dispatcher;
protected $requestParser;
protected $bindings = array();
protected $lastRoute;
protected $lastCallback;
protected $callbacks = array();
public function __construct(Kernel $kernel)
{
$this->kernel = $kernel;
if (!$this->kernel->isBooted()) {
$this->kernel->boot();
}
$this->container = $this->kernel->getContainer();
$this->container->setService('server', $this);
$this->router = $this->container->getRouterService();
$this->requestParser = $this->container->getRequestParserService();
$this->dispatcher = $this->container->getEventDispatcherService();
}
public function get($pattern, $callback, array $defaults = array()) {
$this->appendRoute('GET', $pattern, $callback, $defaults);
return $this;
}
public function post($pattern, $callback, array $defaults = array()) {
$this->appendRoute('POST', $pattern, $callback, $defaults);
return $this;
}
public function put($pattern, $callback, array $defaults = array()) {
$this->appendRoute('PUT', $pattern, $callback, $defaults);
return $this;
}
public function delete($pattern, $callback, array $defaults = array()) {
$this->appendRoute('DELETE', $pattern, $callback, $defaults);
return $this;
}
public function head($pattern, $callback, array $defaults = array()) {
$this->appendRoute('HEAD', $pattern, $callback, $defaults);
return $this;
}
public function run(Request $request = null)
{
if (null === $request) {
$request = new Request();
}
$this->request = $request;
$fileResource = new FileResource($this->request->server->get('SCRIPT_FILENAME'));
$this->persistRoute();
$routes = $this->getRouteCollection();
$routes->addResource($fileResource);
$this->router->getRouteCollection()->addCollection($routes);
$this->dispatcher->disconnect('core.request', array($this->requestParser, 'resolve'));
$this->dispatcher->connect('core.request', array($this, 'resolve'));
return $this->kernel->handle($this->request);
}
public function resolve(Event $event)
{
$this->requestParser->resolve($event);
$request = $event->getParameter('request');
$routeName = $request->path->get('_route');
if (isset ($this->callbacks[$routeName])) {
$callback = $this->callbacks[$routeName];
$params = $this->getCallbackParams(new \ReflectionFunction($callback), $routeName, $request->path->all(), $request);
$response = call_user_func_array($callback, $params);
if (!$response instanceof Response) {
$response = new Response($response);
}
$event->setProcessed(true);
$event->setReturnValue($response);
return true;
}
}
protected function getCallbackParams(\ReflectionFunctionAbstract $r, $function, array $parameters, Request $request)
{
$params = array();
foreach ($r->getParameters() as $param) {
if ($param->getName() == 'container') {
$params[] = $this->container;
} elseif ($param->getName() == 'request') {
$params[] = $request;
} elseif (array_key_exists($param->getName(), $parameters)) {
$params[] = $parameters[$param->getName()];
} elseif ($param->isDefaultValueAvailable()) {
$params[] = $param->getDefaultValue();
} else {
throw new \RuntimeException(sprintf('Function "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $function, $param->getName()));
}
}
return $params;
}
public function setRouteCollection(RouteCollection $collection)
{
$this->routeCollection = $collection;
}
public function getRouteCollection()
{
if (!isset ($this->routeCollection)) {
$this->routeCollection = new RouteCollection();
}
return $this->routeCollection;
}
protected function appendRoute($method, $pattern, $callback, array $defaults = array())
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('$callback, must be callable');
}
$this->persistRoute();
$this->lastCallback = $callback;
$this->lastRoute = new Route($pattern, $defaults, array('_method' => $method));//, array $options = array());
}
protected function persistRoute()
{
if (isset ($this->lastRoute)) {
$routeName = $this->getRouteName($this->lastRoute);
$this->getRouteCollection()->addRoute($routeName, $this->lastRoute);
$this->callbacks[$routeName] = $this->lastCallback;
$this->lastCallback = $this->lastRoute = null;
}
}
public function validate($key, $value)
{
if (isset ($this->lastRoute)) {
$requirements = $this->lastRoute->getRequirements();
$requirements[$key] = $value;
$this->lastRoute->setRequirements($requirements);
}
return $this;
}
public function bind($name)
{
$this->bindings[$name] = $this->lastRoute;
}
private function getRouteName(Route $route)
{
if (false === ($routeName = array_search($route, $this->bindings))) {
$routeName = self::DEFAULT_ROUTE_NAME . '_' . (count($this->getRouteCollection()->getRoutes()) + 1);
}
return $routeName;
}
}