PHP code example of andrebhas / laravel-brick

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

    

andrebhas / laravel-brick example snippets


namespace Bricks\Hotel\Bridge;

use AndreBhas\Brick\Brick\Bridge\Contracts\Bridgeable;

class HotelBridge implements Bridgeable
{
    public static function getBridgeName(): string
    {
        return 'Hotel';
    }

    public static function getExposedMethods(): array
    {
        return ['getById', 'checkAvailability'];
    }

    public function getById(int $id): array
    {
        // Fetch hotel data...
        return ['id' => $id, 'name' => 'Grand Hotel'];
    }

    public function checkAvailability(int $hotelId, string $date): bool
    {
        return true;
    }
}

use AndreBhas\Brick\Brick\Bridge\InternalGateway;

$gateway = app(InternalGateway::class);

// Goes through middleware pipeline (logging, circuit breaker, etc.)
$hotel = $gateway->call('Hotel', 'getById', 42);

// Dispatch to queue, get a job ID
$jobId = $gateway->callAsync('Hotel', 'getById', 42);

// Later, poll for the result
$result = $gateway->getAsyncResult($jobId);
// ['status' => 'success', 'result' => [...], 'completed_at' => '...']
// or
// ['status' => 'pending', 'queued_at' => '...']
// or
// ['status' => 'failed', 'error' => '...']

'circuit_breaker' => [
    'default' => [
        'threshold'              => 5,  // failures before opening
        'timeout'                => 30, // seconds before half-open
        'half_open_max_attempts' => 3,
    ],
],

use AndreBhas\Brick\Brick\Testing\BridgeFake;
use AndreBhas\Brick\Brick\Bridge\InternalGateway;

$fake = new BridgeFake(
    container: app(),
    cache: app('cache')->store(),
    queue: app('queue'),
);

// Register a fake response
$fake->shouldReceive('Hotel', 'getById', fn ($id) => ['id' => $id, 'name' => 'Test Hotel']);

// Swap in the container
app()->instance(InternalGateway::class, $fake);

// ... run your code ...

// Assert calls
$fake->assertCalled('Hotel', 'getById', 1);
$fake->assertNotCalled('Hotel', 'delete');
$fake->assertNothingCalled(); // if no calls expected

use AndreBhas\Brick\Brick\Testing\CircuitBreakerFake;
use AndreBhas\Brick\Brick\Bridge\CircuitBreaker;

$fake = new CircuitBreakerFake();
$fake->forceOpen('Hotel');    // simulate open circuit
$fake->forceHalfOpen('Payment');
$fake->forceClose('Auth');

app()->instance(CircuitBreaker::class, $fake);

// config/brick.php

return [
    // Root directory for all modules
    'modules_path' => base_path('bricks'),

    'bridge' => [
        'enabled' => env('BRICK_BRIDGE_ENABLED', true),

        // Middleware applied to every synchronous bridge call
        'middlewares' => [
            \AndreBhas\Brick\Brick\Bridge\Middleware\LoggingMiddleware::class,
            \AndreBhas\Brick\Brick\Bridge\Middleware\CircuitBreakerMiddleware::class,
            // \AndreBhas\Brick\Brick\Bridge\Middleware\CacheMiddleware::class,
        ],

        'circuit_breaker' => [
            'default' => [
                'threshold' => 5,
                'timeout'   => 30,
                'half_open_max_attempts' => 3,
            ],
        ],

        'queue'       => env('BRICK_BRIDGE_QUEUE', 'default'),
        'cache_store' => env('BRICK_BRIDGE_CACHE', null),
        'cache_ttl'   => 3600,
    ],

    'assets' => [
        'publish_method' => env('BRICK_ASSETS_METHOD', 'copy'), // 'copy' or 'symlink'
        'public_path'    => public_path('bricks'),
    ],

    'dependencies' => [
        'auto_enable' => env('BRICK_AUTO_ENABLE_DEPENDENCIES', false),
    ],
];

namespace App\Brick\Middleware;

use Closure;

class RateLimitMiddleware
{
    public function handle(Closure $next, string $bridge, string $method, array $args): mixed
    {
        // Rate limiting logic...
        return $next($bridge, $method, $args);
    }
}

'middlewares' => [
    \AndreBhas\Brick\Brick\Bridge\Middleware\LoggingMiddleware::class,
    \AndreBhas\Brick\Brick\Bridge\Middleware\CircuitBreakerMiddleware::class,
    \App\Brick\Middleware\RateLimitMiddleware::class, // <-- add here
],
bash
php artisan vendor:publish --tag=brick-config
bash
php artisan vendor:publish --tag=brick-stubs

bricks/
└── Hotel/
    ├── Features/
    │   └── CreateHotel/                     # A cohesive, isolated feature
    │       ├── CreateHotelAction.php        # Core logic
    │       ├── CreateHotelRequest.php       # Specific validation
    │       ├── CreateHotelResponse.php      # Formatting Output
    │       └── CreateHotelFeatureTest.php   # Feature tests lives here
    ├── Models/
    ├── Http/
    └── ... (other standard directories)
bash
php artisan brick:make
bash
composer dump-autoload
bash
php artisan brick:enable Hotel
bash
php artisan brick:migrate Hotel
bash
php artisan brick:make-bridge Hotel
bash
php artisan brick:bridge:status
bash
php artisan brick:bridge:status --reset=Hotel
bash
php artisan brick:enable Booking
# Error: Cannot enable [Booking]. Missing dependencies: Hotel (inactive).
# Use --force to override.

php artisan brick:enable Hotel
php artisan brick:enable Booking  # Now works