Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Bridge Laravel Package

dlakomski/symfony-bridge

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Package

    composer require dlakomski/symfony-bridge
    

    (Note: The package is a fork of simplebus/symfony-bridge; verify compatibility with your Symfony version.)

  2. Enable Bundles Add to config/bundles.php:

    return [
        // ...
        SimpleBus\SymfonyBridge\CommandBusBundle\CommandBusBundle::class => ['all' => true],
        SimpleBus\SymfonyBridge\EventBusBundle\EventBusBundle::class => ['all' => true],
        // Optional: Doctrine integration
        SimpleBus\SymfonyBridge\DoctrineORMBridgeBundle\DoctrineORMBridgeBundle::class => ['all' => true],
    ];
    
  3. Configure the Bus Define a command/event bus in config/packages/simple_bus.yaml:

    simple_bus:
        command_bus:
            middleware: ['@simple_bus.middleware.doctrine_transaction', '@simple_bus.middleware.logger']
        event_bus:
            middleware: ['@simple_bus.middleware.doctrine_transaction']
    
  4. First Use Case: Dispatch a Command

    use SimpleBus\MessageBus\MessageBus;
    use App\Command\CreateUserCommand;
    
    class UserService {
        public function __construct(private MessageBus $commandBus) {}
    
        public function createUser(string $name) {
            $this->commandBus->dispatch(new CreateUserCommand($name));
        }
    }
    

Implementation Patterns

Core Workflows

  1. Command Bus

    • Dispatching: Use MessageBus interface to send commands (e.g., CreateUserCommand).
    • Handling: Implement SimpleBus\MessageBus\Message\MessageHandlerInterface for commands.
      class CreateUserHandler implements MessageHandlerInterface {
          public function handle(CreateUserCommand $command) {
              // Business logic
          }
      }
      
    • Middleware: Chain middleware (e.g., logging, validation) in config/packages/simple_bus.yaml.
  2. Event Bus

    • Publishing: Dispatch events via EventBus (e.g., UserCreatedEvent).
    • Subscribing: Register listeners via Symfony’s EventSubscriberInterface or annotations.
      #[AsEventListener]
      class UserCreatedListener {
          public function __invoke(UserCreatedEvent $event) {
              // React to event
          }
      }
      
  3. Doctrine Integration

    • Auto-flush entities after commands/events using DoctrineTransactionMiddleware.
    • Configure in simple_bus.yaml:
      simple_bus:
          command_bus:
              middleware: ['@simple_bus.middleware.doctrine_transaction']
      

Integration Tips

  • Dependency Injection: Autowire MessageBus (for commands) or EventBus (for events) into services.
  • Testing: Use SimpleBus\MessageBus\Test\MessageBusTestCase for isolated command/event tests.
  • Async Processing: Combine with Symfony Messenger for background jobs (e.g., AsyncCommandBus).

Gotchas and Tips

Pitfalls

  1. Middleware Order Matters

    • Middleware runs in the order defined in simple_bus.yaml. Place DoctrineTransactionMiddleware last to ensure transactions wrap the entire handler chain.
  2. Circular Dependencies

    • Avoid injecting MessageBus into event handlers or command handlers (risk of infinite loops). Use services instead.
  3. Doctrine Entity Lifecycle

    • If using DoctrineTransactionMiddleware, ensure entities are managed before dispatching commands (e.g., entityManager->persist()).
  4. Symfony 6+ Compatibility

    • The fork may lag behind Symfony updates. Check composer.json for supported versions.

Debugging

  • Log Middleware: Add @simple_bus.middleware.logger to debug message flow:
    simple_bus:
        command_bus:
            middleware: ['@simple_bus.middleware.logger']
    
  • Handler Not Found: Verify handlers are tagged as services or autowired (Symfony’s autoconfigure: true helps).

Extension Points

  1. Custom Middleware Create middleware classes implementing SimpleBus\MessageBus\Middleware\Middleware:

    class ValidateCommandMiddleware implements Middleware {
        public function handle($message, callable $next) {
            if (!$this->isValid($message)) {
                throw new \RuntimeException("Invalid command");
            }
            return $next($message);
        }
    }
    

    Register in simple_bus.yaml:

    simple_bus:
        command_bus:
            middleware: ['@validate_command']
    
  2. Event Listener Priorities Use Symfony’s priority option in annotations or YAML to control listener order:

    #[AsEventListener(priority: 10)]
    
  3. Async Command Bus Extend SimpleBus\SymfonyBridge\CommandBusBundle\CommandBus to delegate to Symfony Messenger:

    class AsyncCommandBus extends CommandBus {
        public function dispatch($command) {
            $this->messenger->dispatch($command);
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware