Repository navigation
Expand file tree
/
Copy pathCallbackRoute.hh
More file actions
74 lines (61 loc) · 1.77 KB
/
Copy pathCallbackRoute.hh
File metadata and controls
74 lines (61 loc) · 1.77 KB
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
69
70
71
72
73
74
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Route;
use Titon\Route\Exception\NoMatchException;
use ReflectionFunction;
/**
* The CallbackRoute works in a similar fashion to the default Route with the only difference being
* that a callback is used for dispatching instead of an action.
*
* @package Titon\Route
*/
class CallbackRoute extends Route {
/**
* The callback to execute during dispatch.
*
* @var \Titon\Route\RouteCallback
*/
protected RouteCallback $callback;
/**
* Store the tokenized URL to match and the callback to dispatch to.
*
* @param string $path
* @param \Titon\Route\RouteCallback $callback
*/
public function __construct(string $path, RouteCallback $callback) {
$this->callback = $callback;
parent::__construct($path, 'CallbackRoute@noop');
}
/**
* {@inheritdoc}
*/
public function dispatch(): mixed {
if (!$this->isMatched()) {
throw new NoMatchException('Route cannot be dispatched unless it has been matched');
}
$callback = new ReflectionFunction($this->getCallback());
return $callback->invokeArgs($this->getArguments($callback));
}
/**
* Return the callback function.
*
* @return \Titon\Route\RouteCallback
*/
public function getCallback(): RouteCallback {
return $this->callback;
}
/**
* Set the callback function.
*
* @param \Titon\Route\RouteCallback $callback
* @return $this
*/
public function setCallback(RouteCallback $callback): this {
$this->callback = $callback;
return $this;
}
}