-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
68 lines (49 loc) · 1.84 KB
/
Application.php
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
<?php
namespace Router\Demo\Core;
use Routing\Attribute\Route;
use Routing\Core\Router;
require_once('Router.php');
class Application
{
public function __construct($controllerDir = 'Controller', $namespace = 'Routing\\')
{
$this->prepareControllerRoutes($controllerDir, $namespace);
}
public function prepareControllerRoutes($controllerDir, $namespace)
{
$controllerLoc = dirname(__DIR__) . '/' . $controllerDir;
$controllers = array_diff(scandir($controllerLoc), ['.', '..']);
foreach ($controllers as $controller) {
require_once $controllerLoc . '/' . $controller;
$controllerName = pathinfo($controller, PATHINFO_FILENAME);
$reflectionClass = new \ReflectionClass($namespace . $controllerDir . '\\' . $controllerName);
$rcAttr = $reflectionClass->getAttributes(Route::class);
$prefix = '';
if(count($rcAttr) > 0) {
$prefix = ($rcAttr[0]->newInstance())->getPath();
$prefix = '/'.trim($prefix, '/');
}
foreach ($reflectionClass->getMethods() as $method) {
$attributes = $method->getAttributes(Route::class);
foreach ($attributes as $attribute) {
$route = $attribute->newInstance();
$path = $prefix.'/'.trim($route->getPath(), '/');
if($path == '') {
$path = '/';
}
Router::register(
$path,
[$reflectionClass->getName(), $method->getName()],
$route->getMethod()
);
}
}
unset($rcAttr);
unset($prefix);
}
}
public function resolve()
{
Router::run();
}
}