Skip to content

Commit 91976f9

Browse files
authored
Merge pull request #52 from me-shaon/refactor-service-provider
refactor: Modernize service provider using Laravel Package Tools features
2 parents 136c455 + 7262ec0 commit 91976f9

File tree

7 files changed

+79
-219
lines changed

7 files changed

+79
-219
lines changed

README.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,30 @@ Built with performance and privacy in mind, the package offers intelligent bot d
2222
- Laravel 10.0 or higher
2323
- MySQL 5.7+ / PostgreSQL 10+ / SQLite 3.8+
2424

25-
### Install via Composer
25+
### Quick Installation (Recommended) ✨
2626

27-
Install the package using Composer:
27+
Get up and running in just two commands:
2828

2929
```bash
30+
# Install the package
3031
composer require me-shaon/laravel-request-analytics
31-
```
3232

33-
### Automated Setup (Recommended) ✨
33+
# Run the interactive installer
34+
php artisan request-analytics:install
35+
```
3436

35-
For the fastest installation, use the interactive `setup` command. It handles all the boilerplate configuration in one step. **(This method is the recommended way to get started and replaces the manual steps outlined below)**
37+
This interactive installer will:
38+
- Welcome you with helpful information about the package
39+
- Automatically publish configuration files, assets, and views
40+
- Publish and optionally run database migrations
41+
- Optionally prompt you to star our GitHub repository
42+
- Provide next steps and dashboard access information
3643

37-
```bash
38-
php artisan laravel-request-analytics:setup
39-
```
44+
After running the installer, your analytics dashboard will be ready at `/analytics`!
4045

41-
This single command publishes the necessary configuration, assets, and migration files. It will then interactively prompt you to run the database migrations, providing a safe option that only affects this package's tables.
46+
### Manual Setup (Advanced)
4247

43-
### Manual Setup
48+
For users who prefer manual control over the installation process or need custom configurations:
4449

4550
#### Database Setup
4651

@@ -140,6 +145,21 @@ Optionally, publish the views for customization:
140145
php artisan vendor:publish --tag="request-analytics-views"
141146
```
142147

148+
#### Publishing Options
149+
150+
The package supports granular publishing of individual components:
151+
152+
```bash
153+
# Publish specific components
154+
php artisan vendor:publish --tag="request-analytics-config"
155+
php artisan vendor:publish --tag="request-analytics-migrations"
156+
php artisan vendor:publish --tag="request-analytics-assets"
157+
php artisan vendor:publish --tag="request-analytics-views"
158+
159+
# Or publish everything at once
160+
php artisan vendor:publish --provider="MeShaon\RequestAnalytics\RequestAnalyticsServiceProvider"
161+
```
162+
143163
### Automated Data Pruning
144164

145165
The package includes automatic data cleanup to manage database size. Configure pruning in your scheduler:
@@ -205,6 +225,26 @@ protected function schedule(Schedule $schedule): void
205225

206226
## Usage
207227

228+
### Getting Started
229+
230+
After installation, the package automatically starts tracking requests based on your configuration. Here's what happens next:
231+
232+
1. **Dashboard Access**: Visit `/analytics` (or your configured pathname) to view the analytics dashboard
233+
2. **Automatic Tracking**: Request data is captured automatically for web and API routes
234+
235+
### Quick Commands
236+
237+
```bash
238+
# Run the interactive installer
239+
php artisan request-analytics:install
240+
241+
# View available analytics commands
242+
php artisan list | grep analytics
243+
244+
# Clear old analytics data (if pruning is enabled)
245+
php artisan model:prune --model="MeShaon\RequestAnalytics\Models\RequestAnalytics"
246+
```
247+
208248
## Configuration Options
209249

210250
### Route Configuration

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^8.1 | ^8.2 | ^8.3",
2020
"illuminate/contracts": "^10.0 | ^11.0 | ^12.0",
21-
"spatie/laravel-package-tools": "^1.14.0"
21+
"spatie/laravel-package-tools": "^1.16.0"
2222
},
2323
"require-dev": {
2424
"laravel/pint": "^1.0",

src/Commands/RequestAnalyticsCommand.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Commands/SetupCommand.php

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/RequestAnalyticsServiceProvider.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,51 @@
33
namespace MeShaon\RequestAnalytics;
44

55
use Illuminate\Contracts\Http\Kernel;
6-
use MeShaon\RequestAnalytics\Commands\RequestAnalyticsCommand;
7-
use MeShaon\RequestAnalytics\Commands\SetupCommand;
86
use MeShaon\RequestAnalytics\Http\Middleware\AnalyticsDashboardMiddleware;
97
use MeShaon\RequestAnalytics\Http\Middleware\APIRequestCapture;
108
use MeShaon\RequestAnalytics\Http\Middleware\WebRequestCapture;
9+
use Spatie\LaravelPackageTools\Commands\InstallCommand;
1110
use Spatie\LaravelPackageTools\Package;
1211
use Spatie\LaravelPackageTools\PackageServiceProvider;
1312

1413
class RequestAnalyticsServiceProvider extends PackageServiceProvider
1514
{
1615
public function configurePackage(Package $package): void
1716
{
18-
// Manually publish migrations with proper timestamp
19-
$this->publishes([
20-
__DIR__.'/../database/migrations/create_request_analytics_table.php' => database_path('migrations/'.date('Y_m_d_His').'_create_request_analytics_table.php'),
21-
], 'laravel-request-analytics-migrations');
22-
23-
// Manually publish config with correct tag
24-
$this->publishes([
25-
__DIR__.'/../config/request-analytics.php' => config_path('request-analytics.php'),
26-
], 'laravel-request-analytics-config');
27-
28-
// Manually publish assets with correct tag
29-
$this->publishes([
30-
__DIR__.'/../resources/assets' => public_path('/'),
31-
], 'laravel-request-analytics-assets');
32-
3317
$package
3418
->name('laravel-request-analytics')
19+
->hasConfigFile()
3520
->hasViews()
36-
->hasRoute('web')
37-
->hasRoute('api')
38-
->hasCommand(RequestAnalyticsCommand::class);
21+
->hasRoutes(['web', 'api'])
22+
->hasAssets()
23+
->hasMigrations(['create_request_analytics_table', 'add_indexes_to_request_analytics_table'])
24+
->hasInstallCommand(function (InstallCommand $command): void {
25+
$command
26+
->startWith(function (InstallCommand $command): void {
27+
$command->info('Installing Laravel Request Analytics...');
28+
$command->info('This package will help you track and analyze your application requests.');
29+
})
30+
->publishConfigFile()
31+
->publishMigrations()
32+
->publishAssets()
33+
->askToRunMigrations()
34+
->endWith(function (InstallCommand $command): void {
35+
$command->info('Laravel Request Analytics has been installed successfully!');
36+
$command->info('You can now visit /analytics to view your dashboard.');
37+
$command->info('Check the documentation for configuration options.');
38+
})
39+
->askToStarRepoOnGitHub('me-shaon/laravel-request-analytics');
40+
});
41+
}
3942

43+
public function packageRegistered(): void
44+
{
4045
$this->registerMiddlewareAsAliases();
4146
}
4247

43-
public function boot(): void
48+
public function packageBooted(): void
4449
{
45-
parent::boot();
4650
$this->pushMiddlewareToPipeline();
47-
48-
if ($this->app->runningInConsole()) {
49-
$this->commands([
50-
SetupCommand::class,
51-
]);
52-
}
5351
}
5452

5553
private function registerMiddlewareAsAliases(): void

tests/Feature/Api/AnalyticsApiTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public function it_validates_end_date_before_start_date(): void
126126
#[Test]
127127
public function it_returns_empty_overview_data(): void
128128
{
129+
\Illuminate\Support\Facades\Cache::flush();
130+
129131
$response = $this->getJson(route('request-analytics.api.overview'));
130132

131133
$response->assertOk()

tests/Feature/Commands/SetupCommandTest.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)