1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vssl\Render; |
4
|
|
|
|
5
|
|
|
use Journey\Cache\CacheAdapterInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
|
9
|
|
|
class Resolver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The request object. |
13
|
|
|
* |
14
|
|
|
* @var \Psr\Http\Message\ServerRequestInterface |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Configuration array. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The page api object. |
27
|
|
|
* |
28
|
|
|
* @var \Vssl\Render\PageApi |
29
|
|
|
*/ |
30
|
|
|
protected $api; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initialize a new Resolver |
34
|
|
|
*/ |
35
|
11 |
|
public function __construct(ServerRequestInterface $request, $config = false) |
36
|
|
|
{ |
37
|
11 |
|
$this->request = $request; |
38
|
11 |
|
$this->config = is_array($config) ? static::config($config) : static::config(); |
39
|
11 |
|
if (!$this->config['cache'] instanceof CacheAdapterInterface) { |
40
|
1 |
|
throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.'); |
41
|
1 |
|
} |
42
|
11 |
|
$this->api = new PageApi($request, $this->config); |
43
|
11 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get a modified request message. |
47
|
|
|
* |
48
|
|
|
* @return \Psr\Http\Message\ServerRequestInterface |
49
|
|
|
*/ |
50
|
4 |
|
public function getRequest() |
51
|
|
|
{ |
52
|
4 |
|
return $this->request->withAttribute('vssl-page', $this->resolve()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get an instance of the PageApi class. |
57
|
|
|
* |
58
|
|
|
* @return \Vssl\Render\PageApi |
59
|
|
|
*/ |
60
|
3 |
|
public function getPageApi() |
61
|
|
|
{ |
62
|
3 |
|
return $this->api; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the configuration settings. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
1 |
|
public function getConfig() |
71
|
|
|
{ |
72
|
1 |
|
return $this->config; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the configured CacheAdapterInterface. |
77
|
|
|
* |
78
|
|
|
* @return \Journey\Cache\CacheAdapterInterface |
79
|
|
|
*/ |
80
|
1 |
|
public function getCache() |
81
|
|
|
{ |
82
|
1 |
|
return $this->config['cache']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Resolve the current page from WebStories. Returns a render able page or |
87
|
|
|
* false. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
4 |
|
public function resolve() |
92
|
|
|
{ |
93
|
4 |
|
$response = $this->api->getPage($this->request->getUri()->getPath()); |
94
|
4 |
|
if ($response && ($page = $this->decodePage($response))) { |
95
|
3 |
|
$status = $response->getStatusCode(); |
96
|
|
|
return [ |
97
|
3 |
|
'status' => $status, |
98
|
3 |
|
'data' => $page, |
99
|
3 |
|
'page' => (is_array($page) && $status == 200) ? new Renderer($this->config, $page) : $page, |
100
|
3 |
|
'metadata' => (is_array($page) && $status == 200) ? new Metadata($this->config, $page) : $page, |
101
|
3 |
|
'type' => !empty($page['type']) ? $page['type'] : false |
102
|
3 |
|
]; |
103
|
|
|
} |
104
|
|
|
return [ |
105
|
1 |
|
'status' => false, |
106
|
1 |
|
'error' => 'An unknown error occurred', |
107
|
1 |
|
'data' => [], |
108
|
|
|
'type' => false |
109
|
1 |
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Decode the body of a particular request. |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
4 |
|
public function decodePage(ResponseInterface $response) |
118
|
|
|
{ |
119
|
4 |
|
$body = (string) $response->getBody(); |
120
|
4 |
|
if ($response->getHeaderLine('Content-Type') == "application/json") { |
121
|
3 |
|
$page = json_decode($body, true); |
122
|
3 |
|
return !empty($page['exists']) ? $page['page'] : false; |
123
|
|
|
} |
124
|
1 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Configure the resolver. |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
24 |
|
public static function config($assign = false) |
133
|
|
|
{ |
134
|
24 |
|
static $config; |
135
|
|
|
|
136
|
24 |
|
if (is_array($assign) || !$config) { |
137
|
17 |
|
$config = array_merge([ |
138
|
17 |
|
'cache' => null, |
139
|
17 |
|
'cache_ttl' => false, |
140
|
17 |
|
'base_uri' => 'https://pages.vssl.io/', |
141
|
|
|
'required_fields' => [ |
142
|
17 |
|
'id', |
143
|
17 |
|
'title', |
144
|
|
|
'stripes' |
145
|
17 |
|
], |
146
|
|
|
// 'templates' => 'your-directory' |
|
|
|
|
147
|
17 |
|
], $assign); |
148
|
17 |
|
} |
149
|
24 |
|
return $config ?: []; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.