|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Riclep\StoryblokForms\Console\InstallCommand; |
|
8
|
|
|
use Riclep\StoryblokForms\Traits\GetsComponentGroups; |
|
9
|
|
|
use Storyblok\ManagementClient; |
|
10
|
|
|
|
|
11
|
|
|
class ComponentMaker |
|
12
|
|
|
{ |
|
13
|
|
|
// TODO - refactor to use Laravel Storyblok CLI package |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use GetsComponentGroups; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array Component definition |
|
20
|
|
|
*/ |
|
21
|
|
|
protected array $schema; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Collection A list of all component groups |
|
25
|
|
|
*/ |
|
26
|
|
|
protected Collection $componentGroups; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ManagementClient Storyblok Management Client |
|
30
|
|
|
*/ |
|
31
|
|
|
protected ManagementClient $managementClient; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var InstallCommand The package’s command |
|
35
|
|
|
*/ |
|
36
|
|
|
protected InstallCommand $command; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param InstallCommand $command |
|
40
|
|
|
* @param $schema |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(InstallCommand $command, $schema) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->command = $command; |
|
45
|
|
|
$this->schema = $schema; |
|
46
|
|
|
|
|
47
|
|
|
$this->managementClient = new ManagementClient( |
|
48
|
|
|
apiKey: config('storyblok.oauth_token'), |
|
49
|
|
|
apiEndpoint: config('storyblok.management_api_base_url'), |
|
50
|
|
|
ssl: config('storyblok.use_ssl') |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the import started |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
* @throws \Storyblok\ApiException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function import(): void |
|
61
|
|
|
{ |
|
62
|
|
|
// TODO - validate json..... |
|
63
|
|
|
|
|
64
|
|
|
$this->getGroups(); |
|
65
|
|
|
$this->updatedGroupToUuid(); |
|
66
|
|
|
$this->processBlokFields(); |
|
67
|
|
|
$this->createComponent(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Takes a ‘named group’ from a $schema and replaces it with the Storyblok group UUID |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function updatedGroupToUuid(): void |
|
76
|
|
|
{ |
|
77
|
|
|
// see if UUID is known or name needs replacing |
|
78
|
|
|
if (!Str::isUuid($this->schema['component']['component_group_uuid'])) { |
|
79
|
|
|
if ($uuid = $this->groupUuidFromName($this->schema['component']['component_group_uuid'])) { |
|
80
|
|
|
$this->schema['component']['component_group_uuid'] = $uuid; |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->command->warn('Requested component group ' . $this->schema['component']['component_group_uuid'] . ' does not exist, using in root'); |
|
83
|
|
|
|
|
84
|
|
|
unset($this->schema['component']['component_group_uuid']); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses a schema’s fields to see if they need any processing. |
|
91
|
|
|
* Blok fields will have their whitelist and other settings configured |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function processBlokFields(): void |
|
96
|
|
|
{ |
|
97
|
|
|
// Bloks fields - set up component group whitelist |
|
98
|
|
|
foreach ($this->schema['component']['schema'] as $fieldKey => $field) { |
|
99
|
|
|
if ($field['type'] === 'bloks' && array_key_exists('component_group_whitelist', $field)) { |
|
100
|
|
|
foreach ($field['component_group_whitelist'] as $groupKey => $group) { |
|
101
|
|
|
if ($uuid = $this->groupUuidFromName($group)) { |
|
102
|
|
|
$this->schema['component']['schema'][$fieldKey]['component_group_whitelist'][$groupKey] = $uuid; |
|
103
|
|
|
} else { |
|
104
|
|
|
$this->command->warn('Requested blok component group whitelist ' . $this->schema['component']['component_group_uuid'] . ' does not exist'); |
|
105
|
|
|
|
|
106
|
|
|
unset($this->schema['component']['component_group_uuid']); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Creates the component within Storyblok |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
* @throws \Storyblok\ApiException |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function createComponent(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$response = $this->managementClient->get('spaces/'.config('storyblok.space_id').'/components/')->getBody(); |
|
122
|
|
|
|
|
123
|
|
|
if (collect($response['components'])->keyBy('name')->has($this->schema['component']['name'])) { |
|
124
|
|
|
$this->command->warn('Component already exists: ' . $this->schema['component']['display_name'] . ' (' . $this->schema['component']['name'] . ')'); |
|
125
|
|
|
} else { |
|
126
|
|
|
$this->managementClient->post('spaces/'.config('storyblok.space_id').'/components/', $this->schema)->getBody(); |
|
127
|
|
|
|
|
128
|
|
|
$this->command->info('Created: ' . $this->schema['component']['display_name'] . ' (' . $this->schema['component']['name'] . ')'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// TODO - presets in schema such as adding validators for numeric inputs |
|
133
|
|
|
protected function processPresets() { |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// TODO |
|
138
|
|
|
protected function processTabs() { |
|
139
|
|
|
/* TODO - field tabs |
|
140
|
|
|
"tab-e452755a-71ed-40fb-a735-4fc96c1a1e78" => array:4 [ |
|
141
|
|
|
"type" => "tab" |
|
142
|
|
|
"display_name" => "Settings" |
|
143
|
|
|
"keys" => array:2 [ |
|
144
|
|
|
0 => "placeholder" |
|
145
|
|
|
1 => "type" |
|
146
|
|
|
] |
|
147
|
|
|
"pos" => 5 |
|
148
|
|
|
] |
|
149
|
|
|
"tab-54270608-a51e-48b9-a375-9c6111004250" => array:4 [ |
|
150
|
|
|
"type" => "tab" |
|
151
|
|
|
"display_name" => "Validation" |
|
152
|
|
|
"keys" => array:1 [ |
|
153
|
|
|
0 => "validators" |
|
154
|
|
|
] |
|
155
|
|
|
"pos" => 6 |
|
156
|
|
|
] |
|
157
|
|
|
* */ |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Takes a component group’s name and returns the Storyblok UUID for that group |
|
162
|
|
|
* |
|
163
|
|
|
* @param $name |
|
164
|
|
|
* @return false|mixed |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function groupUuidFromName($name): mixed |
|
167
|
|
|
{ |
|
168
|
|
|
if ($this->componentGroups->has($name)) { |
|
169
|
|
|
return $this->componentGroups[$name]['uuid']; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
} |