PHP code example of brandembassy / unit-of-work

1. Go to this page and download the library: Download brandembassy/unit-of-work 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/ */

    

brandembassy / unit-of-work example snippets


function doBoringStuff(Document $document): UnitOfWork {
	$document->changeBoringStuff();
	$unitOfWork = new UnitOfWork();
	$unitOfWork->registerOperation(new SaveDocumentOperation($document));
	
	return $unitOfWork;
}

function doFunkyStuff(Document $document): UnitOfWork {
	$document->changeFunkyStuff();
	$unitOfWork = new UnitOfWork();
	$unitOfWork->registerOperation(new SaveDocumentOperation($document));
	
	return $unitOfWork; 
}


$boringWork = doBoringStuff($document);
$funkyWork = doFunkyStuff($document)
$unitOfWork = $boringWork->concatenate($funkyWork);

// Every Processor has its own Accessor for Dependecy Injection Container
$accessors = [new SaveDocumentOperationProcessorAccessor()]; 

$naiveExecutor = new NaiveUnitOfWorkExecutor($accesors);
$executor = new ReducingUnitOfWorkExecutor($naiveExecutor); // Decorator pattern to separate merging logic

// And document will be saved only once if your SaveDocumentOperation is implemented
// as mergable. See: `MergeableOperation` as example.
$executor->execute($unitOfWork);