Debian Codegraph @

lib/3rdParty/composer/illuminate/support/ServiceProvider.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Closure;
6 use Illuminate\Console\Application as Artisan;
7 use Illuminate\Contracts\Foundation\CachesConfiguration;
8 use Illuminate\Contracts\Foundation\CachesRoutes;
9 use Illuminate\Contracts\Support\DeferrableProvider;
10 use Illuminate\Database\Eloquent\Factory as ModelFactory;
11 use Illuminate\View\Compilers\BladeCompiler;
12
13 /**
14 * @property array<string, string> $bindings All of the container bindings that should be registered.
15 * @property array<array-key, string> $singletons All of the singletons that should be registered.
16 */
17 abstract class ServiceProvider
18 {
19 /**
20 * The application instance.
21 *
22 * @var \Illuminate\Contracts\Foundation\Application
23 */
24 protected $app;
25
26 /**
27 * All of the registered booting callbacks.
28 *
29 * @var array
30 */
31 protected $bootingCallbacks = [];
32
33 /**
34 * All of the registered booted callbacks.
35 *
36 * @var array
37 */
38 protected $bootedCallbacks = [];
39
40 /**
41 * The paths that should be published.
42 *
43 * @var array
44 */
45 public static $publishes = [];
46
47 /**
48 * The paths that should be published by group.
49 *
50 * @var array
51 */
52 public static $publishGroups = [];
53
54 /**
55 * The migration paths available for publishing.
56 *
57 * @var array
58 */
59 protected static $publishableMigrationPaths = [];
60
61 /**
62 * Commands that should be run during the "optimize" command.
63 *
64 * @var array<string, string>
65 */
66 public static array $optimizeCommands = [];
67
68 /**
69 * Commands that should be run during the "optimize:clear" command.
70 *
71 * @var array<string, string>
72 */
73 public static array $optimizeClearCommands = [];
74
75 /**
76 * Commands that should be run during the "reload" command.
77 *
78 * @var array<string, string>
79 */
80 public static array $reloadCommands = [];
81
82 /**
83 * Create a new service provider instance.
84 *
85 * @param \Illuminate\Contracts\Foundation\Application $app
86 */
87 public function __construct($app)
88 {
89 $this->app = $app;
90 }
91
92 /**
93 * Register any application services.
94 *
95 * @return void
96 */
97 public function register()
98 {
99 //
100 }
101
102 /**
103 * Register a booting callback to be run before the "boot" method is called.
104 *
105 * @param \Closure $callback
106 * @return void
107 */
108 public function booting(Closure $callback)
109 {
110 $this->bootingCallbacks[] = $callback;
111 }
112
113 /**
114 * Register a booted callback to be run after the "boot" method is called.
115 *
116 * @param \Closure $callback
117 * @return void
118 */
119 public function booted(Closure $callback)
120 {
121 $this->bootedCallbacks[] = $callback;
122 }
123
124 /**
125 * Call the registered booting callbacks.
126 *
127 * @return void
128 */
129 public function callBootingCallbacks()
130 {
131 $index = 0;
132
133 while ($index < count($this->bootingCallbacks)) {
134 $this->app->call($this->bootingCallbacks[$index]);
135
136 $index++;
137 }
138 }
139
140 /**
141 * Call the registered booted callbacks.
142 *
143 * @return void
144 */
145 public function callBootedCallbacks()
146 {
147 $index = 0;
148
149 while ($index < count($this->bootedCallbacks)) {
150 $this->app->call($this->bootedCallbacks[$index]);
151
152 $index++;
153 }
154 }
155
156 /**
157 * Merge the given configuration with the existing configuration.
158 *
159 * @param string $path
160 * @param string $key
161 * @return void
162 */
163 protected function mergeConfigFrom($path, $key)
164 {
165 if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) {
166 $config = $this->app->make('config');
167
168 $config->set($key, array_merge(
169 require $path, $config->get($key, [])
170 ));
171 }
172 }
173
174 /**
175 * Replace the given configuration with the existing configuration recursively.
176 *
177 * @param string $path
178 * @param string $key
179 * @return void
180 */
181 protected function replaceConfigRecursivelyFrom($path, $key)
182 {
183 if (! ($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) {
184 $config = $this->app->make('config');
185
186 $config->set($key, array_replace_recursive(
187 require $path, $config->get($key, [])
188 ));
189 }
190 }
191
192 /**
193 * Load the given routes file if routes are not already cached.
194 *
195 * @param string $path
196 * @return void
197 */
198 protected function loadRoutesFrom($path)
199 {
200 if (! ($this->app instanceof CachesRoutes && $this->app->routesAreCached())) {
201 require $path;
202 }
203 }
204
205 /**
206 * Register a view file namespace.
207 *
208 * @param string|array $path
209 * @param string $namespace
210 * @return void
211 */
212 protected function loadViewsFrom($path, $namespace)
213 {
214 $this->callAfterResolving('view', function ($view) use ($path, $namespace) {
215 if (isset($this->app->config['view']['paths']) &&
216 is_array($this->app->config['view']['paths'])) {
217 foreach ($this->app->config['view']['paths'] as $viewPath) {
218 if (is_dir($appPath = $viewPath.'/vendor/'.$namespace)) {
219 $view->addNamespace($namespace, $appPath);
220 }
221 }
222 }
223
224 $view->addNamespace($namespace, $path);
225 });
226 }
227
228 /**
229 * Register the given view components with a custom prefix.
230 *
231 * @param string $prefix
232 * @param array $components
233 * @return void
234 */
235 protected function loadViewComponentsAs($prefix, array $components)
236 {
237 $this->callAfterResolving(BladeCompiler::class, function ($blade) use ($prefix, $components) {
238 foreach ($components as $alias => $component) {
239 $blade->component($component, is_string($alias) ? $alias : null, $prefix);
240 }
241 });
242 }
243
244 /**
245 * Register a translation file namespace or path.
246 *
247 * @param string $path
248 * @param string|null $namespace
249 * @return void
250 */
251 protected function loadTranslationsFrom($path, $namespace = null)
252 {
253 $this->callAfterResolving('translator', fn ($translator) => is_null($namespace)
254 ? $translator->addPath($path)
255 : $translator->addNamespace($namespace, $path));
256 }
257
258 /**
259 * Register a JSON translation file path.
260 *
261 * @param string $path
262 * @return void
263 */
264 protected function loadJsonTranslationsFrom($path)
265 {
266 $this->callAfterResolving('translator', function ($translator) use ($path) {
267 $translator->addJsonPath($path);
268 });
269 }
270
271 /**
272 * Register database migration paths.
273 *
274 * @param array|string $paths
275 * @return void
276 */
277 protected function loadMigrationsFrom($paths)
278 {
279 $this->callAfterResolving('migrator', function ($migrator) use ($paths) {
280 foreach ((array) $paths as $path) {
281 $migrator->path($path);
282 }
283 });
284 }
285
286 /**
287 * Register Eloquent model factory paths.
288 *
289 * @deprecated Will be removed in a future Laravel version.
290 *
291 * @param array|string $paths
292 * @return void
293 */
294 protected function loadFactoriesFrom($paths)
295 {
296 $this->callAfterResolving(ModelFactory::class, function ($factory) use ($paths) {
297 foreach ((array) $paths as $path) {
298 $factory->load($path);
299 }
300 });
301 }
302
303 /**
304 * Setup an after resolving listener, or fire immediately if already resolved.
305 *
306 * @param string $name
307 * @param callable $callback
308 * @return void
309 */
310 protected function callAfterResolving($name, $callback)
311 {
312 $this->app->afterResolving($name, $callback);
313
314 if ($this->app->resolved($name)) {
315 $callback($this->app->make($name), $this->app);
316 }
317 }
318
319 /**
320 * Register migration paths to be published by the publish command.
321 *
322 * @param array $paths
323 * @param mixed $groups
324 * @return void
325 */
326 protected function publishesMigrations(array $paths, $groups = null)
327 {
328 $this->publishes($paths, $groups);
329
330 if ($this->app->config->get('database.migrations.update_date_on_publish', false)) {
331 static::$publishableMigrationPaths = array_unique(array_merge(static::$publishableMigrationPaths, array_keys($paths)));
332 }
333 }
334
335 /**
336 * Register paths to be published by the publish command.
337 *
338 * @param array $paths
339 * @param mixed $groups
340 * @return void
341 */
342 protected function publishes(array $paths, $groups = null)
343 {
344 $this->ensurePublishArrayInitialized($class = static::class);
345
346 static::$publishes[$class] = array_merge(static::$publishes[$class], $paths);
347
348 foreach ((array) $groups as $group) {
349 $this->addPublishGroup($group, $paths);
350 }
351 }
352
353 /**
354 * Ensure the publish array for the service provider is initialized.
355 *
356 * @param string $class
357 * @return void
358 */
359 protected function ensurePublishArrayInitialized($class)
360 {
361 if (! array_key_exists($class, static::$publishes)) {
362 static::$publishes[$class] = [];
363 }
364 }
365
366 /**
367 * Add a publish group / tag to the service provider.
368 *
369 * @param string $group
370 * @param array $paths
371 * @return void
372 */
373 protected function addPublishGroup($group, $paths)
374 {
375 if (! array_key_exists($group, static::$publishGroups)) {
376 static::$publishGroups[$group] = [];
377 }
378
379 static::$publishGroups[$group] = array_merge(
380 static::$publishGroups[$group], $paths
381 );
382 }
383
384 /**
385 * Get the paths to publish.
386 *
387 * @param string|null $provider
388 * @param string|null $group
389 * @return array
390 */
391 public static function pathsToPublish($provider = null, $group = null)
392 {
393 if (! is_null($paths = static::pathsForProviderOrGroup($provider, $group))) {
394 return $paths;
395 }
396
397 return (new Collection(static::$publishes))->reduce(function ($paths, $p) {
398 return array_merge($paths, $p);
399 }, []);
400 }
401
402 /**
403 * Get the paths for the provider or group (or both).
404 *
405 * @param string|null $provider
406 * @param string|null $group
407 * @return array
408 */
409 protected static function pathsForProviderOrGroup($provider, $group)
410 {
411 if ($provider && $group) {
412 return static::pathsForProviderAndGroup($provider, $group);
413 } elseif ($group && array_key_exists($group, static::$publishGroups)) {
414 return static::$publishGroups[$group];
415 } elseif ($provider && array_key_exists($provider, static::$publishes)) {
416 return static::$publishes[$provider];
417 } elseif ($group || $provider) {
418 return [];
419 }
420 }
421
422 /**
423 * Get the paths for the provider and group.
424 *
425 * @param string $provider
426 * @param string $group
427 * @return array
428 */
429 protected static function pathsForProviderAndGroup($provider, $group)
430 {
431 if (! empty(static::$publishes[$provider]) && ! empty(static::$publishGroups[$group])) {
432 return array_intersect_key(static::$publishes[$provider], static::$publishGroups[$group]);
433 }
434
435 return [];
436 }
437
438 /**
439 * Get the service providers available for publishing.
440 *
441 * @return array
442 */
443 public static function publishableProviders()
444 {
445 return array_keys(static::$publishes);
446 }
447
448 /**
449 * Get the migration paths available for publishing.
450 *
451 * @return array
452 */
453 public static function publishableMigrationPaths()
454 {
455 return static::$publishableMigrationPaths;
456 }
457
458 /**
459 * Get the groups available for publishing.
460 *
461 * @return array
462 */
463 public static function publishableGroups()
464 {
465 return array_keys(static::$publishGroups);
466 }
467
468 /**
469 * Register the package's custom Artisan commands.
470 *
471 * @param mixed $commands
472 * @return void
473 */
474 public function commands($commands)
475 {
476 $commands = is_array($commands) ? $commands : func_get_args();
477
478 Artisan::starting(function ($artisan) use ($commands) {
479 $artisan->resolveCommands($commands);
480 });
481 }
482
483 /**
484 * Register commands that should run on "optimize" or "optimize:clear".
485 *
486 * @param string|null $optimize
487 * @param string|null $clear
488 * @param string|null $key
489 * @return void
490 */
491 protected function optimizes(?string $optimize = null, ?string $clear = null, ?string $key = null)
492 {
493 $key = $this->getProviderKey($key);
494
495 if ($optimize) {
496 static::$optimizeCommands[$key] = $optimize;
497 }
498
499 if ($clear) {
500 static::$optimizeClearCommands[$key] = $clear;
501 }
502 }
503
504 /**
505 * Register commands that should run on "reload".
506 *
507 * @param string|null $reload
508 * @param string|null $key
509 * @return void
510 */
511 protected function reloads(string $reload, ?string $key = null)
512 {
513 $key = $this->getProviderKey($key);
514
515 static::$reloadCommands[$key] = $reload;
516 }
517
518 /**
519 * Get a short descriptive key for the current service provider.
520 *
521 * @param string|null $key
522 * @return string
523 */
524 protected function getProviderKey(?string $key = null): string
525 {
526 $key ??= (string) Str::of(get_class($this))
527 ->classBasename()
528 ->before('ServiceProvider')
529 ->kebab()
530 ->lower()
531 ->trim();
532
533 if (empty($key)) {
534 $key = class_basename(get_class($this));
535 }
536
537 return $key;
538 }
539
540 /**
541 * Get the services provided by the provider.
542 *
543 * @return array
544 */
545 public function provides()
546 {
547 return [];
548 }
549
550 /**
551 * Get the events that trigger this service provider to register.
552 *
553 * @return array
554 */
555 public function when()
556 {
557 return [];
558 }
559
560 /**
561 * Determine if the provider is deferred.
562 *
563 * @return bool
564 */
565 public function isDeferred()
566 {
567 return $this instanceof DeferrableProvider;
568 }
569
570 /**
571 * Get the default providers for a Laravel application.
572 *
573 * @return \Illuminate\Support\DefaultProviders
574 */
575 public static function defaultProviders()
576 {
577 return new DefaultProviders;
578 }
579
580 /**
581 * Add the given provider to the application's provider bootstrap file.
582 *
583 * @param string $provider
584 * @param string|null $path
585 * @return bool
586 */
587 public static function addProviderToBootstrapFile(string $provider, ?string $path = null)
588 {
589 $path ??= app()->getBootstrapProvidersPath();
590
591 if (! file_exists($path)) {
592 return false;
593 }
594
595 if (function_exists('opcache_invalidate')) {
596 opcache_invalidate($path, true);
597 }
598
599 $providers = (new Collection(require $path))
600 ->merge([$provider])
601 ->unique()
602 ->sort()
603 ->values()
604 ->map(fn ($p) => ' '.$p.'::class,')
605 ->implode(PHP_EOL);
606
607 $content = '<?php
608
609 return [
610 '.$providers.'
611 ];';
612
613 file_put_contents($path, $content.PHP_EOL);
614
615 return true;
616 }
617
618 /**
619 * Remove a provider from the application's provider bootstrap file.
620 *
621 * @param string|array $providersToRemove
622 * @param string|null $path
623 * @param bool $strict
624 * @return bool
625 */
626 public static function removeProviderFromBootstrapFile(string|array $providersToRemove, ?string $path = null, bool $strict = false)
627 {
628 $path ??= app()->getBootstrapProvidersPath();
629
630 if (! file_exists($path)) {
631 return false;
632 }
633
634 if (function_exists('opcache_invalidate')) {
635 opcache_invalidate($path, true);
636 }
637
638 $providersToRemove = Arr::wrap($providersToRemove);
639
640 $providers = (new Collection(require $path))
641 ->unique()
642 ->sort()
643 ->values()
644 ->when(
645 $strict,
646 static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => in_array($p, $providersToRemove, true)),
647 static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => Str::contains($p, $providersToRemove))
648 )
649 ->map(fn ($p) => ' '.$p.'::class,')
650 ->implode(PHP_EOL);
651
652 $content = '<?php
653
654 return [
655 '.$providers.'
656 ];';
657
658 file_put_contents($path, $content.PHP_EOL);
659
660 return true;
661 }
662 }