|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: thiago |
|
5
|
|
|
* Date: 31/03/18 |
|
6
|
|
|
* Time: 11:53 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
require_once __DIR__ . '/bootstrap.php'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Simple handmade router and dispatcher :) |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { |
|
16
|
|
|
$r->addRoute('GET', '/', \MyWonderland\Controller\HomeController::class . ':index'); |
|
17
|
|
|
$r->addRoute('GET', '/report', \MyWonderland\Controller\HomeController::class . ':report'); |
|
18
|
|
|
$r->addRoute('GET', '/auth', \MyWonderland\Controller\SpotifyAuthController::class . ':auth'); |
|
19
|
|
|
$r->addRoute('GET', '/callback', \MyWonderland\Controller\SpotifyAuthController::class . ':callback'); |
|
20
|
|
|
$r->addRoute('GET', '/logout', \MyWonderland\Controller\SpotifyAuthController::class . ':logout'); |
|
21
|
|
|
}); |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$routeInfo = $dispatcher->dispatch( |
|
25
|
|
|
$_SERVER['REQUEST_METHOD'], |
|
26
|
|
|
rawurldecode(extractAction($_SERVER['REQUEST_URI'])) |
|
27
|
|
|
); |
|
28
|
|
|
callController($routeInfo, $_SERVER['QUERY_STRING']); |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $uri Ex.: /callback?code=bli&test=3 |
|
33
|
|
|
* @return bool|string Ex.? /callback |
|
34
|
|
|
*/ |
|
35
|
|
|
function extractAction($uri) { |
|
36
|
|
|
if (false !== $pos = strpos($uri, '?')) { |
|
37
|
|
|
return substr($uri, 0, $pos); |
|
38
|
|
|
} |
|
39
|
|
|
return $uri; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $class |
|
45
|
|
|
* @param $method |
|
46
|
|
|
* @param $allVars |
|
47
|
|
|
* @return array |
|
48
|
|
|
* @throws Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
function sortParams($class, $method, $allVars) |
|
51
|
|
|
{ |
|
52
|
|
|
$params = (new ReflectionMethod($class, $method))->getParameters(); |
|
53
|
|
|
$sortedVars = []; |
|
54
|
|
|
foreach ($params as $param) { |
|
55
|
|
|
//$param is an instance of ReflectionParameter |
|
56
|
|
|
if (isset($allVars[$param->getName()])) { |
|
57
|
|
|
$sortedVars[$param->getName()] = $allVars[$param->getName()]; |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
if ($param->isOptional()) { |
|
61
|
|
|
$sortedVars[$param->getName()] = $param->getDefaultValue(); |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
throw new \Exception("Missed the '{$param->getName()}'' param.", 1522882396); |
|
65
|
|
|
} |
|
66
|
|
|
return $sortedVars; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $routeInfo |
|
72
|
|
|
* @param $queryString |
|
73
|
|
|
*/ |
|
74
|
|
|
function callController($routeInfo, $queryString) |
|
75
|
|
|
{ |
|
76
|
|
|
switch ($routeInfo[0]) { |
|
77
|
|
|
case FastRoute\Dispatcher::NOT_FOUND: |
|
78
|
|
|
print "404 Not Found"; |
|
79
|
|
|
break; |
|
80
|
|
|
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: |
|
81
|
|
|
print "405 Not Allowed"; |
|
82
|
|
|
break; |
|
83
|
|
|
case FastRoute\Dispatcher::FOUND: |
|
84
|
|
|
$handler = $routeInfo[1]; |
|
85
|
|
|
$vars = $routeInfo[2]; |
|
86
|
|
|
list($class, $method) = explode(":", $handler, 2); |
|
87
|
|
|
$container = new \MyWonderland\Container(); |
|
88
|
|
|
|
|
89
|
|
|
parse_str($queryString, $queryStringVars); |
|
90
|
|
|
|
|
91
|
|
|
$sortedVars = sortParams($class, $method, $vars + $queryStringVars); |
|
92
|
|
|
|
|
93
|
|
|
call_user_func_array(array($container->build($class), $method), $sortedVars); |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
} |