1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Object; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\System\Filesystem; |
11
|
|
|
use allejo\stakx\System\StakxResource; |
12
|
|
|
use org\bovigo\vfs\vfsStream; |
13
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
class PageView extends FrontMatterObject |
17
|
|
|
{ |
18
|
|
|
const TEMPLATE = "---\n%s\n---\n\n%s"; |
19
|
|
|
|
20
|
|
|
const REPEATER_TYPE = 0; |
21
|
|
|
const DYNAMIC_TYPE = 1; |
22
|
|
|
const STATIC_TYPE = 2; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Filesystem |
26
|
|
|
*/ |
27
|
|
|
private static $fileSys; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var PageView[] |
36
|
|
|
*/ |
37
|
|
|
private $children; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var JailObject |
41
|
|
|
*/ |
42
|
|
|
private $jailInstance; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
27 |
|
public function __construct($filePath) |
48
|
|
|
{ |
49
|
27 |
|
parent::__construct($filePath); |
50
|
|
|
|
51
|
27 |
|
$this->children = array(); |
52
|
27 |
|
$this->type = self::STATIC_TYPE; |
|
|
|
|
53
|
27 |
|
} |
54
|
|
|
|
55
|
|
|
// |
56
|
|
|
// Twig Jail |
57
|
|
|
// ========= |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
21 |
|
public function createJail() |
63
|
|
|
{ |
64
|
21 |
|
if (is_null($this->jailInstance)) |
65
|
21 |
|
{ |
66
|
21 |
|
$this->jailInstance = (new JailObject($this, array_merge(self::$whiteListFunctions, array( |
67
|
21 |
|
'getUrl', |
68
|
21 |
|
)), array('getChildren' => 'getJailedChildren'))); |
69
|
21 |
|
} |
70
|
|
|
|
71
|
21 |
|
return $this->jailInstance; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function getJailedChildren() |
75
|
|
|
{ |
76
|
3 |
|
$children = $this->children; |
77
|
|
|
|
78
|
3 |
|
foreach ($children as &$child) |
79
|
|
|
{ |
80
|
3 |
|
$child = $child->createJail(); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
3 |
|
return $children; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// |
87
|
|
|
// Getters |
88
|
|
|
// ======= |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get child PageViews. |
92
|
|
|
* |
93
|
|
|
* A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of |
94
|
|
|
* `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent. |
95
|
|
|
* |
96
|
|
|
* @return PageView[] |
97
|
|
|
*/ |
98
|
3 |
|
public function &getChildren() |
99
|
|
|
{ |
100
|
3 |
|
return $this->children; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string Twig body |
105
|
|
|
*/ |
106
|
9 |
|
public function getContent() |
107
|
|
|
{ |
108
|
9 |
|
return $this->bodyContent; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the type of the PageView. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
15 |
|
public function getType() |
117
|
|
|
{ |
118
|
15 |
|
return $this->type; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* A fallback for the site menus that use the `url` field. |
123
|
|
|
* |
124
|
|
|
* @deprecated 0.1.0 |
125
|
|
|
* |
126
|
|
|
* @todo Remove this in the next major release |
|
|
|
|
127
|
|
|
*/ |
128
|
|
|
public function getUrl() |
129
|
|
|
{ |
130
|
|
|
return $this->getPermalink(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// |
134
|
|
|
// Factory |
135
|
|
|
// ======= |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create the appropriate object type when parsing a PageView. |
139
|
|
|
* |
140
|
|
|
* @param string $filePath The path to the file that will be parsed into a PageView |
141
|
|
|
* |
142
|
|
|
* @return DynamicPageView|PageView|RepeaterPageView |
143
|
|
|
*/ |
144
|
6 |
|
public static function create($filePath) |
145
|
|
|
{ |
146
|
6 |
|
$instance = new self($filePath); |
147
|
|
|
|
148
|
6 |
|
if (isset($instance->getFrontMatter(false)['collection'])) |
149
|
6 |
|
{ |
150
|
4 |
|
return new DynamicPageView($filePath); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
$instance->getFrontMatter(); |
154
|
|
|
|
155
|
2 |
|
if ($instance->hasExpandedFrontMatter()) |
156
|
2 |
|
{ |
157
|
|
|
return new RepeaterPageView($filePath); |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
return $instance; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// |
164
|
|
|
// Virtual PageViews |
165
|
|
|
// ================= |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Create a virtual PageView. |
169
|
|
|
* |
170
|
|
|
* @param array $frontMatter The Front Matter that this virtual PageView will have |
171
|
|
|
* @param string $body The body of the virtual PageView |
172
|
|
|
* |
173
|
|
|
* @return PageView |
174
|
|
|
*/ |
175
|
4 |
|
public static function createVirtual($frontMatter, $body) |
176
|
|
|
{ |
177
|
4 |
|
if (vfsStreamWrapper::getRoot() == null) |
178
|
4 |
|
{ |
179
|
|
|
vfsStream::setup(); |
180
|
|
|
} |
181
|
|
|
|
182
|
4 |
|
$redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid())); |
183
|
|
|
$redirectFile |
184
|
4 |
|
->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body)) |
185
|
4 |
|
->at(vfsStreamWrapper::getRoot()); |
186
|
|
|
|
187
|
4 |
|
return new self($redirectFile->url()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Create a virtual PageView to create redirect files. |
192
|
|
|
* |
193
|
|
|
* @param string $redirectFrom The URL that will be redirecting to the target location |
194
|
|
|
* @param string $redirectTo The URL of the destination |
195
|
|
|
* @param string|bool $redirectTemplate The path to the template |
196
|
|
|
* |
197
|
|
|
* @return PageView A virtual PageView with the redirection template |
198
|
|
|
*/ |
199
|
4 |
|
public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false) |
200
|
|
|
{ |
201
|
4 |
|
if (is_null(self::$fileSys)) |
202
|
4 |
|
{ |
203
|
1 |
|
self::$fileSys = new Filesystem(); |
204
|
1 |
|
} |
205
|
|
|
|
206
|
|
|
$frontMatter = array( |
207
|
4 |
|
'permalink' => $redirectFrom, |
208
|
4 |
|
'redirect' => $redirectTo, |
209
|
4 |
|
'menu' => false, |
210
|
4 |
|
); |
211
|
|
|
|
212
|
4 |
|
if (!$redirectTemplate || !self::$fileSys->exists(self::$fileSys->absolutePath($redirectTemplate))) |
213
|
4 |
|
{ |
214
|
4 |
|
$contentItemBody = StakxResource::getResource('redirect.html.twig'); |
215
|
4 |
|
} |
216
|
|
|
else |
217
|
|
|
{ |
218
|
|
|
$contentItemBody = file_get_contents(self::$fileSys->absolutePath($redirectTemplate)); |
219
|
|
|
} |
220
|
|
|
|
221
|
4 |
|
return self::createVirtual($frontMatter, $contentItemBody); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.