CarefulDatabaseRunner.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class CarefulDatabaseRunner implements CarefulCommand
00016     {
00017         private $command    = null;
00018         private $db         = null;
00019         
00020         private $running = false;
00021         
00022         final public function __construct(EditorCommand $command)
00023         {
00024             $this->command = $command;
00025         }
00026         
00031         public function run(Prototyped $subject, Form $form, HttpRequest $request)
00032         {
00033             Assert::isFalse($this->running, 'command already running');
00034             Assert::isTrue($subject instanceof DAOConnected);
00035             
00036             $this->db = DBPool::getByDao($subject->dao());
00037             
00038             $this->db->begin();
00039             
00040             try {
00041                 $mav = $this->command->run($subject, $form, $request);
00042                 
00043                 $this->running = true;
00044                 
00045                 return $mav;
00046             } catch (BaseException $e) {
00047                 $this->db->rollback();
00048                 
00049                 throw $e;
00050             }
00051             
00052             Assert::isUnreachable();
00053         }
00054         
00058         public function commit()
00059         {
00060             if ($this->running) {
00061                 $this->db->commit();
00062                 $this->running = false;
00063             }
00064             
00065             return $this;
00066         }
00067         
00071         public function rollback()
00072         {
00073             if ($this->running) {
00074                 try {
00075                     $this->db->rollback();
00076                 } catch (DatabaseException $e) {
00077                     // keep silence
00078                 }
00079                 
00080                 $this->running = false;
00081             }
00082             
00083             return $this;
00084         }
00085         
00086         public function __destruct()
00087         {
00088             if ($this->running) {
00089                 try {
00090                     $this->db->rollback();
00091                 } catch (BaseException $e) {
00092                     // fear of fatal error's
00093                 }
00094             }
00095         }
00096     }
00097 ?>