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\Filesystem\File; |
11
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
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
|
|
|
abstract class BasePageView extends PermalinkFrontMatterDocument implements PermalinkDocument |
18
|
|
|
{ |
19
|
|
|
use TemplateEngineDependent; |
20
|
|
|
|
21
|
|
|
const REPEATER_TYPE = 'repeater'; |
22
|
|
|
const DYNAMIC_TYPE = 'dynamic'; |
23
|
|
|
const STATIC_TYPE = 'static'; |
24
|
|
|
|
25
|
|
|
protected $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the type of PageView. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
14 |
|
public function getType() |
33
|
|
|
{ |
34
|
14 |
|
return $this->type; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/// |
38
|
|
|
// Static utilities |
39
|
|
|
/// |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create the appropriate object type when parsing a PageView. |
43
|
|
|
* |
44
|
|
|
* @return DynamicPageView|StaticPageView|RepeaterPageView |
45
|
|
|
*/ |
46
|
14 |
|
public static function create(File $filePath, array $complexVariables = []) |
47
|
|
|
{ |
48
|
14 |
|
$instance = new StaticPageView($filePath); |
49
|
|
|
|
50
|
14 |
|
if (isset($instance->getRawFrontMatter()['collection']) || |
51
|
14 |
|
isset($instance->getRawFrontMatter()['dataset']) |
52
|
|
|
) { |
53
|
|
|
return new DynamicPageView($filePath); |
54
|
|
|
} |
55
|
|
|
|
56
|
14 |
|
$instance->evaluateFrontMatter([], $complexVariables); |
57
|
|
|
|
58
|
14 |
|
if ($instance->hasExpandedFrontMatter()) |
59
|
|
|
{ |
60
|
3 |
|
return new RepeaterPageView($filePath); |
61
|
|
|
} |
62
|
|
|
|
63
|
11 |
|
return $instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/// |
67
|
|
|
// Virtual PageViews |
68
|
|
|
/// |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a virtual PageView. |
72
|
|
|
* |
73
|
|
|
* @param array $frontMatter The Front Matter that this virtual PageView will have |
74
|
|
|
* @param string $body The body of the virtual PageView |
75
|
|
|
* |
76
|
|
|
* @return StaticPageView |
77
|
|
|
*/ |
78
|
2 |
|
public static function createVirtual($frontMatter, $body) |
79
|
|
|
{ |
80
|
2 |
|
if (vfsStreamWrapper::getRoot() == null) |
81
|
|
|
{ |
82
|
|
|
vfsStream::setup(); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$redirectFile = vfsStream::newFile(sprintf('redirect_%s.html.twig', uniqid())); |
86
|
|
|
$redirectFile |
87
|
2 |
|
->setContent(sprintf(self::TEMPLATE, Yaml::dump($frontMatter, 2), $body)) |
88
|
2 |
|
->at(vfsStreamWrapper::getRoot()); |
89
|
|
|
|
90
|
2 |
|
$file = new File($redirectFile->url()); |
91
|
|
|
|
92
|
2 |
|
return new StaticPageView($file); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a virtual PageView to create redirect files. |
97
|
|
|
* |
98
|
|
|
* @param string $redirectFrom The URL that will be redirecting to the target location |
99
|
|
|
* @param string $redirectTo The URL of the destination |
100
|
|
|
* @param string|bool $redirectTemplate The path to the template |
101
|
|
|
* |
102
|
|
|
* @return StaticPageView A virtual PageView with the redirection template |
103
|
|
|
*/ |
104
|
2 |
|
public static function createRedirect($redirectFrom, $redirectTo, $redirectTemplate = false) |
105
|
|
|
{ |
106
|
|
|
$frontMatter = array( |
107
|
2 |
|
'permalink' => $redirectFrom, |
108
|
2 |
|
'redirect' => $redirectTo, |
109
|
|
|
'menu' => false, |
110
|
|
|
); |
111
|
|
|
|
112
|
2 |
|
if (!$redirectTemplate || !fs::exists(fs::absolutePath($redirectTemplate))) |
|
|
|
|
113
|
|
|
{ |
114
|
2 |
|
$contentItemBody = StakxResource::getResource('redirect.html.twig'); |
115
|
|
|
} |
116
|
|
|
else |
117
|
|
|
{ |
118
|
|
|
$contentItemBody = file_get_contents(fs::absolutePath($redirectTemplate)); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return self::createVirtual($frontMatter, $contentItemBody); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.