tourze/access-token-contracts
Composer 安装命令:
composer require tourze/access-token-contracts
包简介
Access token management contracts and interfaces for PHP applications
README 文档
README
A lightweight PHP library that provides interfaces for access token management. This package defines contracts for implementing access token services in your applications.
Features
- 🔐 Access Token Interface: Standard interface for access token entities
- 🛠️ Service Contract: Well-defined service interface for token management
- 🏗️ Framework Agnostic: Works with any PHP 8.2+ application
- 🧪 Testable: Designed for easy unit testing and mocking
- 📦 Zero Dependencies: Only requires symfony/security-core for user interface
Installation
composer require tourze/access-token-contracts
Usage
Basic Implementation
First, implement the AccessTokenInterface for your access token entity:
<?php use Tourze\AccessTokenContracts\AccessTokenInterface; class MyAccessToken implements AccessTokenInterface { private string $token; private \DateTimeInterface $expiresAt; private UserInterface $user; // Implement your access token logic public function getToken(): string { return $this->token; } public function isExpired(): bool { return $this->expiresAt < new \DateTime(); } }
Then implement the TokenServiceInterface for token management:
<?php use Tourze\AccessTokenContracts\TokenServiceInterface; use Tourze\AccessTokenContracts\AccessTokenInterface; use Symfony\Component\Security\Core\User\UserInterface; class MyTokenService implements TokenServiceInterface { public function createToken( UserInterface $user, ?int $expiresIn = null, ?string $deviceInfo = null ): AccessTokenInterface { $token = new MyAccessToken(); $token->setUser($user); $token->setExpiresAt((new \DateTime())->add(new \DateInterval('PT' . ($expiresIn ?? 3600) . 'S'))); $token->setDeviceInfo($deviceInfo); $token->generateToken(); return $token; } }
Integration with Symfony
Register your service in your services.yaml:
services: App\Service\TokenService: arguments: - '@doctrine.orm.entity_manager' - '%env(default:3600:ACCESS_TOKEN_DEFAULT_TTL)%'
API Reference
AccessTokenInterface
Base interface for all access token implementations. This interface defines the contract that access token classes must follow.
TokenServiceInterface
Service interface for managing access tokens:
interface TokenServiceInterface { /** * Create a new access token for the given user * * @param UserInterface $user The user to create the token for * @param int|null $expiresIn Token expiration time in seconds (optional) * @param string|null $deviceInfo Device information for tracking (optional) * @return AccessTokenInterface The created access token */ public function createToken( UserInterface $user, ?int $expiresIn = null, ?string $deviceInfo = null ): AccessTokenInterface; }
Testing
The package is designed with testability in mind. You can easily mock the interfaces in your tests:
<?php use PHPUnit\Framework\TestCase; use Tourze\AccessTokenContracts\TokenServiceInterface; use Tourze\AccessTokenContracts\AccessTokenInterface; class TokenServiceTest extends TestCase { public function testCreateToken() { $service = $this->createMock(TokenServiceInterface::class); $user = $this->createMock(UserInterface::class); $token = $this->createMock(AccessTokenInterface::class); $service->expects($this->once()) ->method('createToken') ->with($user, 3600, 'mobile-device') ->willReturn($token); $result = $service->createToken($user, 3600, 'mobile-device'); $this->assertInstanceOf(AccessTokenInterface::class, $result); } }
Requirements
- PHP 8.2 or higher
- Symfony Security Core ^7.3
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is licensed under the MIT License. See the LICENSE file for details.
Changelog
See CHANGELOG.md for a list of changes in each version.
tourze/access-token-contracts 适用场景与选型建议
tourze/access-token-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.68k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/access-token-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/access-token-contracts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-31