|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vssl\Render; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Journey\Cache\Adapters\LocalAdapter; |
|
7
|
|
|
use Journey\Cache\CacheAdapterInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Resolver |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The request object. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Psr\Http\Message\ServerRequestInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $request; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Configuration array. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The page api object. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Vssl\Render\PageApi |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $api; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Initialize a new Resolver |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function __construct(ServerRequestInterface $request, $config = false) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->request = $request; |
|
40
|
4 |
|
$this->config = is_array($config) ? static::config($config) : static::config(); |
|
|
|
|
|
|
41
|
4 |
|
if (!$this->config['cache'] instanceof CacheAdapterInterface) { |
|
42
|
|
|
throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.'); |
|
43
|
|
|
} |
|
44
|
4 |
|
$this->api = new PageApi($request, $this->config); |
|
45
|
4 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get a modified request message. |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Psr\Http\Message\ServerRequestInterface |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function getRequest() |
|
53
|
|
|
{ |
|
54
|
4 |
|
return $this->request->withAttribute('vssl-page', $this->resolve()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Resolve the current page from WebStories. Returns a render able page or |
|
59
|
|
|
* false. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function resolve() |
|
64
|
|
|
{ |
|
65
|
4 |
|
$response = $this->api->getPage($this->request->getUri()->getPath()); |
|
66
|
4 |
|
if ($response && ($page = $this->decodePage($response))) { |
|
67
|
3 |
|
$status = $response->getStatusCode(); |
|
68
|
|
|
return [ |
|
69
|
3 |
|
'status' => $status, |
|
70
|
3 |
|
'data' => $page, |
|
71
|
3 |
|
'page' => (is_array($page) && $status == 200) ? new Renderer($this->config, $page) : $page, |
|
72
|
3 |
|
'opengraph' => (is_array($page) && $status == 200) ? new Metadata($this->config, $page) : $page, |
|
73
|
3 |
|
'type' => !empty($page['type']) ? $page['type'] : false |
|
74
|
3 |
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
return [ |
|
77
|
1 |
|
'status' => false, |
|
78
|
1 |
|
'error' => 'An unknown error occurred', |
|
79
|
1 |
|
'data' => [] |
|
80
|
1 |
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Decode the body of a particular request. |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function decodePage(ResponseInterface $response) |
|
89
|
|
|
{ |
|
90
|
3 |
|
$body = (string) $response->getBody(); |
|
91
|
3 |
|
if ($response->getHeaderLine('Content-Type') == "application/json") { |
|
92
|
3 |
|
$page = json_decode($body, true); |
|
93
|
3 |
|
return !empty($page['exists']) ? $page['page'] : false; |
|
94
|
|
|
} |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Configure the resolver. |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
16 |
|
public static function config($assign = false) |
|
104
|
|
|
{ |
|
105
|
16 |
|
static $config; |
|
106
|
|
|
|
|
107
|
16 |
|
if (is_array($assign) || !$config) { |
|
108
|
10 |
|
$config = array_merge([ |
|
109
|
10 |
|
'cache' => null, |
|
110
|
10 |
|
'base_uri' => 'https://pages.vssl.io/', |
|
111
|
|
|
'required_fields' => [ |
|
112
|
10 |
|
'id', |
|
113
|
10 |
|
'title', |
|
114
|
|
|
'stripes' |
|
115
|
10 |
|
], |
|
116
|
|
|
// 'templates' => 'your-directory' |
|
|
|
|
|
|
117
|
10 |
|
], $assign); |
|
118
|
10 |
|
} |
|
119
|
16 |
|
return $config ?: []; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: