PHP code example of drmmr763 / php-asyncapi-annotations
1. Go to this page and download the library: Download drmmr763/php-asyncapi-annotations 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/ */
drmmr763 / php-asyncapi-annotations example snippets
use AsyncApi\Attributes\AsyncApi;
use AsyncApi\Attributes\Info;
use AsyncApi\Attributes\Contact;
use AsyncApi\Attributes\License;
use AsyncApi\Attributes\Server;
use AsyncApi\Attributes\Channel;
use AsyncApi\Attributes\Message;
use AsyncApi\Attributes\Schema;
#[AsyncApi(
asyncapi: '3.0.0',
info: new Info(
title: 'User Service API',
version: '1.0.0',
description: 'API for managing user events',
contact: new Contact(
name: 'API Support',
email: '[email protected]',
url: 'https://example.com/support'
),
license: new License(
name: 'MIT',
url: 'https://opensource.org/licenses/MIT'
)
),
defaultContentType: 'application/json'
)]
class UserServiceApi
{
// Your API implementation
}
use AsyncApi\Attributes\Server;
use AsyncApi\Attributes\ServerVariable;
use AsyncApi\Attributes\Servers;
#[Server(
host: '{environment}.example.com:9092',
protocol: 'kafka',
protocolVersion: '2.8.0',
description: 'Kafka broker for user events',
variables: [
'environment' => new ServerVariable(
enum: ['dev', 'staging', 'prod'],
default: 'dev',
description: 'Environment name'
)
]
)]
class KafkaServer
{
}
use AsyncApi\Attributes\Message;
use AsyncApi\Attributes\Schema;
#[Message(
name: 'UserCreated',
title: 'User Created Event',
summary: 'Event emitted when a new user is created',
contentType: 'application/json',
payload: new Schema(
type: 'object',
ser email address',
format: 'email'
),
'name' => new Schema(
type: 'string',
description: 'User full name'
),
'createdAt' => new Schema(
type: 'string',
description: 'Timestamp when user was created',
format: 'date-time'
)
]
)
)]
class UserCreatedMessage
{
}
use AsyncApi\Attributes\Channel;
use AsyncApi\Attributes\Operation;
use AsyncApi\Attributes\Reference;
#[Channel(
address: 'user.events.{eventType}',
description: 'Channel for user-related events',
parameters: new Parameters(
parameters: [
'eventType' => new Parameter(
description: 'Type of user event',
enum: ['created', 'updated', 'deleted']
)
]
)
)]
class UserEventsChannel
{
}
#[Operation(
action: 'send',
channel: new Reference(ref: '#/channels/userEvents'),
summary: 'Send user events',
messages: [
new Reference(ref: '#/components/messages/UserCreated')
]
)]
class SendUserEvent
{
}
use AsyncApi\Attributes\SecurityScheme;
use AsyncApi\Attributes\OAuthFlows;
use AsyncApi\Attributes\OAuthFlow;
#[SecurityScheme(
type: 'oauth2',
description: 'OAuth2 authentication',
flows: new OAuthFlows(
authorizationCode: new OAuthFlow(
authorizationUrl: 'https://example.com/oauth/authorize',
tokenUrl: 'https://example.com/oauth/token',
scopes: [
'read:users' => 'Read user information',
'write:users' => 'Create and update users'
]
)
)
)]
class OAuth2Security
{
}
use AsyncApi\Attributes\Components;
#[Components(
schemas: [
'User' => new Schema(
type: 'object',
properties: [
'id' => new Schema(type: 'string', format: 'uuid'),
'email' => new Schema(type: 'string', format: 'email'),
'name' => new Schema(type: 'string')
]
)
],
messages: [
'UserCreated' => new Message(
payload: new Reference(ref: '#/components/schemas/User')
)
]
)]
class ApiComponents
{
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.