00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class CarefulDatabaseRunner implements CarefulCommand
00017 {
00018 private $command = null;
00019 private $db = null;
00020
00021 private $running = false;
00022
00023 final public function __construct(EditorCommand $command)
00024 {
00025 $this->command = $command;
00026 }
00027
00032 public function run(Prototyped $subject, Form $form, HttpRequest $request)
00033 {
00034 Assert::isFalse($this->running, 'command already running');
00035 Assert::isTrue($subject instanceof DAOConnected);
00036
00037 $this->db = DBPool::getByDao($subject->dao());
00038
00039 $this->db->begin();
00040
00041 try {
00042 $mav = $this->command->run($subject, $form, $request);
00043
00044 $this->running = true;
00045
00046 return $mav;
00047 } catch (BaseException $e) {
00048 $this->db->rollback();
00049
00050 throw $e;
00051 }
00052
00053 Assert::isUnreachable();
00054 }
00055
00059 public function commit()
00060 {
00061 if ($this->running) {
00062 $this->db->commit();
00063 $this->running = false;
00064 }
00065
00066 return $this;
00067 }
00068
00072 public function rollback()
00073 {
00074 if ($this->running) {
00075 try {
00076 $this->db->rollback();
00077 } catch (DatabaseException $e) {
00078
00079 }
00080
00081 $this->running = false;
00082 }
00083
00084 return $this;
00085 }
00086
00087 public function __destruct()
00088 {
00089 if ($this->running) {
00090 try {
00091 $this->db->rollback();
00092 } catch (BaseException $e) {
00093
00094 }
00095 }
00096 }
00097 }
00098 ?>