wiki.techinc.nl/includes/deferred/AtomicSectionUpdate.php
Aaron Schulz a2790b1b80 Remove wfGetCaller() dependency from DatabaseBase
Change-Id: I3e240b2eb5c1f6a21f1bc974c3d28f5755c7451a
2016-09-15 14:40:00 -07:00

46 lines
1.1 KiB
PHP

<?php
/**
* Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
* @since 1.27
*/
class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
/** @var IDatabase */
private $dbw;
/** @var string */
private $fname;
/** @var callable|null */
private $callback;
/**
* @param IDatabase $dbw
* @param string $fname Caller name (usually __METHOD__)
* @param callable $callback
* @see IDatabase::doAtomicSection()
*/
public function __construct( IDatabase $dbw, $fname, callable $callback ) {
$this->dbw = $dbw;
$this->fname = $fname;
$this->callback = $callback;
if ( $this->dbw->trxLevel() ) {
$this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
}
}
public function doUpdate() {
if ( $this->callback ) {
$this->dbw->doAtomicSection( $this->fname, $this->callback );
}
}
public function cancelOnRollback( $trigger ) {
if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
$this->callback = null;
}
}
public function getOrigin() {
return $this->fname;
}
}