Skip to content

Commit e6aae43

Browse files
authored
Merge pull request #8 from blankphp/dev
Route 更新 支持路由组
2 parents 62a2596 + faf03cd commit e6aae43

10 files changed

Lines changed: 170 additions & 69 deletions

File tree

‎src/blankphp/Bootstrap/RegisterProvider.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ class RegisterProvider
1818
protected $providers = [
1919
];
2020

21-
public function getProviders(): void
21+
public function getProviders(): array
2222
{
23-
$this->providers = array_merge($this->providers, config('app.providers', []));
23+
return array_merge($this->providers, config('app.providers', []));
2424
}
2525

2626
public function register(Application $app): void
2727
{
28-
$this->getProviders();
28+
foreach ($this->getProviders() as $provider) {
29+
$app->make($provider);
30+
}
2931
}
3032

3133
public function boot(Application $app): void

‎src/blankphp/Container.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,19 @@ public function make($abstract, $parameters = [])
9595

9696
private function getInstances($abstract)
9797
{
98-
return $this->instances[$abstract] ?? null;
98+
if (isset($this->instances[$abstract])) {
99+
return $this->instances[$abstract];
100+
}
101+
$keys = $this->alice->getByKey($abstract);
102+
foreach ($keys as $item) {
103+
if (isset($this->instances[$item])) {
104+
$this->instances[$abstract] = $this->instances[$item];
105+
106+
return $this->instances[$item];
107+
}
108+
}
109+
110+
return null;
99111
}
100112

101113
private function getBinds($binds)

‎src/blankphp/Provider/MiddleWareProvider.php‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,4 @@
1212

1313
class MiddleWareProvider extends Provider
1414
{
15-
protected $namespace = 'App\Middleware';
16-
protected $middleware = [
17-
];
18-
protected $registerMiddleware;
19-
protected $groupMiddleware = [];
20-
21-
public function getMiddleware($group)
22-
{
23-
if (isset($this->groupMiddleware[$group])) {
24-
return $this->groupMiddleware[$group];
25-
}
26-
}
27-
28-
public function boot(): void
29-
{
30-
$this->app->signal('GroupMiddleware', $this->groupMiddleware);
31-
$this->app->signal('AliceMiddleware', $this->registerMiddleware);
32-
}
33-
34-
public function getAliceMiddleWare($alice): array
35-
{
36-
$temp = [];
37-
foreach ($alice as $item) {
38-
if (isset($this->registerMiddleware[$item])) {
39-
$temp[] = $this->registerMiddleware[$item];
40-
}
41-
}
42-
43-
return $temp;
44-
}
4515
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the /blankphp/framework.
5+
*
6+
* (c) 沉迷 <1136589038@qq.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled.
9+
*/
10+
11+
namespace BlankPhp\Route\Exception;
12+
13+
class ParameterException extends RouteErrorException
14+
{
15+
}

‎src/blankphp/Route/Route.php‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace BlankPhp\Route;
1212

1313
use BlankPhp\Exception\HttpException;
14+
use BlankPhp\Route\Exception\ParameterException;
1415

1516
class Route
1617
{
@@ -19,6 +20,8 @@ class Route
1920
*/
2021
private $routeRules;
2122

23+
private $attributes = [];
24+
2225
/**
2326
* @var string[]
2427
*/
@@ -48,15 +51,36 @@ public function match($uri, $method)
4851

4952
public function group(...$parameter): RuleGroup
5053
{
54+
if (count($parameter) < 1) {
55+
throw new ParameterException();
56+
}
5157
$set = array_shift($parameter);
52-
$group = new RuleGroup($set, $parameter);
5358

54-
return $this->routeRules->addGroup($group);
59+
return new RuleGroup($set, $parameter, $this);
5560
}
5661

5762
private function add($method, $url, $parameter = []): RouteRule
5863
{
59-
return $this->routeRules->add(new RouteRule($method, $url, $parameter));
64+
return $this->routeRules->add(new RouteRule($method, $url, $parameter, $this->getAttribute()));
65+
}
66+
67+
public function setAttributes($attribute)
68+
{
69+
$this->attributes[] = $attribute;
70+
}
71+
72+
public function getAttribute()
73+
{
74+
foreach ($this->attributes as $item) {
75+
return $item;
76+
}
77+
78+
return [];
79+
}
80+
81+
public function popAttributes()
82+
{
83+
array_pop($this->attributes);
6084
}
6185

6286
public function __call(string $name, array $arguments)

‎src/blankphp/Route/RouteCollection.php‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function pregUri($uri)
4242
{
4343
$this->parseRules();
4444
foreach ($this->item as $k => $v) {
45+
// todo 待改成路由树
4546
if (preg_match("#^$k$#", $uri, $match)) {
4647
return $v;
4748
}
@@ -68,13 +69,4 @@ public function add(RouteRule $routeRule): RouteRule
6869

6970
return $routeRule;
7071
}
71-
72-
public function addGroup(RuleGroup $ruleGroup): RuleGroup
73-
{
74-
foreach ($ruleGroup->getAll() as $item) {
75-
$this->addRules($item);
76-
}
77-
78-
return $ruleGroup;
79-
}
8072
}

‎src/blankphp/Route/RouteRule.php‎

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class RouteRule
2121

2222
private $name;
2323

24+
private $namespace = '';
25+
2426
private $method;
2527

2628
private $controller;
@@ -31,17 +33,27 @@ class RouteRule
3133

3234
private $as;
3335

34-
private $uses;
35-
3636
private $where;
3737

38-
public function __construct($method, $url, $parameter)
38+
private $prefix = '';
39+
40+
public function __construct($method, $url, $parameter, $attributes = [])
3941
{
42+
$this->setAttribute($attributes);
4043
$this->method = $method;
4144
$this->url = $url;
4245
$this->parse($parameter);
4346
}
4447

48+
private function setAttribute($attributes)
49+
{
50+
foreach ($attributes as $k => $v) {
51+
if (isset($this->{$k})) {
52+
$this->$k = $v;
53+
}
54+
}
55+
}
56+
4557
private function parse($parameter)
4658
{
4759
if (is_array($parameter)) {
@@ -56,9 +68,9 @@ private function parse($parameter)
5668
}
5769
throw new \RuntimeException('parameter error');
5870
}
59-
}
60-
if ($parameter instanceof \Closure) {
61-
$this->controller = $parameter;
71+
} else {
72+
// 控制器
73+
$this->controller($parameter);
6274
}
6375
}
6476

@@ -72,9 +84,9 @@ public function middleware(...$args)
7284
$this->middleware = array_merge($this->middleware, $args);
7385
}
7486

75-
public function uses(...$controller)
87+
public function uses($controller)
7688
{
77-
$this->uses = $controller;
89+
$this->controller($controller);
7890
}
7991

8092
public function where($name, $preg)
@@ -96,14 +108,38 @@ public function domain($domain)
96108
{
97109
}
98110

111+
public function controller($controller)
112+
{
113+
if ($controller instanceof \Closure) {
114+
$this->controller = $controller;
115+
} else {
116+
$this->controller = $this->makeController($controller);
117+
}
118+
}
119+
120+
private function makeController($controllerArr)
121+
{
122+
if (is_string($controllerArr)) {
123+
$controllerArr = explode('@', $controllerArr);
124+
}
125+
if (!empty($this->namespace)) {
126+
$controllerArr[0] = ltrim($this->namespace, '\\').'\\'.$controllerArr[0];
127+
}
128+
129+
return $controllerArr;
130+
}
131+
99132
private function parent()
100133
{
101134
}
102135

103-
public function getOther()
136+
public function getUrlParameter(): array
104137
{
105-
// 获取所有 attribute 放入数组
138+
return [];
139+
}
106140

141+
public function getOther()
142+
{
107143
return [
108144
'method' => $this->method,
109145
'controller' => $this->controller,
@@ -113,7 +149,11 @@ public function getOther()
113149

114150
public function getUrl()
115151
{
116-
return $this->url;
152+
if (empty($this->prefix)) {
153+
return '/'.trim($this->url, '/');
154+
}
155+
156+
return '/'.trim($this->prefix, '/').rtrim($this->url, '/');
117157
}
118158

119159
public function getController()

‎src/blankphp/Route/Router.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ public function prepareResponse($res): Response
4444
}
4545

4646
/**
47+
* @return mixed|void
48+
*
4749
* @throws \ReflectionException
4850
*/
49-
public function runController($controller)
51+
public function runController(RouteRule $route)
5052
{
51-
$urlVars = $this->getUrlVars();
53+
$controller = $route->getController();
5254
if (is_array($controller)) {
5355
$target = new $controller[0]();
54-
55-
return $target->{$controller[1]}($this->resolveClassMethodDependencies($urlVars, $controller[0], $controller[1]));
56+
// 路由参数通过路由解析获取
57+
return $target->{$controller[1]}(...$this->resolveClassMethodDependencies($route->getUrlParameter(), $controller[0], $controller[1]));
5658
}
5759
if ($controller instanceof \Closure) {
58-
return $controller($this->resolveClassMethodDependencies($urlVars, 'Closure', $controller));
60+
return $controller($this->resolveClassMethodDependencies($route->getUrlParameter(), 'Closure', $controller));
5961
}
60-
// 其他
6162
}
6263

6364
private function getUrlVars(): array
@@ -79,13 +80,12 @@ public function dispatcher($request)
7980
///寻找出request
8081
/** @var RouteRule $routeRule */
8182
$routeRule = $this->findRoute($request);
82-
$controller = $routeRule->getController();
8383

8484
return (new Pipe())
8585
->send($request)
8686
->through($this->translate($routeRule->middleware))
87-
->run(function () use ($controller) {
88-
return $this->prepareResponse($this->runController($controller));
87+
->run(function () use ($routeRule) {
88+
return $this->prepareResponse($this->runController($routeRule));
8989
});
9090
}
9191
}

‎src/blankphp/Route/RuleGroup.php‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,58 @@
1212

1313
class RuleGroup
1414
{
15-
public function __construct($parameter, $data)
15+
private $group = '';
16+
private $prefix = '';
17+
private $namespace = '';
18+
/** @var Route */
19+
private $route;
20+
21+
public function __construct($parameter, $data, $route)
1622
{
23+
$this->route = $route;
1724
}
1825

19-
public function getAll()
26+
public function group($name)
27+
{
28+
$this->group = $name;
29+
30+
return $this;
31+
}
32+
33+
public function prefix($name)
2034
{
35+
$this->prefix = $name;
36+
2137
return $this;
2238
}
39+
40+
public function namespace($name)
41+
{
42+
$this->namespace = $name;
43+
44+
return $this;
45+
}
46+
47+
public function file($file)
48+
{
49+
$this->route->setAttributes($this->getAll());
50+
$this->makeRequireFile($file)();
51+
$this->route->popAttributes();
52+
}
53+
54+
private function makeRequireFile($fileName): \Closure
55+
{
56+
return function () use ($fileName) {
57+
require $fileName;
58+
};
59+
}
60+
61+
public function getAll()
62+
{
63+
return [
64+
'namespace' => $this->namespace,
65+
'group' => $this->group,
66+
'prefix' => $this->prefix,
67+
];
68+
}
2369
}

0 commit comments

Comments
 (0)