1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixBlade\BladeProvider; |
6
|
|
|
use Arrilot\BitrixModels\Debug\IlluminateQueryDebugger; |
7
|
|
|
use Arrilot\BitrixModels\Models\EloquentModel; |
8
|
|
|
use Bitrix\Main\Config\Configuration; |
9
|
|
|
use DB; |
10
|
|
|
use Illuminate\Container\Container; |
11
|
|
|
use Illuminate\Events\Dispatcher; |
12
|
|
|
use Illuminate\Pagination\Paginator; |
13
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
14
|
|
|
|
15
|
|
|
class ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Register the service provider. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public static function register() |
23
|
|
|
{ |
24
|
|
|
self::bootstrapIlluminatePagination(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register eloquent. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public static function registerEloquent() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$capsule = self::bootstrapIlluminateDatabase(); |
35
|
|
|
class_alias(Capsule::class, 'DB'); |
36
|
|
|
|
37
|
|
|
if ($_COOKIE["show_sql_stat"] == "Y") { |
38
|
|
|
Capsule::enableQueryLog(); |
39
|
|
|
|
40
|
|
|
$em = \Bitrix\Main\EventManager::getInstance(); |
41
|
|
|
$em->addEventHandler('main', 'OnAfterEpilog', [IlluminateQueryDebugger::class, 'onAfterEpilogHandler']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
static::addEventListenersForHelpersHighloadblockTables($capsule); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Bootstrap illuminate/pagination |
49
|
|
|
*/ |
50
|
|
|
protected static function bootstrapIlluminatePagination() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
if (class_exists(BladeProvider::class)) { |
53
|
|
|
Paginator::viewFactoryResolver(function () { |
54
|
|
|
return BladeProvider::getViewFactory(); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
Paginator::$defaultView = 'pagination.default'; |
59
|
|
|
Paginator::$defaultSimpleView = 'pagination.simple-default'; |
60
|
|
|
|
61
|
|
|
Paginator::currentPathResolver(function () { |
62
|
|
|
return $GLOBALS['APPLICATION']->getCurPage(); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
Paginator::currentPageResolver(function ($pageName = 'page') { |
66
|
|
|
$page = $_GET[$pageName]; |
67
|
|
|
|
68
|
|
|
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) { |
69
|
|
|
return $page; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return 1; |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Bootstrap illuminate/database |
78
|
|
|
* @return Capsule |
79
|
|
|
*/ |
80
|
|
|
protected static function bootstrapIlluminateDatabase() |
81
|
|
|
{ |
82
|
|
|
$config = self::getBitrixDbConfig(); |
83
|
|
|
|
84
|
|
|
$capsule = new Capsule(self::instantiateServiceContainer()); |
85
|
|
|
$capsule->addConnection([ |
86
|
|
|
'driver' => 'mysql', |
87
|
|
|
'host' => $config['host'], |
88
|
|
|
'database' => $config['database'], |
89
|
|
|
'username' => $config['login'], |
90
|
|
|
'password' => $config['password'], |
91
|
|
|
'charset' => 'utf8', |
92
|
|
|
'collation' => 'utf8_unicode_ci', |
93
|
|
|
'prefix' => '', |
94
|
|
|
'strict' => false, |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
if (class_exists(Dispatcher::class)) { |
98
|
|
|
$capsule->setEventDispatcher(new Dispatcher()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$capsule->setAsGlobal(); |
102
|
|
|
$capsule->bootEloquent(); |
103
|
|
|
|
104
|
|
|
return $capsule; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Instantiate service container if it's not instantiated yet. |
109
|
|
|
*/ |
110
|
|
|
protected static function instantiateServiceContainer() |
111
|
|
|
{ |
112
|
|
|
$container = Container::getInstance(); |
113
|
|
|
|
114
|
|
|
if (!$container) { |
115
|
|
|
$container = new Container(); |
116
|
|
|
Container::setInstance($container); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $container; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get bitrix database configuration array. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected static function getBitrixDbConfig() |
128
|
|
|
{ |
129
|
|
|
$config = Configuration::getInstance(); |
130
|
|
|
$connections = $config->get('connections'); |
131
|
|
|
|
132
|
|
|
return $connections['default']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Для множественных полей Highload блоков битрикс использует вспомогательные таблицы. |
137
|
|
|
* Данный метод вешает обработчики на eloquent события добавления и обновления записей которые будут актуализировать и эти таблицы. |
138
|
|
|
* |
139
|
|
|
* @param Capsule $capsule |
140
|
|
|
*/ |
141
|
|
|
private static function addEventListenersForHelpersHighloadblockTables(Capsule $capsule) |
142
|
|
|
{ |
143
|
|
|
$dispatcher = $capsule->getEventDispatcher(); |
144
|
|
|
if (!$dispatcher) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$dispatcher->listen(['eloquent.created: *'], function($event, $payload) { |
|
|
|
|
149
|
|
|
// $model = $payload[0]; |
|
|
|
|
150
|
|
|
// dump(func_get_args()); |
|
|
|
|
151
|
|
|
}); |
152
|
|
|
|
153
|
|
|
$dispatcher->listen(['eloquent.updated: *', 'eloquent.created: *'], function($event, $payload) { |
154
|
|
|
/** @var EloquentModel $model */ |
155
|
|
|
$model = $payload[0]; |
156
|
|
|
if (empty($model->multipleHighloadBlockFields)) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$dirty = $model->getDirty(); |
161
|
|
|
foreach ($model->multipleHighloadBlockFields as $multipleHighloadBlockField) { |
162
|
|
|
if (isset($dirty[$multipleHighloadBlockField]) && !empty($model['ID'])) { |
163
|
|
|
$tableName = $model->getTable().'_'.strtolower($multipleHighloadBlockField); |
164
|
|
|
|
165
|
|
|
if (substr($event, 0, 16) === 'eloquent.updated') { |
166
|
|
|
DB::table($tableName)->where('ID', $model['ID'])->delete(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$unserializedValues = unserialize($dirty[$multipleHighloadBlockField]); |
170
|
|
|
if (!$unserializedValues) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$newRows = []; |
175
|
|
|
foreach ($unserializedValues as $unserializedValue) { |
176
|
|
|
$newRows[] = [ |
177
|
|
|
'ID' => $model['ID'], |
178
|
|
|
'VALUE' => $unserializedValue, |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($newRows) { |
|
|
|
|
183
|
|
|
DB::table($tableName)->insert($newRows); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
}); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: