PHP code example of din9xtr / source-context

1. Go to this page and download the library: Download din9xtr/source-context library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

din9xtr / source-context example snippets


#[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
    {
    }
}



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
{
    // ...
}

Source::API
Source::WEB
Source::QUEUE
Source::CLI
Source::CRON
Source::TEST



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;
    }
}