|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixMigrations; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
class TemplatesCollection |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Path to directory where basic templates are. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $dir; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Array of available migration file templates. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $templates = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->dir = dirname(__DIR__).'/templates'; |
|
30
|
|
|
|
|
31
|
|
|
$this->registerTemplate([ |
|
32
|
|
|
'name' => 'default', |
|
33
|
|
|
'path' => $this->dir.'/default.template', |
|
34
|
|
|
'description' => 'Default migration template', |
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Register basic templates. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function registerBasicTemplates() |
|
42
|
|
|
{ |
|
43
|
|
|
$templates = [ |
|
44
|
|
|
[ |
|
45
|
|
|
'name' => 'add_iblock', |
|
46
|
|
|
'path' => $this->dir.'/add_iblock.template', |
|
47
|
|
|
'description' => 'Add iblock', |
|
48
|
|
|
], |
|
49
|
|
|
[ |
|
50
|
|
|
'name' => 'add_iblock_type', |
|
51
|
|
|
'path' => $this->dir.'/add_iblock_type.template', |
|
52
|
|
|
'description' => 'Add iblock type', |
|
53
|
|
|
], |
|
54
|
|
|
[ |
|
55
|
|
|
'name' => 'add_iblock_element_property', |
|
56
|
|
|
'path' => $this->dir.'/add_iblock_element_property.template', |
|
57
|
|
|
'description' => 'Add iblock element property', |
|
58
|
|
|
'aliases' => [ |
|
59
|
|
|
'add_iblock_prop', |
|
60
|
|
|
'add_iblock_element_prop', |
|
61
|
|
|
'add_element_prop', |
|
62
|
|
|
'add_element_property', |
|
63
|
|
|
], |
|
64
|
|
|
], |
|
65
|
|
|
[ |
|
66
|
|
|
'name' => 'add_uf', |
|
67
|
|
|
'path' => $this->dir.'/add_uf.template', |
|
68
|
|
|
'description' => 'Add user field (for sections, users e.t.c)', |
|
69
|
|
|
], |
|
70
|
|
|
[ |
|
71
|
|
|
'name' => 'add_table', |
|
72
|
|
|
'path' => $this->dir.'/add_table.template', |
|
73
|
|
|
'description' => 'Create table', |
|
74
|
|
|
'aliases' => [ |
|
75
|
|
|
'create_table', |
|
76
|
|
|
], |
|
77
|
|
|
], |
|
78
|
|
|
[ |
|
79
|
|
|
'name' => 'delete_table', |
|
80
|
|
|
'path' => $this->dir.'/delete_table.template', |
|
81
|
|
|
'description' => 'Drop table', |
|
82
|
|
|
'aliases' => [ |
|
83
|
|
|
'drop_table', |
|
84
|
|
|
], |
|
85
|
|
|
], |
|
86
|
|
|
[ |
|
87
|
|
|
'name' => 'query', |
|
88
|
|
|
'path' => $this->dir.'/query.template', |
|
89
|
|
|
'description' => 'Simple database query', |
|
90
|
|
|
], |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($templates as $template) { |
|
94
|
|
|
$this->registerTemplate($template); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Register templates for automigrations. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function registerAutoTemplates() |
|
102
|
|
|
{ |
|
103
|
|
|
$templates = [ |
|
104
|
|
|
'add_iblock', |
|
105
|
|
|
'update_iblock', |
|
106
|
|
|
'delete_iblock', |
|
107
|
|
|
'add_iblock_element_property', |
|
108
|
|
|
'update_iblock_element_property', |
|
109
|
|
|
'delete_iblock_element_property', |
|
110
|
|
|
'add_uf', |
|
111
|
|
|
'update_uf', |
|
112
|
|
|
'delete_uf', |
|
113
|
|
|
'add_hlblock', |
|
114
|
|
|
'update_hlblock', |
|
115
|
|
|
'delete_hlblock', |
|
116
|
|
|
'add_group', |
|
117
|
|
|
'update_group', |
|
118
|
|
|
'delete_group', |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
foreach ($templates as $template) { |
|
122
|
|
|
$this->registerTemplate([ |
|
123
|
|
|
'name' => 'auto_'.$template, |
|
124
|
|
|
'path' => $this->dir.'/auto/'.$template.'.template', |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Getter for registered templates. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
public function all() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->templates; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Dynamically register migration template. |
|
141
|
|
|
* |
|
142
|
|
|
* @param array $template |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function registerTemplate($template) |
|
147
|
|
|
{ |
|
148
|
|
|
$template = $this->normalizeTemplateDuringRegistration($template); |
|
149
|
|
|
|
|
150
|
|
|
$this->templates[$template['name']] = $template; |
|
151
|
|
|
|
|
152
|
|
|
$this->registerTemplateAliases($template, $template['aliases']); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Path to the file where a template is located. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getTemplatePath($name) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->templates[$name]['path']; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Find out template name from user input. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string|null $template |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function selectTemplate($template) |
|
175
|
|
|
{ |
|
176
|
|
|
if (is_null($template)) { |
|
177
|
|
|
return 'default'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (!array_key_exists($template, $this->templates)) { |
|
181
|
|
|
throw new RuntimeException("Template \"{$template}\" is not registered"); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $template; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Check template fields and normalize them. |
|
189
|
|
|
* |
|
190
|
|
|
* @param $template |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function normalizeTemplateDuringRegistration($template) |
|
195
|
|
|
{ |
|
196
|
|
|
if (empty($template['name'])) { |
|
197
|
|
|
throw new InvalidArgumentException('Impossible to register a template without "name"'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (empty($template['path'])) { |
|
201
|
|
|
throw new InvalidArgumentException('Impossible to register a template without "path"'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$template['description'] = isset($template['description']) ? $template['description'] : ''; |
|
205
|
|
|
$template['aliases'] = isset($template['aliases']) ? $template['aliases'] : []; |
|
206
|
|
|
$template['is_alias'] = false; |
|
207
|
|
|
|
|
208
|
|
|
return $template; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Register template aliases. |
|
213
|
|
|
* |
|
214
|
|
|
* @param array $template |
|
215
|
|
|
* @param array $aliases |
|
216
|
|
|
* |
|
217
|
|
|
* @return void |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function registerTemplateAliases($template, array $aliases = []) |
|
220
|
|
|
{ |
|
221
|
|
|
foreach ($aliases as $alias) { |
|
222
|
|
|
$template['is_alias'] = true; |
|
223
|
|
|
$template['name'] = $alias; |
|
224
|
|
|
$template['aliases'] = []; |
|
225
|
|
|
|
|
226
|
|
|
$this->templates[$template['name']] = $template; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|