Skip to content

Commit

Permalink
Merge pull request #200
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Nov 19, 2024
2 parents 245b055 + 28180ae commit f8876f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Core/CoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ class_alias(get_class($anon_class), $exception_class);
}

if ($act['act'] == "kill") {
if(isset($opt['exception_object']) and !empty($opt['exception_object']))
throw new $opt['exception_object'];

error_reporting(0);
die;
}
Expand Down
50 changes: 50 additions & 0 deletions src/Orm/Traits/TransactionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace BrickLayer\Lay\Orm\Traits;


use BrickLayer\Lay\Core\LayException;
use BrickLayer\Lay\Orm\Enums\OrmExecStatus;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\ExpectedValues;

trait TransactionHandler
Expand Down Expand Up @@ -116,4 +118,52 @@ final public function commit_or_rollback(#[ExpectedValues([

return false;
}

/**
* This function wraps all your operations in a callback function, inside a transaction, and also wrapped in a try catch block
* @param callable $scoped_operations
* @param int $flags [optional] A bitmask of MYSQLI_TRANS_COR_* constants.
* @param string|null $name [optional] If provided then ROLLBACK $name is executed.
* @return array
*/
#[ArrayShape([
'status' => 'bool',
'message' => 'string',
'exception' => '?Throwable',
])]
final public static function scoped_transaction(
callable $scoped_operations,
bool $throw_exception = true,
#[ExpectedValues([
MYSQLI_TRANS_START_READ_ONLY,
MYSQLI_TRANS_START_READ_WRITE,
MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT,
])] int $flags = 0,
?string $name = null
) : array
{
try{
self::new()->begin_transaction($flags, $name);
$scoped_operations();
self::new()->commit_or_rollback($flags, $name);

return [
"status" => true,
"message" => "Operation successful"
];
} catch (\Throwable $exception) {
self::new()->rollback();

if($throw_exception)
LayException::throw_exception("", "ScopedTransactionException", exception: $exception);

LayException::log("", exception: $exception, log_title: "ScopedTransactionLog");

return [
"status" => false,
"message" => "Operation failed",
"exception" => $exception,
];
}
}
}

0 comments on commit f8876f4

Please sign in to comment.