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 $redirects; |
15
|
|
|
private $baseUrl; |
16
|
|
|
private $mapping; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->mapping = []; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getBaseUrl() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
return $this->baseUrl; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setBaseUrl($baseUrl) |
29
|
|
|
{ |
30
|
|
|
$this->baseUrl = $baseUrl; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register a PageView to this router. |
35
|
|
|
*/ |
36
|
|
|
public function registerPageView(BasePageView $pageView) |
37
|
|
|
{ |
38
|
|
|
switch ($pageView->getType()) |
39
|
|
|
{ |
40
|
|
|
case BasePageView::STATIC_TYPE: |
41
|
|
|
$this->mapping[$pageView->getPermalink()] = $pageView; |
42
|
|
|
|
43
|
|
|
foreach ($pageView->getRedirects() as $redirect) |
44
|
|
|
{ |
45
|
|
|
$this->redirects[$redirect] = $pageView->getPermalink(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
|
50
|
|
|
case BasePageView::DYNAMIC_TYPE: |
51
|
|
|
case BasePageView::REPEATER_TYPE: |
52
|
|
|
$rawFM = $pageView->getRawFrontMatter(); |
53
|
|
|
$permalinkFM = \__::get($rawFM, 'permalink'); |
54
|
|
|
|
55
|
|
|
$permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM; |
56
|
|
|
$permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink); |
57
|
|
|
$permalink = preg_replace(FrontMatterParser::ARRAY_DEF, '{$1}', $permalink); |
58
|
|
|
|
59
|
|
|
$this->mapping[$permalink] = $pageView; |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the PageView used for a specified route. |
66
|
|
|
* |
67
|
|
|
* @param string $route |
68
|
|
|
* |
69
|
|
|
* @return StaticPageView|DynamicPageView|RepeaterPageView|null |
70
|
|
|
*/ |
71
|
|
|
public function getRoute($route) |
72
|
|
|
{ |
73
|
|
|
return __::get($this->mapping, $route); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieve all of the URL routes registered. |
78
|
|
|
* |
79
|
|
|
* @return string[] |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function getRoutes() |
82
|
|
|
{ |
83
|
|
|
return array_keys($this->mapping); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function &getRouteMapping() |
87
|
|
|
{ |
88
|
|
|
return $this->mapping; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function extractUrlParameters($permalink) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$matches = []; |
94
|
|
|
|
95
|
|
|
preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches); |
96
|
|
|
|
97
|
|
|
return $matches[1]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.