1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vessel\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 \Vessel\Render\PageApi |
31
|
|
|
*/ |
32
|
|
|
protected $api; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize a new Resolver |
36
|
|
|
*/ |
37
|
3 |
|
public function __construct(ServerRequestInterface $request, $config = false) |
38
|
|
|
{ |
39
|
3 |
|
$this->request = $request; |
40
|
3 |
|
$this->config = is_array($config) ? static::config($config) : static::config(); |
|
|
|
|
41
|
3 |
|
if (!$this->config['cache'] instanceof CacheAdapterInterface) { |
42
|
|
|
throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.'); |
43
|
|
|
} |
44
|
3 |
|
$this->api = new PageApi($this->config); |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a modified request message. |
49
|
|
|
* |
50
|
|
|
* @return \Psr\Http\Message\ServerRequestInterface |
51
|
|
|
*/ |
52
|
3 |
|
public function getRequest() |
53
|
|
|
{ |
54
|
3 |
|
return $this->request->withAttribute('ws-page', $this->resolve()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Resolve the current page from WebStories. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
3 |
|
public function resolve() |
63
|
|
|
{ |
64
|
3 |
|
if ($response = $this->api->getPage($this->request->getUri()->getPath())) { |
65
|
2 |
|
$data = $this->decodeBody($response); |
66
|
1 |
|
return [ |
67
|
2 |
|
'status' => $response->getStatusCode(), |
68
|
2 |
|
'data' => $data, |
69
|
2 |
|
'page' => is_array($data) ? new Renderer($this->config, $data) : $data, |
70
|
2 |
|
]; |
71
|
|
|
} |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Decode the body of a particular request. |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
2 |
|
public function decodeBody(ResponseInterface $response) |
81
|
|
|
{ |
82
|
2 |
|
$body = (string) $response->getBody(); |
83
|
2 |
|
if ($response->getHeaderLine('Content-Type') == "application/json") { |
84
|
2 |
|
return json_decode($body, true); |
85
|
|
|
} |
86
|
|
|
return $body; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Configure the resolver. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
9 |
|
public static function config($assign = false) |
95
|
|
|
{ |
96
|
9 |
|
static $config; |
97
|
|
|
|
98
|
9 |
|
if (is_array($assign) || !$config) { |
99
|
4 |
|
$config = array_merge([ |
100
|
4 |
|
'cache' => null, |
101
|
4 |
|
'base_uri' => 'https://www.webstories.com/', |
102
|
|
|
'required_fields' => [ |
103
|
4 |
|
'id', |
104
|
4 |
|
'title', |
105
|
|
|
'stripes' |
106
|
4 |
|
], |
107
|
|
|
// 'templates' => 'your-directory' |
|
|
|
|
108
|
4 |
|
], $assign); |
109
|
4 |
|
} |
110
|
9 |
|
return $config ?: []; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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: