Package for managing source context and proxy pattern implementation for PHP classes with enum support
din9xtr/source-context is a Laravel package for package for managing source context and proxy pattern implementation for php classes with enum support.
It currently has 1 GitHub stars and 0 downloads on Packagist (latest version v1.0.2).
Install it with composer require din9xtr/source-context.
Discover more Laravel packages by din9xtr
or browse all Laravel packages to compare alternatives.
Last updated
A library for validating access to methods based on the source context (API, Web, CLI, etc.) using attributes and dynamic proxies
composer require din9xtr/source-context
#[AutoValidateSources]
class UserService implements SomeInterface // it's important
{
// ...
}
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
{
}
}
<?php
readonly class SomeInstance
{
public function __construct(
private SourceContextInterface $context,
) {
}
public function exec(): ?SourceInterface
{
$this->context->set(NoSource::CLI);
}
}
#[AutoValidateSources]
class YourService
{
// ...
}
#[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`
Supported sources:
Source::API
Source::WEB
Source::QUEUE
Source::CLI
Source::CRON
Source::TEST
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,
];
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);
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;
}
}
This project is open-source and available under the MIT License.
Copyright © 2026 Din9xtr