Repository navigation
Expand file tree
/
Copy pathPluginServiceProviderTrait.php
More file actions
285 lines (248 loc) · 7.58 KB
/
Copy pathPluginServiceProviderTrait.php
File metadata and controls
285 lines (248 loc) · 7.58 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
namespace ProcessMaker\Traits;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use ProcessMaker\Events\ModelerStarting;
use ProcessMaker\Events\ScriptBuilderStarting;
use ProcessMaker\Managers\IndexManager;
use ProcessMaker\Managers\LoginManager;
use ProcessMaker\Managers\PackageManager;
use ProcessMaker\Providers\ProcessMakerServiceProvider;
/**
* Add functionality to control a PM plug-in
*/
trait PluginServiceProviderTrait
{
private $modelerScripts = [];
private $scriptBuilderScripts = [];
private static $bootStart = null;
private static $bootTime;
public function __construct($app)
{
parent::__construct($app);
$this->bootServerTiming();
}
/**
* The `bootServerTiming` function sets up timing measurements for the booting and booted events of the packages
*
* @return void If the condition `config('app.server_timing.enabled')` is false, nothing is being returned as the
* function will exit early.
*/
protected function bootServerTiming(): void
{
if (!config('app.server_timing.enabled')) {
return;
}
$package = $this->getPackageName();
$this->booting(function () use ($package) {
self::$bootStart = microtime(true);
ProcessMakerServiceProvider::setPackageBootStart($package, self::$bootStart);
});
$this->booted(function () use ($package) {
self::$bootTime = microtime(true);
ProcessMakerServiceProvider::setPackageBootedTime($package, self::$bootTime);
});
}
/**
* Get the package name for the Server Timing header
*
* @return string
*/
protected function getPackageName(): string
{
if (defined('static::name')) {
return ucfirst(\Str::camel(static::name));
}
return substr(static::class, strrpos(static::class, '\\') + 1);
}
/**
* Boot the PM plug-in.
*/
protected function completePluginBoot()
{
if (defined('static::name')) {
$this->registerPackage(static::name);
}
Event::listen(ModelerStarting::class, [$this, 'modelerStarting']);
Event::listen(ScriptBuilderStarting::class, [$this, 'scriptBuilderStarting']);
}
/**
* Executed during modeler starting
*
* @param ModelerStarting $event
*
* @throws \Exception
*/
public function modelerStarting(ModelerStarting $event)
{
foreach ($this->modelerScripts as $path => $config) {
if (File::exists(public_path() . '/' . $config['script_src'])) {
$event->manager->addScript(mix($path, $config['script_src']), $config['script_params']);
}
}
}
/**
* Register a custom script for the modeler with optional parameters.
*
* This method registers a script for the modeler along with optional parameters.
* The script path and its corresponding public URL are added to the modeler scripts registry,
* and if additional parameters are provided, they are included in the registration.
*
* @param string $path The path to the script file.
* @param string $public The public URL of the script.
* @param array $params Additional parameters for configuring the script (optional).
* @return void
*/
protected function registerModelerScript($path, $public, array $params = [])
{
// Register the script path and public URL along with optional parameters
$this->modelerScripts[$path] = [
'script_src' => $public,
'script_params' => $params,
];
}
/**
* Executed once when the plug-in version was changed.
*/
protected function updateVersion()
{
//
}
/**
* Check if the plug-in was updated
*/
protected function isUpdated()
{
$key = str_replace('\\', '_', static::class);
return static::version === Cache::get($key, '0.0.0');
}
/**
* Register a seeder that should be executed when php artisan db:seed is called.
*
* @param string $seederClass
*/
protected function registerSeeder($seederClass)
{
LoadPluginSeeders::registerSeeder($seederClass);
}
/**
* Register package installed
*
* @param $package
*/
protected function registerPackage($package)
{
App::make(PackageManager::class)->addPackage($package);
}
/**
* Register index
*
* @param $name string name of index
* @param $model string path of model
* @param $callback callback function to perform indexing
*/
protected function registerIndex($name, $model, $callback)
{
App::make(IndexManager::class)->add($name, $model, $callback);
}
/**
* Register login addon
*
* @param $name string name of view file
* @param $data array data to pass to the view
*/
protected function registerLoginAddon($view, $data = [])
{
App::make(LoginManager::class)->add($view, $data);
}
/**
* Do not display standard login form
*/
protected function blockStandardLogin()
{
App::make(LoginManager::class)->block();
}
/**
* Verify package is in register installed
*
* @param $name
* @return bool
*/
public function isRegisteredPackage($package)
{
return App::make(PackageManager::class)->isRegistered($package);
}
/**
* Remove package of list installed
*
* @param $name
*/
public function removePackage($package)
{
App::make(PackageManager::class)->remove($package);
}
/**
* Executed during script builder starting
*
* @param ScriptBuilderStarting $event
*
* @throws \Exception
*/
public function scriptBuilderStarting(ScriptBuilderStarting $event)
{
foreach ($this->scriptBuilderScripts as $path => $public) {
if (File::exists(public_path() . '/' . $public)) {
$event->manager->addScript(mix($path, $public));
}
}
}
/**
* Load upgrade migrations from a provided directory/ies
*
* @param $paths
*
* @return void
*/
public function loadUpgradeMigrationsFrom($paths)
{
$this->callAfterResolving('upgrade', function ($migrator) use ($paths) {
foreach ((array) $paths as $path) {
$migrator->path($path);
}
});
}
/**
* Register a custom javascript for the script builder
*
* @param string $path
* @param string $public
*/
protected function registerJsToScriptBuilder($path, $public)
{
$this->scriptBuilderScripts[$path] = $public;
}
/**
* Update config so l5-swagger knows where to look for @OA annotations
*
* @param array $paths
*
* @return void
*/
public function registerOpenApiAnnotationPaths(array $paths)
{
if (!app()->runningInConsole()) {
return;
}
$configString = 'l5-swagger.documentations.default.paths.annotations';
// Avoid duplicate directories: with config cached, paths may already include
// plugin folders; each console boot would otherwise merge them again and
// swagger-php would scan the same files twice (duplicate @OA paths/operations).
$merged = array_values(array_unique(array_merge(
config($configString),
$paths
)));
config([$configString => $merged]);
}
}