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