din9xtr/source-context
Composer 安装命令:
composer require din9xtr/source-context
包简介
Package for managing source context and proxy pattern implementation for PHP classes with enum support
README 文档
README
A library for validating access to methods based on the source context (API, Web, CLI, etc.) using attributes and dynamic proxies
Features
- Source Validation — Restrict access to methods by source type
- Dynamic Proxies — Automatic generation of proxy classes
- Attributes — Simple declarative configuration via PHP attributes
- Laravel Integration — Ready-to-use Laravel integration
- Multiple Contexts — Support for various context implementations
📦 Installation
composer require din9xtr/source-context
🎯 Quick Start
1. Add an attribute to the class
#[AutoValidateSources] class UserService implements SomeInterface // it's important { // ... }
2. Protect methods with the AllowedSources attribute
class UserService implements SomeInterface // it's important { #[AllowedSources([Source::API, Source::WEB])] public function createUser(array $data): User { } #[AllowedSources([Source::CLI, Source::CRON])] public function listUsers(): void { } }
3. Set the source context
<?php readonly class SomeInstance { public function __construct( private SourceContextInterface $context, ) { } public function exec(): ?SourceInterface { $this->context->set(NoSource::CLI); } }
📋 Components
Attributes
- AutoValidateSources — Marks the class for automatic source validation
#[AutoValidateSources] class YourService { // ... }
- AllowedSources — Restricts access to the method to certain sources
#[AllowedSources([Source::API, Source::WEB])] public function someMethod(): void { // ... }
Throws exceptions if the source is not in
#[AllowedSources([])]
RuntimeException: Method App\Service\UserService::createUser not allowed for source `name`
Source Contexts
- InMemorySourceContext — In-memory context for request-response applications
- StaticSourceContext — Static context
Enum Source
Supported sources:
Source::API Source::WEB Source::QUEUE Source::CLI Source::CRON Source::TEST
Configuration
Laravel Integration
The service provider automatically registers all classes with the AutoValidateSources attribute
Or you can specify them in the source-context.php configuration file
<?php declare(strict_types=1); return [ 'auto_validate_classes' => [ \YourApp\YourService::class, ], 'scan_chunk_size' => 2048, ];
Custom integration
use App\SourceContext\Contract\SourceContextInterface; use App\SourceContext\Implementation\InMemorySourceContext; $context = new InMemorySourceContext(); $context->set(Source::API); $service = new UserService(); $proxy = ProxyGenerator::createProxy($service, $context);
Extension
Adding new sources
enum YourSource: string implements SourceInterface { case API = 'api'; case WEB = 'web'; case NEW_SOURCE = 'new_source'; public function is(self|string|SourceInterface $other): bool { return $this->value === (string) $other; } }
License
This project is open-source and available under the MIT License.
Copyright © 2026 Din9xtr
din9xtr/source-context 适用场景与选型建议
din9xtr/source-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「source」 「Context」 「enum」 「laravel」 「source-context」 「laravel context」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 din9xtr/source-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 din9xtr/source-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 din9xtr/source-context 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat Context for testing Symfony Api
Enum type behavior for Yii2 based on class constants
A Laravel 5 Package for Using & Working With Emojis
Context support (request, session and application scope) for n2n framework.
A Laravel 5 Package Provider to Identify/detect a user's browser, device, operating system and Language
A PHP Abstract Enum Class
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-28