|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LBHurtado\Missive\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Opis\Pattern\RegexBuilder; |
|
7
|
|
|
use LBHurtado\Missive\Missive; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use LBHurtado\Missive\Classes\SMSAbstract; |
|
10
|
|
|
|
|
11
|
|
|
class Router |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var \Opis\Pattern\RegexBuilder */ |
|
14
|
|
|
protected $builder; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected $routes = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var \LBHurtado\Missive\Missive */ |
|
20
|
|
|
public $missive; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ContainerInterface */ |
|
23
|
|
|
protected $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Router constructor. |
|
27
|
|
|
* Capture the Missive instance from the container. |
|
28
|
|
|
* Instantiate RegexBuilder that ignores casing. |
|
29
|
|
|
* @param Missive $missive |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Missive $missive) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->missive = $missive; |
|
34
|
|
|
|
|
35
|
|
|
$this->builder = new RegexBuilder([ |
|
36
|
|
|
RegexBuilder::REGEX_MODIFIER => 'i' |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $route |
|
42
|
|
|
* @param string|callable $action |
|
43
|
|
|
* @return Router |
|
44
|
|
|
*/ |
|
45
|
|
|
public function register(string $route, $action): self |
|
46
|
|
|
{ |
|
47
|
|
|
$regex = $this->builder->getRegex($route); |
|
48
|
|
|
$callable = $this->getCallable($action); |
|
49
|
|
|
$this->routes[$regex] = $callable; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets the SMS property of Missive before |
|
56
|
|
|
* executing the contents of them sms |
|
57
|
|
|
* @param SMSAbstract $sms |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function process(SMSAbstract $sms) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->missive->setSMS($sms); |
|
63
|
|
|
|
|
64
|
|
|
return $this->execute($this->missive->getSMS()->getMessage()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $path |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public function execute(string $path) |
|
72
|
|
|
{ |
|
73
|
|
|
$ordered_routes = array_reverse($this->routes, true); |
|
74
|
|
|
foreach ($ordered_routes as $regex => $action) { |
|
75
|
|
|
if ($this->builder->matches($regex, $path)) { |
|
76
|
|
|
$values = $this->builder->getValues($regex, $path); |
|
77
|
|
|
$data = $action($path, $values); |
|
78
|
|
|
if ($data === false) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $data; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Make an action for an invokable controller. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $action |
|
93
|
|
|
* @return string |
|
94
|
|
|
* @throws UnexpectedValueException |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function makeInvokableAction($action) |
|
97
|
|
|
{ |
|
98
|
|
|
if (! method_exists($action, '__invoke')) { |
|
99
|
|
|
throw new UnexpectedValueException(sprintf( |
|
100
|
|
|
'Invalid hears action: [%s]', $action |
|
101
|
|
|
)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $action.'@__invoke'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $callback |
|
109
|
|
|
* @return array|string|Closure |
|
110
|
|
|
* @throws UnexpectedValueException |
|
111
|
|
|
* @throws NotFoundExceptionInterface |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getCallable($callback) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($callback instanceof Closure) { |
|
116
|
|
|
return $callback; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (\is_array($callback)) { |
|
120
|
|
|
return $callback; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (\is_object($callback)) { |
|
124
|
|
|
return $callback; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (strpos($callback, '@') === false) { |
|
128
|
|
|
$callback = $this->makeInvokableAction($callback); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
list($class, $method) = explode('@', $callback); |
|
132
|
|
|
|
|
133
|
|
|
$command = $this->container ? $this->container->get($class) : new $class($this); |
|
134
|
|
|
|
|
135
|
|
|
return [$command, $method]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param ContainerInterface $container |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setContainer(ContainerInterface $container) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->container = $container; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|