PHP code example of aeatech / transaction-manager-bundle

1. Go to this page and download the library: Download aeatech/transaction-manager-bundle 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/ */

    

aeatech / transaction-manager-bundle example snippets


use AEATech\TransactionManager\DatabaseErrorHeuristicsInterface;
use AEATech\TransactionManagerBundle\Attribute\AsHeuristics;

#[AsHeuristics(managerNames: 'main')]
class MyCustomHeuristics implements DatabaseErrorHeuristicsInterface
{
    public function isConnectionIssue(?string $sqlState, ?int $driverCode, string $message): bool
    {
        // your logic
    }

    public function isTransientIssue(?string $sqlState, ?int $driverCode, string $message): bool
    {
        // your logic
    }
}

use AEATech\TransactionManager\TransactionManagerInterface;
use AEATech\TransactionManager\MySQL\MySQLTransactionsFactoryInterface;

class RegisterUserService
{
    public function __construct(
        private TransactionManagerInterface $tm,
        private MySQLTransactionsFactoryInterface $factory
    ) {}

    public function register(string $email): void
    {
        // Create a transaction (this doesn't execute anything yet)
        $tx = $this->factory->createInsert('users', ['email' => $email]);
        
        // Run the transaction (

public function __construct(TransactionManagerInterface $tm) 
{
    // Injects the manager configured in 'default_manager'
}

use Symfony\Component\DependencyInjection\Attribute\Autowire;

public function __construct(
    #[Autowire(service: 'aea_tech_transaction_manager.analytics')]
    TransactionManagerInterface $analyticsTm
) {}

use AEATech\TransactionManager\MySQL\MySQLTransactionsFactoryInterface;

public function __construct(MySQLTransactionsFactoryInterface $factory) { /* ... */ }

use AEATech\TransactionManagerBundle\TransactionManagerRegistryInterface;

class MaintenanceCommand
{
    public function __construct(private TransactionManagerRegistryInterface $registry) {}

    public function execute(): void
    {
        // Get a specific manager by name
        $tm = $this->registry->getManager('analytics');
        
        // Get all registered manager names
        $names = $this->registry->getManagerNames();
        
        // Get the default manager name
        $default = $this->registry->getDefaultManagerName();
    }
}

public function testRegister(): void
{
    $tm = \Mockery::mock(TransactionManagerInterface::class);
    $factory = \Mockery::mock(MySQLTransactionsFactoryInterface::class);
    
    $tx = \Mockery::mock(\AEATech\TransactionManager\TransactionInterface::class);
    $factory->shouldReceive('createInsert')->andReturn($tx);
    
    // Expect the transaction to be run
    $tm->shouldReceive('run')->with($tx)->once();

    $service = new RegisterUserService($tm, $factory);
    $service->register('[email protected]');
}
bash
# Run tests for all supported PHP versions
for v in 8.2 8.3 8.4; do \
    echo "Testing PHP $v..."; \
    docker-compose -p aeatech-transaction-manager-bundle -f docker/docker-compose.yml exec php-cli-$v vendor/bin/phpunit || break; \
done