領収証発行サービス
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

65 líneas
1.4KB

  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. }
  21. DB::beginTransaction();
  22. $this->isBeginning = true;
  23. }
  24. public function commit(): void
  25. {
  26. if (!$this->isBeginning) {
  27. throw new LogicException("無効なコミット検知");
  28. }
  29. DB::commit();
  30. $this->isBeginning = false;
  31. }
  32. public function rollBack(): void
  33. {
  34. if (!$this->isBeginning) {
  35. throw new LogicException("無効なロールバック検知");
  36. }
  37. DB::rollBack();
  38. $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  39. foreach ($trace as $line) {
  40. logger(sprintf(
  41. "Rollback from File:%s Line:%d",
  42. data_get($line, "file", ""),
  43. data_get($line, "line", 0)
  44. ));
  45. }
  46. logs()->warning("ロールバック検知");
  47. $this->isBeginning = false;
  48. }
  49. public function isBeginning(): bool
  50. {
  51. return $this->isBeginning;
  52. }
  53. }