PHP code example of kumwe / transaction

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

    

kumwe / transaction example snippets




declare(strict_types=1);

use Kumwe\Transaction\Contract\TransactionManager;
use Kumwe\Transaction\Contract\TransactionState;

final readonly class LedgerPosting
{
    public function __construct(
        private TransactionManager $transactions,
        private TransactionState $state,
        private LedgerRepository $ledger,
        private Notifier $notifier,
    ) {
    }

    public function post(Entry $entry): Receipt
    {
        if ($this->state->isActive()) {
            throw new LogicException('A posting must open its own transaction scope.');
        }

        return $this->transactions->transactional(function () use ($entry): Receipt {
            $receipt = $this->ledger->append($entry);
            $this->transactions->afterCommit(fn () => $this->notifier->posted($receipt));
            $this->transactions->afterRollback(fn () => $this->notifier->withdrawn($entry));

            return $receipt;
        });
    }
}

$container->share(TransactionManager::class, static fn (Container $c): TransactionManager =>
    new DoctrineTransactionManager($c->get(Connection::class)));
$container->share(TransactionState::class, static fn (Container $c): TransactionState =>
    new DoctrineTransactionState($c->get(Connection::class)));
bash
composer install
composer check                    # the complete lane, ending in the built-archive clean-consumer gate
php tests/run.php                 # the dependency-free suite alone; no composer install needed
php examples/typed-consumer.php   # the shipped example; no composer install needed