Skip to content

Commit ef5e12e

Browse files
committed
Add custom data handler event
- Added custom data handle event (LicenseChecked) and listener config. - Moved license check action to own controller. - Improved source code.
1 parent fd90748 commit ef5e12e

12 files changed

+178
-18
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,43 @@ $licenseStatus = LicenseService::setLicenseStatus($licenseKey, "suspended");
143143

144144
You can only set `active`, `inactive`, `suspended` status.
145145

146+
## 🪢 Events
147+
148+
### 🪡 LicenseChecked Event
149+
150+
You can send custom data with connector and on the license server-side, you can catch this custom data. First you need to create a listener for this event.
151+
152+
```bash
153+
php artisan make:listener LicenseCheckedListener --event=LicenseChecked
154+
```
155+
156+
Add class `LicenseChecked` with `LaravelReady\LicenseServer\Events\LicenseChecked` namespace.
157+
158+
Finally, you can retrieve custom data from event.
159+
160+
```php
161+
<?php
162+
163+
namespace App\Listeners;
164+
165+
use LaravelReady\LicenseServer\Events\LicenseChecked;
166+
167+
class LicenseCheckedListener
168+
{
169+
public function __construct()
170+
{
171+
//
172+
}
173+
174+
public function handle(LicenseChecked $event)
175+
{
176+
// $event->license,
177+
// $event->data,
178+
}
179+
}
180+
181+
```
182+
146183
## Ready to Use API
147184

148185
Ready to use API is included with simple resource methods. API endpoint is `/api/license-server/licenses`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "License server for Laravel",
44
"type": "library",
55
"license": "MIT",
6-
"version": "1.0.3",
6+
"version": "1.1.3",
77
"keywords": [
88
"laravel",
99
"license",

config/license-server.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,21 @@
6464
'auth:sanctum',
6565
'throttle:api',
6666
],
67+
68+
/**
69+
* Event listeners for License Server
70+
*/
71+
'listeners' => [
72+
/**
73+
* License checked event listener
74+
*
75+
* You can use this event to do something when a license is checked.
76+
* Also you can handle custom data with this listener.
77+
*
78+
* See the documentation for more information.
79+
*
80+
* Default: null
81+
*/
82+
'license_checked' => null
83+
]
6784
];

routes/api-public.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

3+
use Illuminate\Http\Request;
34
use Illuminate\Support\Facades\Route;
45

5-
use LaravelReady\LicenseServer\Services\LicenseService;
66
use LaravelReady\LicenseServer\Http\Controllers\Api\AuthController;
7+
use LaravelReady\LicenseServer\Http\Controllers\Api\LicenseValidateController;
78

89
/**
910
* Public routes for License Server connector package
@@ -25,13 +26,5 @@
2526
Route::middleware([
2627
'auth:sanctum',
2728
'ls-license-guard',
28-
])->get('license', function () {
29-
$license = auth()->user();
30-
31-
unset($license['id']);
32-
unset($license['user_id']);
33-
unset($license['created_by']);
34-
35-
return $license;
36-
});
29+
])->post('license', [LicenseValidateController::class, 'licenseValidate']);
3730
});

src/EventServiceProvider.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace LaravelReady\LicenseServer;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
7+
8+
use LaravelReady\LicenseServer\Events\LicenseChecked;
9+
use LaravelReady\LicenseServer\Exceptions\ListenerNotFoundException;
10+
11+
final class EventServiceProvider extends ServiceProvider
12+
{
13+
public function __construct($app)
14+
{
15+
parent::__construct($app);
16+
17+
$eventListeners = [];
18+
19+
$licenseCheckedListener = Config::get('license-server.listeners.license_checked');
20+
21+
if ($licenseCheckedListener) {
22+
if (!class_exists($licenseCheckedListener)) {
23+
throw new ListenerNotFoundException('LicenseChecked listener class not found');
24+
}
25+
26+
$eventListeners[LicenseChecked::class] = [
27+
$licenseCheckedListener
28+
];
29+
}
30+
31+
$this->listen = $eventListeners;
32+
}
33+
34+
/**
35+
* Register any events for your application.
36+
*
37+
* @return void
38+
*/
39+
public function boot()
40+
{
41+
parent::boot();
42+
}
43+
}

src/Events/LicenseChecked.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace LaravelReady\LicenseServer\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
use LaravelReady\LicenseServer\Models\License;
10+
11+
class LicenseChecked
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(License $license, array|null $data = [])
16+
{
17+
$this->license = $license;
18+
$this->data = $data;
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace LaravelReady\LicenseServer\Exceptions;
4+
5+
use Exception;
6+
7+
final class ListenerNotFoundException extends Exception
8+
{
9+
}

src/Http/Controllers/Api/AuthController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use LaravelReady\LicenseServer\Models\IpAddress;
88
use LaravelReady\UltimateSupport\Support\IpSupport;
99
use LaravelReady\LicenseServer\Services\LicenseService;
10-
use LaravelReady\LicenseServer\Http\Controllers\ApiBaseController;
10+
use LaravelReady\LicenseServer\Http\Controllers\BaseController;
1111

12-
class AuthController extends ApiBaseController
12+
class AuthController extends BaseController
1313
{
1414
/**
1515
* Login with sanctum

src/Http/Controllers/Api/LicenseController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use LaravelReady\LicenseServer\Models\License;
88
use LaravelReady\LicenseServer\Http\Requests\LicenseUpdateRequest;
9-
use LaravelReady\LicenseServer\Http\Controllers\ApiBaseController;
9+
use LaravelReady\LicenseServer\Http\Controllers\BaseController;
1010

11-
class LicenseController extends ApiBaseController
11+
class LicenseController extends BaseController
1212
{
1313
/**
1414
* Display a listing of the resource.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace LaravelReady\LicenseServer\Http\Controllers\Api;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Event;
7+
8+
use LaravelReady\LicenseServer\Models\License;
9+
use LaravelReady\LicenseServer\Events\LicenseChecked;
10+
use LaravelReady\LicenseServer\Http\Controllers\BaseController;
11+
12+
class LicenseValidateController extends BaseController
13+
{
14+
/**
15+
* Validate given license
16+
*
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function licenseValidate(Request $request, License $license)
20+
{
21+
$_license = $license->select(
22+
'domain',
23+
'license_key',
24+
'status',
25+
'expiration_date',
26+
'is_trial',
27+
'is_lifetime',
28+
'deleted_at',
29+
'created_at',
30+
'updated_at'
31+
)->where('id', auth()->user()->id)->first();
32+
33+
$data = $request->input();
34+
35+
Event::dispatch(new LicenseChecked($_license, $data));
36+
37+
return $_license;
38+
}
39+
}

0 commit comments

Comments
 (0)