din9xtr / source-context
Package for managing source context and proxy pattern implementation for PHP classes with enum support
v1.0.2
2026-02-28 21:16 UTC
Requires
- php: ^8.3
- psr/container: ^2.0
Requires (Dev)
- laravel/framework: ^11
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
Suggests
- laravel/framework: Required for Laravel integration
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