PHP code example of solidframe / modular

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

    

solidframe / modular example snippets


use SolidFrame\Modular\Module\AbstractModule;

final class OrderModule extends AbstractModule
{
    public function __construct()
    {
        parent::__construct(
            name: 'order',
            dependsOn: ['inventory', 'payment'],
        );
    }
}

use SolidFrame\Modular\Registry\InMemoryModuleRegistry;

$registry = new InMemoryModuleRegistry();
$registry->register(new OrderModule());
$registry->register(new InventoryModule());
$registry->register(new PaymentModule());

// List all modules
$modules = $registry->all();

// Get by name
$order = $registry->get('order');
$order->dependsOn(); // ['inventory', 'payment']

// Topological sort — respects dependency order
$sorted = $registry->dependencyOrder();
// [InventoryModule, PaymentModule, OrderModule]

use SolidFrame\Modular\Exception\CircularDependencyException;

// A depends on B, B depends on A → throws CircularDependencyException

use SolidFrame\Modular\Contract\ModuleContractInterface;

interface InventoryContractInterface extends ModuleContractInterface
{
    public function reserve(string $productId, int $quantity): void;
    public function checkAvailability(string $productId): int;
}

final readonly class PlaceOrderHandler implements CommandHandler
{
    public function __construct(
        private InventoryContractInterface $inventory,
    ) {}

    public function __invoke(PlaceOrder $command): void
    {
        $this->inventory->reserve($command->productId, $command->quantity);
        // ...
    }
}

use SolidFrame\Modular\Event\AbstractIntegrationEvent;

final readonly class OrderPlacedIntegration extends AbstractIntegrationEvent
{
    public function __construct(
        public string $orderId,
        public string $productId,
        public int $quantity,
    ) {
        parent::__construct(sourceModule: 'order');
    }

    public function eventName(): string
    {
        return 'order.placed';
    }
}

// In another module's listener:
final readonly class ReserveInventoryOnOrderPlaced implements EventListener
{
    public function __invoke(OrderPlacedIntegration $event): void
    {
        $event->sourceModule(); // 'order'
        $event->occurredAt();   // DateTimeImmutable
        // reserve inventory...
    }
}

use SolidFrame\Modular\AntiCorruption\TranslatorInterface;

/** @implements TranslatorInterface<ExternalOrder, DomainOrder> */
final readonly class OrderTranslator implements TranslatorInterface
{
    public function translate(object $source): object
    {
        return new DomainOrder(
            id: new OrderId($source->getId()),
            total: Money::from($source->getTotal(), $source->getCurrency()),
        );
    }
}