PHP code example of mrthito / microservice

1. Go to this page and download the library: Download mrthito/microservice 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/ */

    

mrthito / microservice example snippets


use MrThito\MicroService\Contracts\MicroServiceConfig;
use MrThito\MicroService\Support\Env;
use MrThito\MicroService\Support\Manifest;

final readonly class Config implements MicroServiceConfig
{
    public static function fromEnvironment(string $basePath): self
    {
        $manifest = Manifest::load($basePath);

        return new self(
            serviceName: $manifest['name'],
            // ... map Env::getString(), Env::getInt(), etc.
        );
    }

    // Implement MicroServiceConfig methods...
}

use MrThito\MicroService\Bootstrap;
use MrThito\MicroService\Queue\RedisQueueListener;
use MrThito\MicroService\Security\SignedEventVerifier;
use MrThito\MicroService\Support\BaseConfigValidator;
use MrThito\MicroService\Support\Logger;

$config = Bootstrap::boot('/path/to/service', fn () => Config::fromEnvironment('/path/to/service'));
BaseConfigValidator::validate($config);

$logger = new Logger($config->logLevel());

$verifier = new SignedEventVerifier(
    expectedEvent: 'order.created',
    signingSecret: $config->signingSecret(),
    eventMaxAgeSeconds: $config->eventMaxAgeSeconds(),
    payloadValidator: static fn (array $payload): array => [
        'order_id' => (int) $payload['order_id'],
    ],
);

$listener = new RedisQueueListener(
    redisConfig: $config->redis(),
    verifier: $verifier,
    processor: $orderProcessor,
    logger: $logger,
);

$listener->listen();

use MrThito\MicroService\Http\Server;

$config = Bootstrap::boot('/path/to/service', fn () => Config::fromEnvironment('/path/to/service'));

(new Server($config))->run();

$server = new Server($config);
$server->router()->get('/metrics', static fn (): array => ['uptime' => time()]);

$server->run();