PHP code example of drmmr763 / laravel-asyncapi

1. Go to this page and download the library: Download drmmr763/laravel-asyncapi 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 / laravel-asyncapi example snippets


return [
    'version' => env('ASYNCAPI_VERSION', '3.0.0'),
    'default_content_type' => env('ASYNCAPI_DEFAULT_CONTENT_TYPE', 'application/json'),
    'scan_paths' => [
        app_path(),
    ],
    'output_path' => base_path('asyncapi'),
    'export_formats' => ['json', 'yaml'],
    'default_export_format' => env('ASYNCAPI_EXPORT_FORMAT', 'yaml'),
    'pretty_print' => env('ASYNCAPI_PRETTY_PRINT', true),
    'cache' => [
        'enabled' => env('ASYNCAPI_CACHE_ENABLED', true),
        'ttl' => env('ASYNCAPI_CACHE_TTL', 3600),
        'key' => 'asyncapi_annotations',
    ],
];



namespace App\AsyncApi;

use AsyncApi\Attributes\AsyncApi;
use AsyncApi\Attributes\Info;
use AsyncApi\Attributes\Server;
use AsyncApi\Attributes\Channel;
use AsyncApi\Attributes\Operation;

#[AsyncApi(
    asyncapi: '3.0.0',
    info: new Info(
        title: 'My Application API',
        version: '1.0.0',
        description: 'AsyncAPI specification for my application'
    ),
    servers: [
        'production' => new Server(
            host: 'api.example.com',
            protocol: 'kafka',
            description: 'Production Kafka server'
        )
    ]
)]
class MyAsyncApiSpec
{
}

use Drmmr763\AsyncApi\Facades\AsyncApi;

// Build the specification
$spec = AsyncApi::build();

// Export to JSON
$json = AsyncApi::toJson();

// Export to YAML
$yaml = AsyncApi::toYaml();

// Export to file
AsyncApi::exportToFile('asyncapi.yaml', 'yaml');
AsyncApi::exportToFile('asyncapi.json', 'json');

// Scan for annotations
$annotations = AsyncApi::scan();

use Drmmr763\AsyncApi\AsyncApi;

class MyController
{
    public function __construct(private AsyncApi $asyncApi)
    {
    }

    public function generateSpec()
    {
        $specification = $this->asyncApi->build();
        return response()->json($specification);
    }
}

use AsyncApi\Attributes\Message;
use AsyncApi\Attributes\Schema;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

#[Message(
    name: 'UserRegistered',
    title: 'User Registered Event',
    summary: 'Broadcast when a new user registers',
    payload: new Schema(
        type: 'object',
        properties: [
            'event' => new Schema(type: 'string', example: 'user.registered'),
            'data' => new Schema(
                type: 'object',
                properties: [
                    'user' => new Schema(/* user schema */),
                    'ip_address' => new Schema(type: 'string')
                ]
            )
        ]
    )
)]
class UserRegistered implements ShouldBroadcast
{
    // Your broadcast event implementation
}

// Define reusable schemas
class CommonSchemas
{
    public const USER = '#/components/schemas/User';
    public const ORDER = '#/components/schemas/Order';
    public const PRODUCT = '#/components/schemas/Product';
}

// Reference them in your messages
#[AsyncApi(
    channels: new Channels(
        channels: [
            'orders/created' => new Channel(
                messages: [
                    'orderCreated' => new Message(
                        payload: new Schema(
                            type: 'object',
                            properties: [
                                'order' => new Reference(ref: CommonSchemas::ORDER),
                                'user' => new Reference(ref: CommonSchemas::USER)
                            ]
                        )
                    )
                ]
            )
        ]
    ),
    components: new Components(
        schemas: [
            'User' => new Schema(/* user schema definition */),
            'Order' => new Schema(
                properties: [
                    'items' => new Schema(
                        type: 'array',
                        items: new Reference(ref: CommonSchemas::PRODUCT)
                    )
                ]
            )
        ]
    )
)]
class MyAsyncApiSpec {}

// Serve AsyncAPI specification
Route::get('/asyncapi.json', [AsyncApiController::class, 'getSpecJson']);
Route::get('/asyncapi.yaml', [AsyncApiController::class, 'getSpecYaml']);

// Render interactive documentation
Route::get('/asyncapi/docs', [AsyncApiController::class, 'renderDocs']);

// app/AsyncApi/Schemas/UserSchemas.php
class UserSchemas
{
    public const USER = '#/components/schemas/User';
    public const USER_PROFILE = '#/components/schemas/UserProfile';
}

// app/AsyncApi/Schemas/OrderSchemas.php
class OrderSchemas
{
    public const ORDER = '#/components/schemas/Order';
    public const ORDER_ITEM = '#/components/schemas/OrderItem';
}

// app/AsyncApi/MainSpec.php
#[AsyncApi(
    // Reference schemas from different classes
    components: new Components(
        schemas: [
            'User' => new Schema(/* ... */),
            'Order' => new Schema(/* ... */)
        ]
    )
)]
class MainSpec {}

// config/asyncapi.php
return [
    'scan_paths' => [
        app_path('Events'),
        app_path('AsyncApi'),
        app_path('Broadcasting'),
    ],
];

// .env
ASYNCAPI_CACHE_ENABLED=true
ASYNCAPI_CACHE_TTL=3600
bash
php artisan vendor:publish --tag="asyncapi-config"
bash
php artisan asyncapi:generate
bash
php artisan asyncapi:generate --format=json
php artisan asyncapi:generate --format=yaml
bash
php artisan asyncapi:generate --output=asyncapi.yaml
php artisan asyncapi:generate --output=asyncapi.json --format=json
bash
php artisan asyncapi:export asyncapi.yaml
php artisan asyncapi:export asyncapi.json --format=json
bash
php artisan asyncapi:validate
bash
php artisan asyncapi:list
bash
php artisan asyncapi:list --type=Channel
php artisan asyncapi:list --type=Operation
bash
php artisan cache:forget asyncapi_annotations