din9xtr/source-context 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

din9xtr/source-context

最新稳定版本:v1.0.2

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固