1 | <?php |
||
26 | class Transaction |
||
27 | { |
||
28 | |||
29 | public const IsolationMVCC = 'mvcc'; |
||
30 | public const IsolationSerializable = 'serializable'; |
||
31 | public const IsolationReadUncommitted = 'readUncommitted'; |
||
32 | public const CommandBegin = 'beginTransaction'; |
||
33 | public const CommandCommit = 'commitTransaction'; |
||
34 | public const CommandRollback = 'rollbackTransaction'; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var CommandProxy |
||
39 | */ |
||
40 | private $cmd; |
||
41 | |||
42 | /** |
||
43 | * Whenever transaction is currently active |
||
44 | * @var bool |
||
45 | */ |
||
46 | private static $isActive = false; |
||
47 | |||
48 | /** |
||
49 | * Whenever transactions are available in current database |
||
50 | * @var bool |
||
51 | */ |
||
52 | private static $isAvailable; |
||
53 | |||
54 | /** |
||
55 | * Begin new transaction |
||
56 | * @param AnnotatedInterface $model |
||
57 | * @param string $isolation |
||
58 | */ |
||
59 | public function __construct(AnnotatedInterface $model, $isolation = self::IsolationMVCC) |
||
60 | { |
||
61 | $this->cmd = new CommandProxy($model); |
||
62 | if (null === self::$isAvailable) |
||
63 | { |
||
64 | self::$isAvailable = $this->isAvailable(); |
||
65 | } |
||
66 | if (!self::$isAvailable) |
||
67 | { |
||
68 | return; |
||
69 | } |
||
70 | if (self::$isActive) |
||
71 | { |
||
72 | throw new TransactionException('Transaction is already running'); |
||
73 | } |
||
74 | |||
75 | $this->cmd->call(self::CommandBegin, [ |
||
76 | 'isolation' => $isolation |
||
77 | ]); |
||
78 | |||
79 | self::$isActive = true; |
||
80 | } |
||
81 | |||
82 | public function isAvailable(): bool |
||
83 | { |
||
84 | return $this->cmd->isAvailable(self::CommandBegin) && $this->cmd->isAvailable(self::CommandCommit); |
||
85 | } |
||
86 | |||
87 | public function commit(): void |
||
91 | |||
92 | public function rollback(): void |
||
93 | { |
||
96 | |||
97 | private function _finish($command): void |
||
110 | |||
111 | } |
||
112 |