1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Routing; |
24
|
|
|
|
25
|
|
|
use LogicException; |
26
|
|
|
use Syscodes\Support\Str; |
27
|
|
|
use Syscodes\Collections\Arr; |
28
|
|
|
use UnexpectedValueException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Solve the actions obtained from a route. |
32
|
|
|
* |
33
|
|
|
* @author Alexander Campo <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class RouteAction |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Parse the given action into an array. |
39
|
|
|
* |
40
|
|
|
* @param string|array $uri |
41
|
|
|
* @param mixed $action |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function parse($uri, $action) |
46
|
|
|
{ |
47
|
|
|
if (is_null($action)) { |
48
|
|
|
return static::usesAction($uri); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (is_callable($action, true)) { |
52
|
|
|
return ! is_array($action) ? ['uses' => $action] : [ |
53
|
|
|
'uses' => $action[0].'@'.$action[1], |
54
|
|
|
'controller' => $action[0].'@'.$action[1], |
55
|
|
|
]; |
56
|
|
|
} elseif ( ! isset($action['uses'])) { |
57
|
|
|
$action['uses'] = static::findClosureAction($action); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (is_string($action['uses']) && ! Str::contains($action['uses'], '@')) { |
61
|
|
|
$action['uses'] = static::callInvokable($action['uses']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $action; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get an action for a route that has no action. |
69
|
|
|
* |
70
|
|
|
* @param string $uri |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
* |
74
|
|
|
* @throws \LogicException |
75
|
|
|
*/ |
76
|
|
|
protected static function usesAction($uri) |
77
|
|
|
{ |
78
|
|
|
return ['uses' => function () use ($uri) { |
79
|
|
|
throw new LogicException(__('route.hasNoAction', ['uri' => $uri])); |
80
|
|
|
}]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Find the callable in an action array. |
85
|
|
|
* |
86
|
|
|
* @param array $action |
87
|
|
|
* |
88
|
|
|
* @return \Closure |
89
|
|
|
*/ |
90
|
|
|
protected static function findClosureAction(array $action) |
91
|
|
|
{ |
92
|
|
|
return Arr::first($action, function ($value, $key) { |
93
|
|
|
return is_callable($value) && is_numeric($key); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Call an action for an invokable controller. |
99
|
|
|
* @param string $action |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
* |
103
|
|
|
* @throws \UnexpectedValueException |
104
|
|
|
*/ |
105
|
|
|
protected static function callInvokable($action) |
106
|
|
|
{ |
107
|
|
|
if ( ! method_exists($action, '__invoke')) { |
108
|
|
|
throw new UnexpectedValueException("Invalid route action: [{$action}]."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $action.'@__invoke'; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |