feat: added install script
This commit is contained in:
3 files changed
+129
No files matched your search
@@ -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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Mattitja\Analytics\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
protected $signature = 'mattitja:install';
|
||||
|
||||
protected $description = 'Install Mattitja Analytics package';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->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");
|
||||
});
|
||||
Reference in new issue
Block a user