|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Storyblok\ApiException; |
|
10
|
|
|
|
|
11
|
|
|
class RequestStory |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string|null A comma delimited string of relations to resolve matching: component_name.field_name |
|
15
|
|
|
* @see https://www.storyblok.com/tp/using-relationship-resolving-to-include-other-content-entries |
|
16
|
|
|
*/ |
|
17
|
|
|
protected ?string $resolveRelations = null; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string|null The language version of the Story to load |
|
22
|
|
|
* @see https://www.storyblok.com/docs/guide/in-depth/internationalization |
|
23
|
|
|
*/ |
|
24
|
|
|
protected ?string $language = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string|null The fallback language version of the Story to load |
|
28
|
|
|
* @see https://www.storyblok.com/docs/guide/in-depth/internationalization |
|
29
|
|
|
*/ |
|
30
|
|
|
protected ?string $fallbackLanguage = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Caches the response if needed |
|
34
|
|
|
* |
|
35
|
|
|
* @param $slugOrUuid |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
* @throws ApiException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get($slugOrUuid): mixed |
|
40
|
|
|
{ |
|
41
|
|
|
if (request()->has('_storyblok') || !config('storyblok.cache')) { |
|
42
|
|
|
$response = $this->makeRequest($slugOrUuid); |
|
43
|
|
|
} else { |
|
44
|
|
|
$cache = Cache::getFacadeRoot(); |
|
45
|
|
|
|
|
46
|
|
|
if (Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) { |
|
47
|
|
|
$cache = $cache->tags('storyblok'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$api_hash = md5(config('storyblok.api_public_key') ?? config('storyblok.api_preview_key')); |
|
51
|
|
|
$response = $cache->remember($slugOrUuid . '_' . $api_hash , config('storyblok.cache_duration') * 60, function () use ($slugOrUuid) { |
|
52
|
|
|
return $this->makeRequest($slugOrUuid); |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $response['story']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Prepares the relations so the format is correct for the API call |
|
61
|
|
|
* |
|
62
|
|
|
* @param $resolveRelations |
|
63
|
|
|
*/ |
|
64
|
|
|
public function resolveRelations($resolveRelations): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->resolveRelations = implode(',', $resolveRelations); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the language and fallback language to use for this Story, will default to ‘default’ |
|
71
|
|
|
* |
|
72
|
|
|
* @param string|null $language |
|
73
|
|
|
* @param string|null $fallbackLanguage |
|
74
|
|
|
*/ |
|
75
|
|
|
public function language($language, $fallbackLanguage = null) { |
|
76
|
|
|
$this->language = $language; |
|
77
|
|
|
$this->fallbackLanguage = $fallbackLanguage; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Makes the API request |
|
82
|
|
|
* |
|
83
|
|
|
* @param $slugOrUuid |
|
84
|
|
|
* @return array |
|
85
|
|
|
* @throws ApiException |
|
86
|
|
|
*/ |
|
87
|
|
|
private function makeRequest($slugOrUuid): array |
|
88
|
|
|
{ |
|
89
|
|
|
$storyblokClient = resolve('Storyblok\Client'); |
|
90
|
|
|
|
|
91
|
|
|
if ($this->resolveRelations) { |
|
92
|
|
|
$storyblokClient = $storyblokClient->resolveRelations($this->resolveRelations); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (config('storyblok.resolve_links')) { |
|
96
|
|
|
$storyblokClient = $storyblokClient->resolveLinks(config('storyblok.resolve_links')); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($this->language) { |
|
100
|
|
|
$storyblokClient = $storyblokClient->language($this->language); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($this->fallbackLanguage) { |
|
104
|
|
|
$storyblokClient = $storyblokClient->fallbackLanguage($this->fallbackLanguage); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (Str::isUuid($slugOrUuid)) { |
|
108
|
|
|
$storyblokClient = $storyblokClient->getStoryByUuid($slugOrUuid); |
|
109
|
|
|
} else { |
|
110
|
|
|
$storyblokClient = $storyblokClient->getStoryBySlug($slugOrUuid); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $storyblokClient->getBody(); |
|
114
|
|
|
} |
|
115
|
|
|
} |