-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from cycle/transact
Add ActiveRecord::transact() method
- Loading branch information
Showing
11 changed files
with
256 additions
and
90 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ActiveRecord\Exception\Transaction; | ||
|
||
use Cycle\ActiveRecord\Exception\ActiveRecordException; | ||
|
||
class TransactionException extends \RuntimeException implements ActiveRecordException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ActiveRecord\Internal; | ||
|
||
use Cycle\ActiveRecord\Exception\Transaction\TransactionException; | ||
use Cycle\ActiveRecord\Facade; | ||
use Cycle\ActiveRecord\TransactionMode; | ||
use Cycle\ORM\EntityManagerInterface; | ||
use Cycle\ORM\Transaction\Runner; | ||
|
||
final class TransactionFacade | ||
{ | ||
private static ?EntityManagerInterface $em = null; | ||
|
||
public static function getEntityManager(): ?EntityManagerInterface | ||
{ | ||
return self::$em; | ||
} | ||
|
||
/** | ||
* @template TResult | ||
* @param callable(): TResult $callback | ||
* @return TResult | ||
* | ||
* @throws TransactionException | ||
* @throws \Throwable | ||
*/ | ||
public static function transact( | ||
callable $callback, | ||
TransactionMode $mode = TransactionMode::OpenNew, | ||
): mixed { | ||
$runner = match ($mode) { | ||
TransactionMode::Ignore => Runner::outerTransaction(strict: false), | ||
TransactionMode::Current => Runner::outerTransaction(strict: true), | ||
TransactionMode::OpenNew => Runner::innerTransaction(), | ||
}; | ||
|
||
self::$em === null or throw new TransactionException( | ||
'A transaction is already in progress.', | ||
); | ||
self::$em = Facade::getEntityManager(); | ||
|
||
try { | ||
$result = $callback(); | ||
self::$em->run(true, $runner); | ||
return $result; | ||
} finally { | ||
self::$em = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ActiveRecord; | ||
|
||
enum TransactionMode | ||
{ | ||
/** | ||
* Do nothing with transactions. | ||
* | ||
* @see \Cycle\ORM\Transaction\Runner::innerTransaction() with non-strict mode. | ||
*/ | ||
case Ignore; | ||
|
||
/** | ||
* The currently opened transaction will be used. If no transaction is opened, a new one will be created. | ||
* | ||
* @see \Cycle\ORM\Transaction\Runner::innerTransaction() with strict mode. | ||
*/ | ||
case Current; | ||
|
||
/** | ||
* A new transaction will be open for each used driver connection and will close they on finish. | ||
* | ||
* @see \Cycle\ORM\Transaction\Runner::innerTransaction() | ||
*/ | ||
case OpenNew; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.