領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5KB

  1. <?php
  2. namespace App\Util;
  3. use App\Features\InstanceAble;
  4. use Illuminate\Support\Facades\DB;
  5. use LogicException;
  6. class DBUtil
  7. {
  8. use InstanceAble;
  9. private bool $isBeginning = false;
  10. public function __destruct()
  11. {
  12. if ($this->isBeginning) {
  13. $this->rollBack();
  14. }
  15. }
  16. public function beginTransaction(): void
  17. {
  18. if ($this->isBeginning) {
  19. // throw new LogicException("2重トランザクション開始検知");
  20. return;
  21. }
  22. DB::beginTransaction();
  23. $this->isBeginning = true;
  24. }
  25. public function commit(): void
  26. {
  27. if (!$this->isBeginning) {
  28. // throw new LogicException("無効なコミット検知");
  29. return;
  30. }
  31. DB::commit();
  32. $this->isBeginning = false;
  33. }
  34. public function rollBack(): void
  35. {
  36. if (!$this->isBeginning) {
  37. // throw new LogicException("無効なロールバック検知");
  38. return;
  39. }
  40. DB::rollBack();
  41. $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  42. foreach ($trace as $line) {
  43. logger(sprintf(
  44. "Rollback from File:%s Line:%d",
  45. data_get($line, "file", ""),
  46. data_get($line, "line", 0)
  47. ));
  48. }
  49. logs()->warning("ロールバック検知");
  50. $this->isBeginning = false;
  51. }
  52. public function isBeginning(): bool
  53. {
  54. return $this->isBeginning;
  55. }
  56. }