|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author jan huang <[email protected]> |
|
4
|
|
|
* @copyright 2016 |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://www.github.com/janhuang |
|
7
|
|
|
* @see http://www.fast-d.cn/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FastD\ServiceProvider; |
|
11
|
|
|
|
|
12
|
|
|
use FastD\Container\Container; |
|
13
|
|
|
use FastD\Container\ServiceProviderInterface; |
|
14
|
|
|
use FastD\Routing\RouteCollection; |
|
15
|
|
|
use FastD\Routing\RouteDispatcher; |
|
16
|
|
|
|
|
17
|
|
|
class Router extends RouteCollection |
|
18
|
|
|
{ |
|
19
|
60 |
|
protected function concat($callback) |
|
20
|
|
|
{ |
|
21
|
60 |
|
if (is_string($callback)) { |
|
22
|
60 |
|
return '\\Controller\\'.$callback; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return $callback; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
60 |
|
public function get($path, $callback, array $defaults = []) |
|
29
|
|
|
{ |
|
30
|
60 |
|
return parent::get($path, $this->concat($callback), $defaults); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
60 |
|
public function post($path, $callback, array $defaults = []) |
|
34
|
|
|
{ |
|
35
|
60 |
|
return parent::post($path, $this->concat($callback), $defaults); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function patch($path, $callback, array $defaults = []) |
|
39
|
|
|
{ |
|
40
|
|
|
return parent::patch($path, $this->concat($callback), $defaults); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function put($path, $callback, array $defaults = []) |
|
44
|
|
|
{ |
|
45
|
|
|
return parent::put($path, $this->concat($callback), $defaults); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function delete($path, $callback, array $defaults = []) |
|
49
|
|
|
{ |
|
50
|
|
|
return parent::delete($path, $this->concat($callback), $defaults); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function head($path, $callback, array $defaults = []) |
|
54
|
|
|
{ |
|
55
|
|
|
return parent::head($path, $this->concat($callback), $defaults); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Class RouteServiceProvider. |
|
61
|
|
|
*/ |
|
62
|
|
|
class RouteServiceProvider implements ServiceProviderInterface |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* @param Container $container |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
60 |
|
public function register(Container $container) |
|
70
|
|
|
{ |
|
71
|
60 |
|
$router = new Router(); |
|
72
|
60 |
|
$dispatcher = new RouteDispatcher($router, $container['config']->get('middleware', [])); |
|
73
|
|
|
|
|
74
|
60 |
|
$container->add('router', $router); |
|
75
|
60 |
|
$container->add('dispatcher', $dispatcher); |
|
76
|
|
|
|
|
77
|
60 |
|
include app()->getPath().'/config/routes.php'; |
|
78
|
60 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.