1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Riclep\Storyblok\Traits\ProcessesBlocks; |
9
|
|
|
|
10
|
|
|
abstract class Page |
11
|
|
|
{ |
12
|
|
|
use ProcessesBlocks; |
13
|
|
|
|
14
|
|
|
private $processedJson; |
15
|
|
|
private $content; |
16
|
|
|
private $seo; |
17
|
|
|
protected $title; |
18
|
|
|
|
19
|
|
|
public function __construct($rawStory) |
20
|
|
|
{ |
21
|
|
|
$this->processedJson = $rawStory; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Performs any actions on the Storyblok content before it is parsed into Block classes |
26
|
|
|
* Move SEO plugin out of content to the root of the page’s response |
27
|
|
|
*/ |
28
|
|
|
public function preprocess() { |
29
|
|
|
if (array_key_exists('seo', $this->processedJson['content'])) { |
30
|
|
|
$this->processedJson['seo'] = $this->processedJson['content']['seo']; |
31
|
|
|
unset($this->processedJson['content']['seo']); |
32
|
|
|
|
33
|
|
|
$this->seo = $this->processedJson['seo']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Processes the page’s meta content |
42
|
|
|
*/ |
43
|
|
|
public function process() { |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Perform actions on the data after all blocks have been prepared |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function postProcess() |
53
|
|
|
{ |
54
|
|
|
$this->content()->makeComponentPath([]); |
55
|
|
|
|
56
|
|
|
foreach (class_uses_recursive($this) as $trait) { |
57
|
|
|
if (method_exists($this, $method = 'init' . class_basename($trait))) { |
58
|
|
|
$this->{$method}(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getBlocks() { |
66
|
|
|
$this->content = $this->processBlock($this->processedJson['content'], 'root'); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function views() { |
75
|
|
|
$views = []; |
76
|
|
|
|
77
|
|
|
//$viewFile = strtolower(subStr((new \ReflectionClass($this))->getShortName(), 0, -4)); |
78
|
|
|
|
79
|
|
|
$segments = explode('/', rtrim($this->slug(), '/')); |
80
|
|
|
|
81
|
|
|
// match full path first |
82
|
|
|
$views[] = config('storyblok.view_path') . 'pages.' . implode('.', $segments); |
83
|
|
|
|
84
|
|
|
// creates an array of dot paths for each path segment |
85
|
|
|
// site.com/this/that/them becomes: |
86
|
|
|
// this.that.them |
87
|
|
|
// this.that |
88
|
|
|
// this |
89
|
|
|
while (count($segments) >= 1) { |
90
|
|
|
if (!in_array($path = config('storyblok.view_path') . 'pages.' . implode('.', $segments), $views)) { |
91
|
|
|
$views[] = config('storyblok.view_path') . 'pages.' . implode('.', $segments) . '.' . $this->content()->component(); |
92
|
|
|
$views[] = $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
array_pop($segments); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!in_array($path = config('storyblok.view_path') . 'pages.' . $this->content()->component(), $views)) { |
99
|
|
|
$views[] = config('storyblok.view_path') . 'pages.' . $this->content()->component(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$views[] = config('storyblok.view_path') . 'pages.default'; |
103
|
|
|
|
104
|
|
|
return $views; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
public function view() { |
109
|
|
|
foreach ($this->views() as $view) { |
110
|
|
|
if (view()->exists($view)) { |
111
|
|
|
return $view; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Reads the story |
118
|
|
|
* |
119
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
120
|
|
|
*/ |
121
|
|
|
public function render($additionalContent = null) { |
122
|
|
|
$content = [ |
123
|
|
|
'title' => $this->title(), |
124
|
|
|
'meta_description' => $this->metaDescription(), |
125
|
|
|
'story' => $this->content(), |
126
|
|
|
'seo' => $this->seo, |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
if ($additionalContent) { |
130
|
|
|
$content = array_merge($content, $additionalContent); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return view($this->view(), $content); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function title() { |
137
|
|
|
if ($this->seo) { |
138
|
|
|
return $this->seo['title']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->processedJson['name']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function metaDescription() { |
145
|
|
|
if ($this->seo) { |
146
|
|
|
return $this->seo['description']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return config('seo.default-description'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function content() { |
153
|
|
|
return $this->content; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function slug() |
157
|
|
|
{ |
158
|
|
|
return $this->processedJson['full_slug']; |
159
|
|
|
} |
160
|
|
|
} |