laravel-ready/license-server
Composer 安装命令:
composer require laravel-ready/license-server
包简介
License server for Laravel
README 文档
README
✅ Version Compatibility
| Laravel | License Server | PHP |
|---|---|---|
| 9.x | 1.2.x | ^8.0 |
| 10.x | 2.x | ^8.1 |
| 11.x | 3.x | ^8.2 |
📂 About
License Server package, which is a Laravel package that allows you to manage your Laravel applications license. You can use it with any product or service. License Server comes with the agnostic license management system, which allows you to manage your licenses in a simple and easy way. Just add license relation to any product model then you can work in your logic.
This package requires license-connector package. License Connector is client implementation for License Server. Package for the client makes a request to License Server and gets a response.
📋 Requirements
PHP
This package requires PHP 8.0 or higher. Also these extesnions are required:
If the above extensions already installed, you can enable them with php.ini.
Laravel
This package requires Laravel 8.x or higher. Other versions are ignored.
📦 Installation (for Host App)
Get via composer
composer require laravel-ready/license-server
Publish migrations and migrate
# publish migrations php artisan vendor:publish --tag=license-server-migrations # apply migrations php artisan migrate --path=/database/migrations/laravel-ready/license-server
Configs are very important. You can find them in license-server.php file. You should read all configs and configure for your needs.
# publish configs
php artisan vendor:publish --tag=license-server-configs
🗝️ Model Relations
Every license must-have product, because we need to know what it is licensed for. The client application will send this information to the License Server. Then we can check if the license is valid for given the product.
Product model can be any model that you want to be licensed. Add Licensable trait to your product model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; use LaravelReady\LicenseServer\Traits\Licensable; class Product extends Model { use HasFactory, Licensable; protected $table = 'products'; protected $fillable = [ 'name', 'description', 'price', 'image', ... ]; ... }
📌 Service Methods
Add in your namespace list:
use LaravelReady\LicenseServer\Services\LicenseService;
and product model
use App\Models\Product;
addLicense
First, we need to know licensing models. This package supports two types of licensing models: to Domain and to User. Both of them are valid. If you want to add license to domain, you must pass domain parameter. If you want to add license to user, you must pass userId parameter. Also, when you pass both of them, you will get domain license.
// get licensable product $product = Product::find(1); $user = User::find(1); // add license to domain $license = LicenseService::addLicense($product, 'example.com', $user->id); // add license to user $license = LicenseService::addLicense($product, null, $user->id); // with expiration in days (see configs for defaults) $license = LicenseService::addLicense($product, null, $user->id, 999); // with lifetime license (see configs for defaults) $license = LicenseService::addLicense($product, null, $user->id, null, true); // with trial license (see configs for defaults) $license = LicenseService::addLicense($product, null, $user->id, null, false, true);
- If you provide domain, then the license will be added to the domain. If you don't provide domain, then the license will be added to the user (in this case user id is required.).
- Other parameters are optional and do not forget to configure configs.
- This method returns
LaravelReady\LicenseServer\Models\Licensemodel. - All license keys are in UUID format.
getLicenseBy*
- getLicenseByKey: get license by license key.
LicenseService::getLicenseByKey(string $licenseKey)
- getLicenseByUserId: get license by user id and license key.
LicenseService::getLicenseByUserId(int $userId, string $licenseKey = null)
- getLicenseByDomain: get license by domain and license key.
LicenseService::getLicenseByDomain(string $domain, string $licenseKey = null)
checkLicenseStatus
// license key in uuid format $licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13"; // check license status $licenseStatus = LicenseService::checkLicenseStatus($licenseKey);
Returns "active", "inactive", "suspended", "expired", "invalid-license-key" and "no-license-found".
setLicenseStatus
// license key in uuid format $licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13"; // check license status $licenseStatus = LicenseService::setLicenseStatus($licenseKey, "suspended");
You can only set active, inactive, suspended status.
🛠️ Custom Controller
If you want to use own license validation controller you can integrate it easily.
Click to see the example!
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Event; use LaravelReady\LicenseServer\Models\License; use LaravelReady\LicenseServer\Events\LicenseChecked; class LicenseController extends Controller { /** * Custom license validation * * @param Request $request * @param License $license * * @return \Illuminate\Http\JsonResponse */ public function licenseValidate(Request $request, License $license) { // this controller is works under 'auth:sanctum' and 'ls-license-guard' middleware // in this case 'auth()->user()' will be is our license $_license = $license->select( 'domain', 'license_key', 'status', 'expiration_date', 'is_trial', 'is_lifetime' )->where([ ['id', '=', auth()->user()->id], ['is_trial', '!=', true] ])->first(); $data = $request->input(); // event will be fired after license is checked // this part depends to your logic, you can remove it or change it Event::dispatch(new LicenseChecked($_license, $data)); $_license->appent_some_data = 'some data and date now -> ' . now(); return $_license; } }
Then you need to register this custom controller in your config/license-server.php file.
Click to see the example!
/** * Custom controllers for License Server */ 'controllers' => [ /** * License validation controller * * You can use this controller to handle license validating * * See the documentation for more information. * */ 'license_validation' => [ App\Http\Controllers\LicenseController::class, 'licenseValidate' ] ]
🪢 Events
🪡 LicenseChecked Event
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.
php artisan make:listener LicenseCheckedListener --event=LicenseChecked
Add class LicenseChecked with LaravelReady\LicenseServer\Events\LicenseChecked namespace. You can retrieve custom data from event.
<?php namespace App\Listeners; use LaravelReady\LicenseServer\Events\LicenseChecked; class LicenseCheckedListener { public function __construct() { // } public function handle(LicenseChecked $event) { // $event->license, // $event->data, } }
Finally, you need to register this listener in your config/license-server.php file.
Click to see the example!
/** * Event listeners for License Server */ 'event_listeners' => [ /** * License checked event listener * * You can use this event to do something when a license is checked. * Also you can handle custom data with this listener. * * See the documentation for more information. * * Default: null */ 'license_checked' => App\Listeners\LicenseCheckedListener::class, ],
Ready to Use API
Ready to use API is included with simple resource methods. API endpoint is /api/license-server/licenses.
Domain Validation
License Server uses a cache to store the public tld list. See tld list at https://data.iana.org/TLD/tlds-alpha-by-domain.txt
The TLD list cache is will be stored at the storage/license-server/iana-tld-list.txt file and do not edit this file.
In development you may use domain like example.test etc. but you won't pass domain validation because the test is not valid tld.
⚠️ Warnings
- This package is under active development and is not yet stable. There may be some changes in later versions.
- Don't forget this package just provides management of licenses and product/customer communication.
- Please don't confuse it with ioncube or similar source code encryption tools.
laravel-ready/license-server 适用场景与选型建议
laravel-ready/license-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.25k 次下载、GitHub Stars 达 129, 最近一次更新时间为 2022 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「license」 「license-manager」 「license-server」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laravel-ready/license-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravel-ready/license-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laravel-ready/license-server 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for license generation
Cpanel Licensing Class
Fantastico Licensing Class
Cloudlinux Licensing Class
LiteSpeed Licensing Class
Parallels Licensing Class
统计信息
- 总下载量: 1.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 129
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-18