1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixMigrations\Autocreate; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixMigrations\Autocreate\Handlers\HandlerInterface; |
6
|
|
|
use Arrilot\BitrixMigrations\Exceptions\SkipHandlerException; |
7
|
|
|
use Arrilot\BitrixMigrations\Exceptions\StopHandlerException; |
8
|
|
|
use Arrilot\BitrixMigrations\Migrator; |
9
|
|
|
use Arrilot\BitrixMigrations\TemplatesCollection; |
10
|
|
|
use Bitrix\Main\Entity\EventResult; |
11
|
|
|
use Bitrix\Main\EventManager; |
12
|
|
|
|
13
|
|
|
class Manager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* A flag that autocreation was turned on. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected static $isTurnedOn = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Migrator |
24
|
|
|
*/ |
25
|
|
|
protected static $migrator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Handlers that are used by autocreation. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $handlers = [ |
33
|
|
|
'iblock' => [ |
34
|
|
|
'OnBeforeIBlockAdd' => 'OnBeforeIBlockAdd', |
35
|
|
|
'OnBeforeIBlockUpdate' => 'OnBeforeIBlockUpdate', |
36
|
|
|
'OnBeforeIBlockDelete' => 'OnBeforeIBlockDelete', |
37
|
|
|
'OnBeforeIBlockPropertyAdd' => 'OnBeforeIBlockPropertyAdd', |
38
|
|
|
'OnBeforeIBlockPropertyUpdate' => 'OnBeforeIBlockPropertyUpdate', |
39
|
|
|
'OnBeforeIBlockPropertyDelete' => 'OnBeforeIBlockPropertyDelete', |
40
|
|
|
], |
41
|
|
|
'main' => [ |
42
|
|
|
'OnBeforeUserTypeAdd' => 'OnBeforeUserTypeAdd', |
43
|
|
|
'OnBeforeUserTypeDelete' => 'OnBeforeUserTypeDelete', |
44
|
|
|
'OnBeforeGroupAdd' => 'OnBeforeGroupAdd', |
45
|
|
|
'OnBeforeGroupUpdate' => 'OnBeforeGroupUpdate', |
46
|
|
|
'OnBeforeGroupDelete' => 'OnBeforeGroupDelete', |
47
|
|
|
], |
48
|
|
|
'highloadblock' => [ |
49
|
|
|
'\\Bitrix\\Highloadblock\\Highloadblock::OnBeforeAdd' => 'OnBeforeHLBlockAdd', |
50
|
|
|
'\\Bitrix\\Highloadblock\\Highloadblock::OnBeforeUpdate' => 'OnBeforeHLBlockUpdate', |
51
|
|
|
'\\Bitrix\\Highloadblock\\Highloadblock::OnBeforeDelete' => 'OnBeforeHLBlockDelete', |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize autocreation. |
57
|
|
|
* |
58
|
|
|
* @param string $dir |
59
|
|
|
* @param string|null $table |
60
|
|
|
*/ |
61
|
|
|
public static function init($dir, $table = null) |
62
|
|
|
{ |
63
|
|
|
$templates = new TemplatesCollection(); |
64
|
|
|
$templates->registerAutoTemplates(); |
65
|
|
|
|
66
|
|
|
$config = [ |
67
|
|
|
'dir' => $dir, |
68
|
|
|
'table' => is_null($table) ? 'migrations' : $table, |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
static::$migrator = new Migrator($config, $templates); |
72
|
|
|
|
73
|
|
|
static::addEventHandlers(); |
74
|
|
|
|
75
|
|
|
static::turnOn(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine if autocreation is turned on. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public static function isTurnedOn() |
84
|
|
|
{ |
85
|
|
|
return static::$isTurnedOn && defined('ADMIN_SECTION'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Turn on autocreation. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public static function turnOn() |
94
|
|
|
{ |
95
|
|
|
static::$isTurnedOn = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Turn off autocreation. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public static function turnOff() |
104
|
|
|
{ |
105
|
|
|
static::$isTurnedOn = false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Instantiate handler. |
110
|
|
|
* |
111
|
|
|
* @param string $handler |
112
|
|
|
* @param array $parameters |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
protected static function instantiateHandler($handler, $parameters) |
117
|
|
|
{ |
118
|
|
|
$class = __NAMESPACE__.'\\Handlers\\'.$handler; |
119
|
|
|
|
120
|
|
|
return new $class($parameters); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create migration and apply it. |
125
|
|
|
* |
126
|
|
|
* @param HandlerInterface $handler |
127
|
|
|
*/ |
128
|
|
|
protected static function createMigration(HandlerInterface $handler) |
129
|
|
|
{ |
130
|
|
|
$migrator = static::$migrator; |
131
|
|
|
$notifier = new Notifier(); |
132
|
|
|
|
133
|
|
|
$migration = $migrator->createMigration( |
134
|
|
|
strtolower($handler->getName()), |
135
|
|
|
$handler->getTemplate(), |
136
|
|
|
$handler->getReplace() |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$migrator->logSuccessfulMigration($migration); |
140
|
|
|
$notifier->newMigration($migration); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add event handlers. |
145
|
|
|
*/ |
146
|
|
|
protected static function addEventHandlers() |
147
|
|
|
{ |
148
|
|
|
$eventManager = EventManager::getInstance(); |
149
|
|
|
|
150
|
|
|
foreach (static::$handlers as $module => $handlers) { |
151
|
|
|
foreach ($handlers as $event => $handler) { |
152
|
|
|
$eventManager->addEventHandler($module, $event, [__CLASS__, $handler], false, 5000); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$eventManager->addEventHandler('main', 'OnAfterEpilog', function () { |
157
|
|
|
$notifier = new Notifier(); |
158
|
|
|
$notifier->deleteNotificationFromPreviousMigration(); |
159
|
|
|
|
160
|
|
|
return new EventResult(); |
161
|
|
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Magic static call to a handler. |
166
|
|
|
* |
167
|
|
|
* @param string $method |
168
|
|
|
* @param array $parameters |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
|
public static function __callStatic($method, $parameters) |
173
|
|
|
{ |
174
|
|
|
$eventResult = new EventResult(); |
175
|
|
|
|
176
|
|
|
if (!static::isTurnedOn()) { |
177
|
|
|
return $eventResult; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
try { |
181
|
|
|
$handler = static::instantiateHandler($method, $parameters); |
182
|
|
|
} catch (SkipHandlerException $e) { |
183
|
|
|
return $eventResult; |
184
|
|
|
} catch (StopHandlerException $e) { |
185
|
|
|
global $APPLICATION; |
186
|
|
|
$APPLICATION->throwException($e->getMessage()); |
187
|
|
|
|
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
static::createMigration($handler); |
192
|
|
|
|
193
|
|
|
return $eventResult; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|