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\Engines\Markdown\MarkdownEngine; |
11
|
|
|
use allejo\stakx\Engines\PlainTextEngine; |
12
|
|
|
use allejo\stakx\Engines\RST\RstEngine; |
13
|
|
|
use allejo\stakx\FrontMatter\FrontMatterDocument; |
14
|
|
|
use allejo\stakx\Manager\TwigManager; |
15
|
|
|
|
16
|
|
|
class ContentItem extends FrontMatterDocument implements \JsonSerializable, RepeatableItem |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The collection this Content Item belongs to. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $parentCollection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The Page View that will be used to render this Content Item. |
27
|
|
|
* |
28
|
|
|
* @var PageView |
29
|
|
|
*/ |
30
|
|
|
private $parentPageView; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
27 |
|
public function createJail() |
36
|
|
|
{ |
37
|
27 |
|
return new JailedDocument($this, array_merge(self::$whiteListFunctions, array( |
38
|
27 |
|
'getCollection', 'isDraft' |
39
|
27 |
|
)), array('getPageView' => 'getJailedPageView')); |
40
|
|
|
} |
41
|
|
|
|
42
|
21 |
|
public function getNamespace() |
43
|
|
|
{ |
44
|
21 |
|
return $this->parentCollection; |
45
|
|
|
} |
46
|
|
|
|
47
|
37 |
|
public function setNamespace($collection) |
48
|
|
|
{ |
49
|
37 |
|
$this->parentCollection = $collection; |
50
|
37 |
|
} |
51
|
|
|
|
52
|
3 |
|
public function handleSpecialRedirects() |
53
|
|
|
{ |
54
|
3 |
|
$fm = $this->getFrontMatter(); |
|
|
|
|
55
|
|
|
|
56
|
3 |
|
if (isset($fm['redirect_from'])) |
57
|
|
|
{ |
58
|
|
|
$redirects = $fm['redirect_from']; |
59
|
|
|
|
60
|
|
|
if (!is_array($redirects)) |
61
|
|
|
{ |
62
|
|
|
$redirects = array($redirects); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->redirects = array_merge($this->redirects, $redirects); |
66
|
|
|
} |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the body of the Content Item parsed as markdown. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
7 |
|
public function getContent() |
75
|
|
|
{ |
76
|
7 |
|
if (!$this->bodyContentEvaluated) |
77
|
|
|
{ |
78
|
7 |
|
$this->parseTwig(); |
79
|
7 |
|
$this->parseEngines(); |
80
|
|
|
|
81
|
7 |
|
$this->bodyContentEvaluated = true; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
return (string)$this->bodyContent; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parse the Twig that is embedded inside a ContentItem's body. |
89
|
|
|
*/ |
90
|
7 |
|
private function parseTwig() |
91
|
|
|
{ |
92
|
7 |
|
$twig = TwigManager::getInstance(); |
93
|
|
|
|
94
|
7 |
|
if ($twig instanceof \Twig_Environment) |
95
|
|
|
{ |
96
|
7 |
|
$template = $twig->createTemplate($this->bodyContent); |
97
|
7 |
|
$this->bodyContent = $template->render(array()); |
98
|
|
|
} |
99
|
7 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Parse the ContentItem's body based on the extension of the file. |
103
|
|
|
*/ |
104
|
7 |
|
private function parseEngines() |
105
|
|
|
{ |
106
|
7 |
|
switch ($this->getExtension()) |
107
|
|
|
{ |
108
|
7 |
|
case 'md': |
109
|
5 |
|
case 'markdown': |
110
|
2 |
|
$pd = new MarkdownEngine(); |
|
|
|
|
111
|
2 |
|
break; |
112
|
|
|
|
113
|
5 |
|
case 'rst': |
114
|
2 |
|
$pd = new RstEngine(); |
115
|
2 |
|
$pd->setIncludePolicy(true, getcwd()); |
116
|
2 |
|
break; |
117
|
|
|
|
118
|
|
|
default: |
119
|
3 |
|
$pd = new PlainTextEngine(); |
120
|
3 |
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
$this->bodyContent = $pd->parse($this->bodyContent); |
124
|
7 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return PageView |
128
|
|
|
*/ |
129
|
|
|
public function &getPageView() |
130
|
|
|
{ |
131
|
|
|
return $this->parentPageView; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getJailedPageView() |
135
|
|
|
{ |
136
|
|
|
return $this->parentPageView->createJail(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the parent Page View that this Content Item will have be assigned to. |
141
|
|
|
* |
142
|
|
|
* @param PageView $pageView |
143
|
|
|
*/ |
144
|
5 |
|
public function setParentPageView(PageView &$pageView) |
145
|
|
|
{ |
146
|
5 |
|
$this->parentPageView = &$pageView; |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function jsonSerialize() |
153
|
|
|
{ |
154
|
|
|
return array_merge($this->getFrontMatter(), array( |
155
|
|
|
'content' => $this->getContent(), |
156
|
|
|
'permalink' => $this->getPermalink(), |
157
|
|
|
'redirects' => $this->getRedirects(), |
158
|
|
|
)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.