| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
abstract class ServiceProvider |
| 18 |
{ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
protected $app; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected $bootingCallbacks = []; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected $bootedCallbacks = []; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static $publishes = []; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static $publishGroups = []; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected static $publishableMigrationPaths = []; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static array $optimizeCommands = []; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static array $optimizeClearCommands = []; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static array $reloadCommands = []; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function __construct($app) |
| 88 |
{ |
| 89 |
$this->app = $app; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function register() |
| 98 |
{ |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function booting(Closure $callback) |
| 109 |
{ |
| 110 |
$this->bootingCallbacks[] = $callback; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function booted(Closure $callback) |
| 120 |
{ |
| 121 |
$this->bootedCallbacks[] = $callback; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
protected function loadRoutesFrom($path) |
| 199 |
{ |
| 200 |
if (! ($this->app instanceof CachesRoutes && $this->app->routesAreCached())) { |
| 201 |
require $path; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 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 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected function loadJsonTranslationsFrom($path) |
| 265 |
{ |
| 266 |
$this->callAfterResolving('translator', function ($translator) use ($path) { |
| 267 |
$translator->addJsonPath($path); |
| 268 |
}); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 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 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 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 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 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 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 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 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 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 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
protected function ensurePublishArrayInitialized($class) |
| 360 |
{ |
| 361 |
if (! array_key_exists($class, static::$publishes)) { |
| 362 |
static::$publishes[$class] = []; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 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 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 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 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 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 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 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 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static function publishableProviders() |
| 444 |
{ |
| 445 |
return array_keys(static::$publishes); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public static function publishableMigrationPaths() |
| 454 |
{ |
| 455 |
return static::$publishableMigrationPaths; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public static function publishableGroups() |
| 464 |
{ |
| 465 |
return array_keys(static::$publishGroups); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 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 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 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 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 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 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 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 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function provides() |
| 546 |
{ |
| 547 |
return []; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public function when() |
| 556 |
{ |
| 557 |
return []; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public function isDeferred() |
| 566 |
{ |
| 567 |
return $this instanceof DeferrableProvider; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
public static function defaultProviders() |
| 576 |
{ |
| 577 |
return new DefaultProviders; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 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 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 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 |
} |