1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Server; |
4
|
|
|
|
5
|
|
|
use __\__; |
6
|
|
|
use allejo\stakx\Document\BasePageView; |
7
|
|
|
use allejo\stakx\Document\DynamicPageView; |
8
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
9
|
|
|
use allejo\stakx\Document\StaticPageView; |
10
|
|
|
use allejo\stakx\FrontMatter\FrontMatterParser; |
11
|
|
|
|
12
|
|
|
class PageViewRouter |
13
|
|
|
{ |
14
|
|
|
private $mapping; |
15
|
|
|
private $redirects; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->mapping = []; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Register a PageView to this router. |
24
|
|
|
*/ |
25
|
|
|
public function registerPageView(BasePageView $pageView) |
26
|
|
|
{ |
27
|
|
|
switch ($pageView->getType()) |
28
|
|
|
{ |
29
|
|
|
case BasePageView::STATIC_TYPE: |
30
|
|
|
$this->mapping[$pageView->getPermalink()] = $pageView; |
31
|
|
|
|
32
|
|
|
foreach ($pageView->getRedirects() as $redirect) |
33
|
|
|
{ |
34
|
|
|
$this->redirects[$redirect] = $pageView->getPermalink(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
break; |
38
|
|
|
|
39
|
|
|
case BasePageView::DYNAMIC_TYPE: |
40
|
|
|
case BasePageView::REPEATER_TYPE: |
41
|
|
|
$rawFM = $pageView->getRawFrontMatter(); |
42
|
|
|
$permalinkFM = \__::get($rawFM, 'permalink'); |
43
|
|
|
|
44
|
|
|
$permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM; |
45
|
|
|
$permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink); |
46
|
|
|
$permalink = preg_replace(FrontMatterParser::ARRAY_DEF, '{$1}', $permalink); |
47
|
|
|
|
48
|
|
|
$this->mapping[$permalink] = $pageView; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the PageView used for a specified route. |
55
|
|
|
* |
56
|
|
|
* @param string $route |
57
|
|
|
* |
58
|
|
|
* @return StaticPageView|DynamicPageView|RepeaterPageView|null |
59
|
|
|
*/ |
60
|
|
|
public function getRoute($route) |
61
|
|
|
{ |
62
|
|
|
return __::get($this->mapping, $route); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve all of the URL routes registered. |
67
|
|
|
* |
68
|
|
|
* @return string[] |
|
|
|
|
69
|
|
|
*/ |
70
|
|
|
public function getRoutes() |
71
|
|
|
{ |
72
|
|
|
return array_keys($this->mapping); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function &getRouteMapping() |
76
|
|
|
{ |
77
|
|
|
return $this->mapping; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function extractUrlParameters($permalink) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$matches = []; |
83
|
|
|
|
84
|
|
|
preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches); |
85
|
|
|
|
86
|
|
|
return $matches[1]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.