diff --git a/CHANGELOG.md b/CHANGELOG.md index f87eb12..df358fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added support for auto-aliasing module classes in tinker sessions + +## [2.2.0] - 2024-04-05 + +### Added + - The modules sync command now adds modules to PhpStorm exclude path, preventing double-registration of modules +## [2.1.0] - 2024-03-18 + ### Added - Added support for Laravel 11 diff --git a/src/Support/ModularServiceProvider.php b/src/Support/ModularServiceProvider.php index 362441e..22120ca 100644 --- a/src/Support/ModularServiceProvider.php +++ b/src/Support/ModularServiceProvider.php @@ -10,6 +10,7 @@ use Illuminate\Database\Console\Migrations\MigrateMakeCommand; use Illuminate\Database\Eloquent\Factories\Factory as EloquentFactory; use Illuminate\Database\Migrations\Migrator; +use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; use Illuminate\Translation\Translator; @@ -68,7 +69,7 @@ public function register(): void $this->registerLazily(Gate::class, [$this, 'registerPolicies']); // Look for and register all our commands in the CLI context - Artisan::starting(Closure::fromCallable([$this, 'registerCommands'])); + Artisan::starting(Closure::fromCallable([$this, 'onArtisanStart'])); } public function boot(): void @@ -275,6 +276,12 @@ protected function registerPolicies(Gate $gate): void }); } + protected function onArtisanStart(Artisan $artisan): void + { + $this->registerCommands($artisan); + $this->registerNamespacesInTinker(); + } + protected function registerCommands(Artisan $artisan): void { $this->autoDiscoveryHelper() @@ -288,6 +295,22 @@ protected function registerCommands(Artisan $artisan): void }); } + protected function registerNamespacesInTinker() + { + if (! class_exists('Laravel\\Tinker\\TinkerServiceProvider')) { + return; + } + + $namespaces = app(ModuleRegistry::class) + ->modules() + ->flatMap(fn(ModuleConfig $config) => $config->namespaces) + ->reject(fn($ns) => Str::endsWith($ns, ['Tests\\', 'Database\\Factories\\', 'Database\\Seeders\\'])) + ->values() + ->all(); + + Config::set('tinker.alias', array_merge($namespaces, Config::get('tinker.alias', []))); + } + protected function registerLazily(string $class_name, callable $callback): self { $this->app->resolving($class_name, Closure::fromCallable($callback));