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, \IteratorAggregate, TwigDocumentInterface |
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
|
|
|
/** |
53
|
|
|
* Return the body of the Content Item parsed as markdown. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
7 |
|
public function getContent() |
58
|
|
|
{ |
59
|
7 |
|
if (!$this->bodyContentEvaluated) |
60
|
7 |
|
{ |
61
|
7 |
|
$this->parseTwig(); |
62
|
7 |
|
$this->parseEngines(); |
63
|
|
|
|
64
|
7 |
|
$this->bodyContentEvaluated = true; |
65
|
7 |
|
} |
66
|
|
|
|
67
|
7 |
|
return (string)$this->bodyContent; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Parse the Twig that is embedded inside a ContentItem's body. |
72
|
|
|
*/ |
73
|
7 |
|
private function parseTwig() |
74
|
|
|
{ |
75
|
7 |
|
$twig = TwigManager::getInstance(); |
76
|
|
|
|
77
|
7 |
|
if ($twig instanceof \Twig_Environment) |
78
|
7 |
|
{ |
79
|
7 |
|
$template = $twig->createTemplate($this->bodyContent); |
80
|
7 |
|
$this->bodyContent = $template->render(array()); |
81
|
7 |
|
} |
82
|
7 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Parse the ContentItem's body based on the extension of the file. |
86
|
|
|
*/ |
87
|
7 |
|
private function parseEngines() |
88
|
|
|
{ |
89
|
7 |
|
switch ($this->getExtension()) |
90
|
|
|
{ |
91
|
7 |
|
case 'md': |
92
|
7 |
|
case 'markdown': |
93
|
2 |
|
$pd = new MarkdownEngine(); |
|
|
|
|
94
|
2 |
|
break; |
95
|
|
|
|
96
|
5 |
|
case 'rst': |
97
|
2 |
|
$pd = new RstEngine(); |
98
|
2 |
|
$pd->setIncludePolicy(true, getcwd()); |
99
|
2 |
|
break; |
100
|
|
|
|
101
|
3 |
|
default: |
102
|
3 |
|
$pd = new PlainTextEngine(); |
103
|
3 |
|
break; |
104
|
7 |
|
} |
105
|
|
|
|
106
|
7 |
|
$this->bodyContent = $pd->parse($this->bodyContent); |
107
|
7 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check whether a ContentItem should be treated as a draft |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
3 |
|
public function isDraft() |
115
|
|
|
{ |
116
|
3 |
|
return ($this['draft'] === true); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return PageView |
121
|
|
|
*/ |
122
|
|
|
public function &getPageView() |
123
|
|
|
{ |
124
|
|
|
return $this->parentPageView; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getJailedPageView() |
128
|
|
|
{ |
129
|
|
|
return $this->parentPageView->createJail(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the parent Page View that this Content Item will have be assigned to. |
134
|
|
|
* |
135
|
|
|
* @param PageView $pageView |
136
|
|
|
*/ |
137
|
5 |
|
public function setPageView(&$pageView) |
138
|
|
|
{ |
139
|
5 |
|
$this->parentPageView = &$pageView; |
140
|
5 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function jsonSerialize() |
146
|
|
|
{ |
147
|
|
|
return array_merge($this->getFrontMatter(), array( |
148
|
|
|
'content' => $this->getContent(), |
149
|
|
|
'permalink' => $this->getPermalink(), |
150
|
|
|
'redirects' => $this->getRedirects(), |
151
|
|
|
)); |
152
|
|
|
} |
153
|
|
|
|
154
|
19 |
|
public function getIterator() |
155
|
|
|
{ |
156
|
19 |
|
return (new \ArrayIterator($this->frontMatter)); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.