1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class RequestStory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array An array of relations to resolve matching: component_name.field_name |
14
|
|
|
* @see https://www.storyblok.com/tp/using-relationship-resolving-to-include-other-content-entries |
15
|
|
|
*/ |
16
|
|
|
private $resolveRelations; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Caches the response if needed |
20
|
|
|
* |
21
|
|
|
* @param $slugOrUuid |
22
|
|
|
* @return mixed |
23
|
|
|
* @throws \Storyblok\ApiException |
24
|
|
|
*/ |
25
|
|
|
public function get($slugOrUuid) { |
26
|
|
|
if (request()->has('_storyblok') || !config('storyblok.cache')) { |
27
|
|
|
$response = $this->makeRequest($slugOrUuid); |
28
|
|
|
} else { |
29
|
|
|
$cache = Cache::getFacadeRoot(); |
30
|
|
|
|
31
|
|
|
if (Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) { |
32
|
|
|
$cache = $cache->tags('storyblok'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$response = $cache->remember($slugOrUuid, config('storyblok.cache_duration') * 60, function () use ($slugOrUuid) { |
36
|
|
|
return $this->makeRequest($slugOrUuid); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $response['story']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Prepares the relations so the format is correct for the API call |
45
|
|
|
* |
46
|
|
|
* @param $resolveRelations |
47
|
|
|
*/ |
48
|
|
|
public function prepareRelations($resolveRelations) { |
49
|
|
|
$this->resolveRelations = implode(',', $resolveRelations); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Makes the API request |
54
|
|
|
* |
55
|
|
|
* @param $slugOrUuid |
56
|
|
|
* @return array|\Storyblok\stdClass |
|
|
|
|
57
|
|
|
* @throws \Storyblok\ApiException |
58
|
|
|
*/ |
59
|
|
|
private function makeRequest($slugOrUuid) { |
60
|
|
|
$storyblokClient = resolve('Storyblok\Client'); |
61
|
|
|
|
62
|
|
|
if ($this->resolveRelations) { |
|
|
|
|
63
|
|
|
$storyblokClient = $storyblokClient->resolveRelations($this->resolveRelations); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (Str::isUuid($slugOrUuid)) { |
67
|
|
|
$storyblokClient = $storyblokClient->getStoryByUuid($slugOrUuid); |
68
|
|
|
} else { |
69
|
|
|
$storyblokClient = $storyblokClient->getStoryBySlug($slugOrUuid); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $storyblokClient->getBody(); |
73
|
|
|
} |
74
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..