-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFormMigrator.php
145 lines (125 loc) · 3.08 KB
/
FormMigrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Statamic\Migrator;
class FormMigrator extends Migrator
{
use Concerns\MigratesFile,
Concerns\PreparesPathFolder;
protected $form;
protected $blueprint;
/**
* Perform migration.
*/
public function migrate()
{
$this
->setNewPath(resource_path("forms/{$this->handle}.yaml"))
->validateUnique()
->parseForm()
->migrateFieldsToBlueprint()
->migrateFormSchema()
->saveMigratedYaml($this->blueprint, $this->blueprintPath())
->saveMigratedYaml($this->form)
->migrateSubmissions();
}
/**
* Specify unique paths that shouldn't be overwritten.
*
* @return array
*/
protected function uniquePaths()
{
return [
$this->newPath(),
$this->blueprintPath(),
$this->submissionsPath(),
];
}
/**
* Parse user.
*
* @param string $relativePath
* @return $this
*/
protected function parseForm()
{
$this->form = $this->getSourceYaml("settings/formsets/{$this->handle}.yaml");
return $this;
}
/**
* Migrate default v2 form schema to new schema.
*
* @return $this
*/
protected function migrateFormSchema()
{
unset($this->form['fields']);
unset($this->form['columns']);
return $this;
}
/**
* Migrate form fields to blueprint schema.
*
* @return $this
*/
protected function migrateFieldsToBlueprint()
{
$fields = collect($this->form['fields'])
->map(function ($field, $handle) {
return [
'handle' => $handle,
'field' => $this->migrateField($field, $handle),
];
})
->values()
->all();
$this->blueprint = [
'title' => $this->form['title'],
'fields' => $fields,
];
return $this;
}
/**
* Migrate field.
*
* @param array $field
* @param string $handle
* @return array
*/
protected function migrateField($field, $handle)
{
$field = array_merge(['type' => 'text'], $field);
if (isset($this->form['columns']) && ! in_array($handle, $this->form['columns'])) {
$field['listable'] = false;
}
return $field;
}
/**
* Get blueprint path.
*
* @return string
*/
protected function blueprintPath()
{
return resource_path("blueprints/forms/{$this->handle}.yaml");
}
/**
* Migrate submissions.
*
* @return $this
*/
protected function migrateSubmissions()
{
$this->prepareFolder($newPath = $this->submissionsPath());
$this->files->copyDirectory($this->sitePath("storage/forms/{$this->handle}"), $newPath);
return $this;
}
/**
* Get submissions path.
*
* @return string
*/
protected function submissionsPath()
{
return storage_path("forms/{$this->handle}");
}
}