├── .gitignore
├── README.md
├── app
├── controllers
│ └── HomeController.php
└── Http
│ ├── RouterInterface.php
│ ├── FastRouteRouter.php
│ └── AuraRouter.php
├── composer.json
├── .editorconfig
├── public
└── index.php
└── composer.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | vendor/
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Code for the talk "Roll your own micro framework"
2 |
--------------------------------------------------------------------------------
/app/controllers/HomeController.php:
--------------------------------------------------------------------------------
1 | addDefinitions([
10 | Router::class => object(AuraRouter::class)
11 | ]);
12 | $container = $containerBuilder->build();
13 |
14 | $router = $container->get('Router');
15 |
16 | $router->get('/', 'MicroFramework\Controllers\HomeController', 'index');
17 |
18 | $router->dispatch();
19 |
20 | if ($router->notFound()) {
21 | echo '404';
22 | } elseif ($router->methodNotAllowed()) {
23 | echo '403';
24 | } elseif ($router->found()) {
25 | $container->call($router->getController(), $router->getParams());
26 | }
27 |
28 | echo '
from: ' . get_class($router);
29 |
--------------------------------------------------------------------------------
/app/Http/FastRouteRouter.php:
--------------------------------------------------------------------------------
1 | routes[] = [
16 | 'action' => $action,
17 | 'route' => $route,
18 | 'handler' => [$controller, $method]
19 | ];
20 | }
21 |
22 | public function get ($route, $controller, $method)
23 | {
24 | $this->addRoute('GET', $route, $controller, $method);
25 | }
26 |
27 | public function dispatch ()
28 | {
29 | $dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $r) {
30 | foreach ($this->routes as $route) {
31 | $r->addRoute($route['action'], $route['route'], $route['handler']);
32 | }
33 | });
34 |
35 | $this->dispatchedRoute = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
36 | }
37 |
38 | public function getController ()
39 | {
40 | return $this->dispatchedRoute[1];
41 | }
42 |
43 | public function getParams ()
44 | {
45 | return $this->dispatchedRoute[2];
46 | }
47 |
48 | public function found ()
49 | {
50 | return $this->dispatchedRoute[0] === FastRoute\Dispatcher::FOUND;
51 | }
52 |
53 | public function notFound ()
54 | {
55 | return $this->dispatchedRoute[0] === FastRoute\Dispatcher::NOT_FOUND;
56 | }
57 |
58 | public function methodNotAllowed ()
59 | {
60 | return $this->dispatchedRoute[0] === FastRoute\Dispatcher::METHOD_NOT_ALLOWED;
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/app/Http/AuraRouter.php:
--------------------------------------------------------------------------------
1 | router = $routerFactory->newInstance();
17 | }
18 |
19 | public function addRoute ($action, $route, $controller, $method)
20 | {
21 | $action = 'add' . ucfirst(strtolower($action));
22 |
23 | $this->router->{$action}($controller . '::' . $method, $route);
24 | }
25 |
26 | public function get ($route, $controller, $method)
27 | {
28 | $this->addRoute('GET', $route, $controller, $method);
29 | }
30 |
31 | public function dispatch ()
32 | {
33 | $routerFactory = new RouterFactory;
34 | $router = $routerFactory->newInstance();
35 |
36 | $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
37 |
38 | $this->dispatchedRoute = $this->router->match($path, $_SERVER);
39 | }
40 |
41 | public function getController ()
42 | {
43 | return explode("::", $this->dispatchedRoute->name);
44 | }
45 |
46 | public function getParams ()
47 | {
48 | return $this->dispatchedRoute->params;
49 | }
50 |
51 | public function found ()
52 | {
53 | return $this->router->getFailedRoute() === null;
54 | }
55 |
56 | public function notFound ()
57 | {
58 | $failedRoute = $this->router->getFailedRoute();
59 |
60 | if ($failedRoute === null) {
61 | return false;
62 | }
63 |
64 | return $failedRoute->failed === Route::FAILED_ROUTABLE || $failedRoute->failed === Route::FAILED_REGEX;
65 | }
66 |
67 | public function methodNotAllowed ()
68 | {
69 | $failedRoute = $this->router->getFailedRoute();
70 |
71 | if ($failedRoute === null) {
72 | return false;
73 | }
74 |
75 | return $failedRoute->failed === Route::FAILED_METHOD;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/composer.lock:
--------------------------------------------------------------------------------
1 | {
2 | "_readme": [
3 | "This file locks the dependencies of your project to a known state",
4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 | "This file is @generated automatically"
6 | ],
7 | "hash": "b89c4f9791e14eedd14602c6169e2d6b",
8 | "packages": [
9 | {
10 | "name": "aura/router",
11 | "version": "2.3.0",
12 | "source": {
13 | "type": "git",
14 | "url": "https://github.com/auraphp/Aura.Router.git",
15 | "reference": "457efd185e4306fa671d659a66a2d9d28bf91a56"
16 | },
17 | "dist": {
18 | "type": "zip",
19 | "url": "https://api.github.com/repos/auraphp/Aura.Router/zipball/457efd185e4306fa671d659a66a2d9d28bf91a56",
20 | "reference": "457efd185e4306fa671d659a66a2d9d28bf91a56",
21 | "shasum": ""
22 | },
23 | "require": {
24 | "php": ">=5.3.0"
25 | },
26 | "require-dev": {
27 | "aura/di": "~2.0"
28 | },
29 | "type": "library",
30 | "extra": {
31 | "aura": {
32 | "type": "library",
33 | "config": {
34 | "common": "Aura\\Router\\_Config\\Common"
35 | }
36 | }
37 | },
38 | "autoload": {
39 | "psr-4": {
40 | "Aura\\Router\\": "src/",
41 | "Aura\\Router\\_Config\\": "config/"
42 | }
43 | },
44 | "notification-url": "https://packagist.org/downloads/",
45 | "license": [
46 | "BSD-2-Clause"
47 | ],
48 | "authors": [
49 | {
50 | "name": "Aura.Router Contributors",
51 | "homepage": "https://github.com/auraphp/Aura.Router/contributors"
52 | }
53 | ],
54 | "description": "A web router implementation; given a URI path and a copy of $_SERVER, it will extract path-info parameter values for a specific route.",
55 | "homepage": "https://github.com/auraphp/Aura.Router",
56 | "keywords": [
57 | "route",
58 | "router",
59 | "routing"
60 | ],
61 | "time": "2015-07-17 15:55:37"
62 | },
63 | {
64 | "name": "container-interop/container-interop",
65 | "version": "1.1.0",
66 | "source": {
67 | "type": "git",
68 | "url": "https://github.com/container-interop/container-interop.git",
69 | "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e"
70 | },
71 | "dist": {
72 | "type": "zip",
73 | "url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e",
74 | "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e",
75 | "shasum": ""
76 | },
77 | "type": "library",
78 | "autoload": {
79 | "psr-4": {
80 | "Interop\\Container\\": "src/Interop/Container/"
81 | }
82 | },
83 | "notification-url": "https://packagist.org/downloads/",
84 | "license": [
85 | "MIT"
86 | ],
87 | "description": "Promoting the interoperability of container objects (DIC, SL, etc.)",
88 | "time": "2014-12-30 15:22:37"
89 | },
90 | {
91 | "name": "nikic/fast-route",
92 | "version": "v0.6.0",
93 | "source": {
94 | "type": "git",
95 | "url": "https://github.com/nikic/FastRoute.git",
96 | "reference": "31fa86924556b80735f98b294a7ffdfb26789f22"
97 | },
98 | "dist": {
99 | "type": "zip",
100 | "url": "https://api.github.com/repos/nikic/FastRoute/zipball/31fa86924556b80735f98b294a7ffdfb26789f22",
101 | "reference": "31fa86924556b80735f98b294a7ffdfb26789f22",
102 | "shasum": ""
103 | },
104 | "require": {
105 | "php": ">=5.4.0"
106 | },
107 | "type": "library",
108 | "autoload": {
109 | "psr-4": {
110 | "FastRoute\\": "src/"
111 | },
112 | "files": [
113 | "src/functions.php"
114 | ]
115 | },
116 | "notification-url": "https://packagist.org/downloads/",
117 | "license": [
118 | "BSD-3-Clause"
119 | ],
120 | "authors": [
121 | {
122 | "name": "Nikita Popov",
123 | "email": "nikic@php.net"
124 | }
125 | ],
126 | "description": "Fast request router for PHP",
127 | "keywords": [
128 | "router",
129 | "routing"
130 | ],
131 | "time": "2015-06-18 19:15:47"
132 | },
133 | {
134 | "name": "php-di/invoker",
135 | "version": "1.0.0",
136 | "source": {
137 | "type": "git",
138 | "url": "https://github.com/PHP-DI/Invoker.git",
139 | "reference": "7ea703c62dbb29d64763fa85258826034ce3c97d"
140 | },
141 | "dist": {
142 | "type": "zip",
143 | "url": "https://api.github.com/repos/PHP-DI/Invoker/zipball/7ea703c62dbb29d64763fa85258826034ce3c97d",
144 | "reference": "7ea703c62dbb29d64763fa85258826034ce3c97d",
145 | "shasum": ""
146 | },
147 | "require": {
148 | "container-interop/container-interop": "~1.1"
149 | },
150 | "require-dev": {
151 | "athletic/athletic": "~0.1.8",
152 | "phpunit/phpunit": "~4.5"
153 | },
154 | "type": "library",
155 | "autoload": {
156 | "psr-4": {
157 | "Invoker\\": "src/"
158 | }
159 | },
160 | "notification-url": "https://packagist.org/downloads/",
161 | "license": [
162 | "MIT"
163 | ],
164 | "description": "Generic and extensible callable invoker",
165 | "homepage": "https://github.com/PHP-DI/Invoker",
166 | "keywords": [
167 | "callable",
168 | "dependency",
169 | "dependency-injection",
170 | "injection",
171 | "invoke",
172 | "invoker"
173 | ],
174 | "time": "2015-04-24 10:18:34"
175 | },
176 | {
177 | "name": "php-di/php-di",
178 | "version": "5.0.3",
179 | "source": {
180 | "type": "git",
181 | "url": "https://github.com/PHP-DI/PHP-DI.git",
182 | "reference": "c59af31dcf2ee6246e28464e775599321df40955"
183 | },
184 | "dist": {
185 | "type": "zip",
186 | "url": "https://api.github.com/repos/PHP-DI/PHP-DI/zipball/c59af31dcf2ee6246e28464e775599321df40955",
187 | "reference": "c59af31dcf2ee6246e28464e775599321df40955",
188 | "shasum": ""
189 | },
190 | "require": {
191 | "container-interop/container-interop": "~1.0",
192 | "php": ">=5.4.0",
193 | "php-di/invoker": "~1.0",
194 | "php-di/phpdoc-reader": "~2.0"
195 | },
196 | "replace": {
197 | "mnapoli/php-di": "*"
198 | },
199 | "require-dev": {
200 | "doctrine/annotations": "~1.2",
201 | "doctrine/cache": "~1.0",
202 | "mnapoli/phpunit-easymock": "~0.1.4",
203 | "ocramius/proxy-manager": "~1.0",
204 | "phpunit/phpunit": "~4.5"
205 | },
206 | "suggest": {
207 | "doctrine/annotations": "Install it if you want to use annotations (version ~1.2)",
208 | "doctrine/cache": "Install it if you want to use the cache (version ~1.0)",
209 | "ocramius/proxy-manager": "Install it if you want to use lazy injection (version ~1.0)"
210 | },
211 | "type": "library",
212 | "autoload": {
213 | "psr-4": {
214 | "DI\\": "src/DI/"
215 | },
216 | "files": [
217 | "src/DI/functions.php"
218 | ]
219 | },
220 | "notification-url": "https://packagist.org/downloads/",
221 | "license": [
222 | "MIT"
223 | ],
224 | "description": "The dependency injection container for humans",
225 | "homepage": "http://php-di.org/",
226 | "keywords": [
227 | "container",
228 | "dependency injection",
229 | "di"
230 | ],
231 | "time": "2015-08-12 13:46:41"
232 | },
233 | {
234 | "name": "php-di/phpdoc-reader",
235 | "version": "2.0.0",
236 | "source": {
237 | "type": "git",
238 | "url": "https://github.com/PHP-DI/PhpDocReader.git",
239 | "reference": "21dce5e29f640d655e7b4583ecfb7d166127a5da"
240 | },
241 | "dist": {
242 | "type": "zip",
243 | "url": "https://api.github.com/repos/PHP-DI/PhpDocReader/zipball/21dce5e29f640d655e7b4583ecfb7d166127a5da",
244 | "reference": "21dce5e29f640d655e7b4583ecfb7d166127a5da",
245 | "shasum": ""
246 | },
247 | "require": {
248 | "php": ">=5.3.0"
249 | },
250 | "require-dev": {
251 | "phpunit/phpunit": "~4.6"
252 | },
253 | "type": "library",
254 | "autoload": {
255 | "psr-4": {
256 | "PhpDocReader\\": "src/PhpDocReader"
257 | }
258 | },
259 | "notification-url": "https://packagist.org/downloads/",
260 | "license": [
261 | "MIT"
262 | ],
263 | "description": "PhpDocReader parses @var and @param values in PHP docblocks (supports namespaced class names with the same resolution rules as PHP)",
264 | "keywords": [
265 | "phpdoc",
266 | "reflection"
267 | ],
268 | "time": "2015-06-01 14:23:20"
269 | }
270 | ],
271 | "packages-dev": [],
272 | "aliases": [],
273 | "minimum-stability": "stable",
274 | "stability-flags": [],
275 | "prefer-stable": false,
276 | "prefer-lowest": false,
277 | "platform": [],
278 | "platform-dev": []
279 | }
280 |
--------------------------------------------------------------------------------