From 7f48f4fc0b86ada717624fc41154bc82f0e5b58b Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Wed, 22 Jan 2025 09:48:38 +0100 Subject: [PATCH] avoid method name collisions --- src/PackageServiceProvider.php | 121 ++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 1a19117..c2f7160 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -63,19 +63,20 @@ public function boot() { $this->bootingPackage(); - $this->bootAssets(); - $this->bootCommands(); - $this->bootConsoleCommands(); - $this->bootConfigs(); - $this->bootInertia(); - $this->bootMigrations(); - $this->bootProviders(); - $this->bootRoutes(); - $this->bootTranslations(); - $this->bootViews(); - $this->bootViewComponents(); - $this->bootViewComposers(); - $this->bootViewSharedData(); + $this + ->bootPackageAssets() + ->bootPackageCommands() + ->bootPackageConsoleCommands() + ->bootPackageConfigs() + ->bootPackageInertia() + ->bootPackageMigrations() + ->bootPackageProviders() + ->bootPackageRoutes() + ->bootPackageTranslations() + ->bootPackageViews() + ->bootPackageViewComponents() + ->bootPackageViewComposers() + ->bootPackageViewSharedData(); $this->packageBooted(); @@ -104,9 +105,9 @@ public function packageView(?string $namespace): ?string : $this->package->viewNamespace; } - protected function bootAssets(): void + protected function bootPackageAssets(): static { - if (! $this->package->hasAssets || ! $this->app->runningInConsole()) { + if (!$this->package->hasAssets || !$this->app->runningInConsole()) { return; } @@ -114,27 +115,33 @@ protected function bootAssets(): void $appAssets = public_path("vendor/{$this->package->shortName()}"); $this->publishes([$vendorAssets => $appAssets], "{$this->package->shortName()}-assets"); + + return $this; } - protected function bootCommands() + protected function bootPackageCommands(): self { if (empty($this->package->commands)) { return; } $this->commands($this->package->commands); + + return $this; } - protected function bootConsoleCommands() + protected function bootPackageConsoleCommands(): self { - if (empty($this->package->consoleCommands) || ! $this->app->runningInConsole()) { + if (empty($this->package->consoleCommands) || !$this->app->runningInConsole()) { return; } $this->commands($this->package->consoleCommands); + + return $this; } - protected function bootConfigs() + protected function bootPackageConfigs(): self { if ($this->app->runningInConsole()) { foreach ($this->package->configFileNames as $configFileName) { @@ -144,12 +151,14 @@ protected function bootConfigs() $this->publishes([$vendorConfig => $appConfig], "{$this->package->shortName()}-config"); } } + + return $this; } - protected function bootInertia() + protected function bootPackageInertia(): self { - if (! $this->package->hasInertiaComponents) { - return; + if (!$this->package->hasInertiaComponents) { + return $this; } $namespace = $this->package->viewNamespace; @@ -163,14 +172,16 @@ protected function bootInertia() "{$this->packageView($namespace)}-inertia-components" ); } + + return $this; } - protected function bootMigrations() + protected function bootPackageMigrations(): self { if ($this->package->discoversMigrations) { - $this->discoverMigrations(); + $this->discoverPackageMigrations(); - return; + return $this; } $now = Carbon::now(); @@ -180,7 +191,7 @@ protected function bootMigrations() $appMigration = $this->generateMigrationName($migrationFileName, $now->addSecond()); // Support for the .stub file extension - if (! file_exists($vendorMigration)) { + if (!file_exists($vendorMigration)) { $vendorMigration .= '.stub'; } @@ -195,12 +206,14 @@ protected function bootMigrations() $this->loadMigrationsFrom($vendorMigration); } } + + return $this; } - protected function bootProviders() + protected function bootPackageProviders(): self { - if (! $this->package->publishableProviderName || ! $this->app->runningInConsole()) { - return; + if (!$this->package->publishableProviderName || !$this->app->runningInConsole()) { + return $this; } $providerName = $this->package->publishableProviderName; @@ -208,23 +221,27 @@ protected function bootProviders() $appProvider = base_path("app/Providers/{$providerName}.php"); $this->publishes([$vendorProvider => $appProvider], "{$this->package->shortName()}-provider"); + + return $this; } - protected function bootRoutes() + protected function bootPackageRoutes(): self { if (empty($this->package->routeFileNames)) { - return; + return $this; } foreach ($this->package->routeFileNames as $routeFileName) { $this->loadRoutesFrom("{$this->package->basePath('/../routes/')}{$routeFileName}.php"); } + + return $this; } - protected function bootTranslations() + protected function bootPackageTranslations(): self { - if (! $this->package->hasTranslations) { - return; + if (!$this->package->hasTranslations) { + return $this; } $vendorTranslations = $this->package->basePath('/../resources/lang'); @@ -243,12 +260,14 @@ protected function bootTranslations() "{$this->package->shortName()}-translations" ); } + + return $this; } - protected function bootViews() + protected function bootPackageViews(): self { - if (! $this->package->hasViews) { - return; + if (!$this->package->hasViews) { + return $this; } $namespace = $this->package->viewNamespace; @@ -260,12 +279,14 @@ protected function bootViews() if ($this->app->runningInConsole()) { $this->publishes([$vendorViews => $appViews], "{$this->packageView($namespace)}-views"); } + + return $this; } - protected function bootViewComponents() + protected function bootPackageViewComponents(): self { if (empty($this->package->viewComponents)) { - return; + return $this; } foreach ($this->package->viewComponents as $componentClass => $prefix) { @@ -278,31 +299,37 @@ protected function bootViewComponents() $this->publishes([$vendorComponents => $appComponents], "{$this->package->name}-components"); } + + return $this; } - protected function bootViewComposers() + protected function bootPackageViewComposers(): self { if (empty($this->package->viewComposers)) { - return; + return $this; } foreach ($this->package->viewComposers as $viewName => $viewComposer) { View::composer($viewName, $viewComposer); } + + return $this; } - protected function bootViewSharedData() + protected function bootPackageViewSharedData(): self { if (empty($this->package->sharedViewData)) { - return; + return $this; } foreach ($this->package->sharedViewData as $name => $value) { View::share($name, $value); } + + return $this; } - protected function discoverMigrations() + protected function discoverPackageMigrations(): void { $now = Carbon::now(); $migrationsPath = trim($this->package->migrationsPath, '/'); @@ -330,7 +357,7 @@ protected function discoverMigrations() protected function generateMigrationName(string $migrationFileName, Carbon $now): string { - $migrationsPath = 'migrations/'.dirname($migrationFileName).'/'; + $migrationsPath = 'migrations/' . dirname($migrationFileName) . '/'; $migrationFileName = basename($migrationFileName); $len = strlen($migrationFileName) + 4; @@ -341,7 +368,7 @@ protected function generateMigrationName(string $migrationFileName, Carbon $now) } foreach (glob(database_path("{$migrationsPath}*.php")) as $filename) { - if ((substr($filename, -$len) === $migrationFileName.'.php')) { + if ((substr($filename, -$len) === $migrationFileName . '.php')) { return $filename; } } @@ -349,6 +376,6 @@ protected function generateMigrationName(string $migrationFileName, Carbon $now) $timestamp = $now->format('Y_m_d_His'); $migrationFileName = Str::of($migrationFileName)->snake()->finish('.php'); - return database_path($migrationsPath.$timestamp.'_'.$migrationFileName); + return database_path($migrationsPath . $timestamp . '_' . $migrationFileName); } }