diff --git a/src/AnalyticsServiceProvider.php b/src/AnalyticsServiceProvider.php index f5cbaf3..6ccecc9 100644 --- a/src/AnalyticsServiceProvider.php +++ b/src/AnalyticsServiceProvider.php @@ -3,7 +3,9 @@ namespace Mattitja\Analytics; use Illuminate\Routing\Router; +use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; +use Mattitja\Analytics\Console\InstallCommand; use Mattitja\Analytics\Middleware\AnalyticsMiddleware; use Mattitja\Analytics\Services\AnalyticsClient; @@ -33,7 +35,14 @@ class AnalyticsServiceProvider extends ServiceProvider __DIR__.'/../config/mattitja-analytics.php' => config_path('mattitja-analytics.php'), ], 'mattitja-analytics-config'); + if ($this->app->runningInConsole()) { + $this->commands([ + InstallCommand::class, + ]); + } + $this->registerMiddleware(); + $this->warnIfSiteKeyMissing(); } /** @@ -46,4 +55,18 @@ class AnalyticsServiceProvider extends ServiceProvider $router->aliasMiddleware('mattitja-analytics', AnalyticsMiddleware::class); $router->pushMiddlewareToGroup('web', AnalyticsMiddleware::class); } + + /** + * Log a warning if site_key is not configured. + */ + private function warnIfSiteKeyMissing(): void + { + if ($this->app->runningInConsole()) { + return; + } + + if (empty(config('mattitja-analytics.site_key'))) { + Log::warning('Mattitja Analytics: site_key is not configured. Add MATTITJA_ANALYTICS_SITE_KEY to your .env file.'); + } + } } diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php new file mode 100644 index 0000000..f2688fd --- /dev/null +++ b/src/Console/InstallCommand.php @@ -0,0 +1,41 @@ +call('vendor:publish', [ + '--tag' => 'mattitja-analytics-config', + ]); + + $this->addEnvVariable(); + + $this->components->info('Mattitja Analytics installed successfully!'); + $this->components->warn('Add your site key to .env: MATTITJA_ANALYTICS_SITE_KEY=your-key'); + + return Command::SUCCESS; + } + + private function addEnvVariable(): void + { + $envPath = base_path('.env'); + + if (! file_exists($envPath)) { + return; + } + + $envContent = file_get_contents($envPath); + + if (! str_contains($envContent, 'MATTITJA_ANALYTICS_SITE_KEY')) { + file_put_contents($envPath, $envContent."\nMATTITJA_ANALYTICS_SITE_KEY=\n"); + } + } +} diff --git a/tests/Feature/InstallCommandTest.php b/tests/Feature/InstallCommandTest.php new file mode 100644 index 0000000..929ee80 --- /dev/null +++ b/tests/Feature/InstallCommandTest.php @@ -0,0 +1,65 @@ +envPath = base_path('.env'); + $this->configPath = config_path('mattitja-analytics.php'); + + if (File::exists($this->configPath)) { + File::delete($this->configPath); + } +}); + +afterEach(function () { + if (File::exists($this->configPath)) { + File::delete($this->configPath); + } +}); + +it('publishes the config file', function () { + $this->artisan('mattitja:install') + ->assertSuccessful(); + + expect(File::exists($this->configPath))->toBeTrue(); +}); + +it('displays success message', function () { + $this->artisan('mattitja:install') + ->expectsOutputToContain('Mattitja Analytics installed successfully') + ->assertSuccessful(); +}); + +it('displays warning about site key', function () { + $this->artisan('mattitja:install') + ->expectsOutputToContain('MATTITJA_ANALYTICS_SITE_KEY') + ->assertSuccessful(); +}); + +it('adds env variable if .env exists and key is missing', function () { + $originalContent = "APP_NAME=Test\n"; + File::put($this->envPath, $originalContent); + + $this->artisan('mattitja:install') + ->assertSuccessful(); + + $envContent = File::get($this->envPath); + + expect($envContent)->toContain('MATTITJA_ANALYTICS_SITE_KEY='); + + File::put($this->envPath, $originalContent); +}); + +it('does not duplicate env variable if already exists', function () { + $originalContent = "APP_NAME=Test\nMATTITJA_ANALYTICS_SITE_KEY=existing-key\n"; + File::put($this->envPath, $originalContent); + + $this->artisan('mattitja:install') + ->assertSuccessful(); + + $envContent = File::get($this->envPath); + + expect(substr_count($envContent, 'MATTITJA_ANALYTICS_SITE_KEY'))->toBe(1); + + File::put($this->envPath, "APP_NAME=Test\n"); +});