~cyrenity/zivios/devel

« back to all changes in this revision

Viewing changes to application/library/Ecl/Transaction/Item.php

  • Committer: Faraz Khan
  • Date: 2008-09-15 13:29:33 UTC
  • Revision ID: fkhan@zivios.org-20080915132933-d27jml5l29xw4gsr
Initial release to bazaar, code in sync with 0.5.0-release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright (c) 2008 Zivios, LLC.
 
4
 *
 
5
 * This file is part of Zivios.
 
6
 *
 
7
 * Zivios is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Zivios is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Zivios.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * @package             Transaction
 
21
 * @copyright   Copyright (c) 2008 Zivios, LLC. (http://www.zivios.org)
 
22
 * @license             http://www.zivios.org/legal/license
 
23
 * @version             $Id: Item.php 943 2008-08-26 10:50:52Z fkhan $
 
24
 **/
 
25
 
 
26
class Ecl_Transaction_Item
 
27
{
 
28
        public $codearray;
 
29
        private $rollbackarray, $objarray, $description, $status;
 
30
 
 
31
        const STATUS_FAILED=1;
 
32
        const STATUS_COMPLETE=2;
 
33
        const STATUS_READY=3;
 
34
 
 
35
        public function __construct($description='')
 
36
        {
 
37
                $this->codearray = array();
 
38
                $this->objarray = array();
 
39
                $this->description=$description;
 
40
                $this->status = self::STATUS_READY;
 
41
        }
 
42
 
 
43
        public function getDescription()
 
44
        {
 
45
                return $this->description;
 
46
        }
 
47
 
 
48
        public function getObject($name)
 
49
        {
 
50
                return $this->objarray[$name];
 
51
        }
 
52
 
 
53
        public function addObject($id,$object)
 
54
        {
 
55
        $obj = $object;
 
56
                if (array_key_exists($id,$this->objarray))
 
57
                        throw new Ecl_Exception("Object key " . $id . " exists, will not override");
 
58
                else {
 
59
                        $this->objarray[$id] = 1;
 
60
                        $this->$id = $obj;
 
61
                }
 
62
        }
 
63
 
 
64
        public function getCodeArray()
 
65
        {
 
66
                return $this->codearray;
 
67
        }
 
68
 
 
69
        public function addCommitLine($line)
 
70
        {
 
71
        $bt = debug_backtrace();
 
72
        $class = $bt[2]['class'];
 
73
                $ln = $bt[1]['line'];
 
74
 
 
75
        Ecl_Log::debug($class . " (" . $ln .") :::: code ::: " . $line);
 
76
 
 
77
                $this->codearray[] = $line;
 
78
        }
 
79
 
 
80
        public function addRollbackLine($line)
 
81
        {
 
82
                $this->rollbackarray[] = $line;
 
83
        }
 
84
 
 
85
        public function commit()
 
86
        {
 
87
                /**
 
88
                 * Setup objects first, their id's being variable names
 
89
                 */
 
90
 
 
91
                foreach ($this->codearray as $code) {
 
92
                        Ecl_Log::debug('evaluating : ' . $code);
 
93
 
 
94
                        $retval = eval($code);
 
95
                        Ecl_Log::debug("eval returned " . $retval);
 
96
 
 
97
                        if ($retval === FALSE) {
 
98
                                Ecl_Log::debug("Returning FALSE as eval outpt");
 
99
                                $this->status = self::STATUS_FAILED;
 
100
                                return FALSE;
 
101
                        }
 
102
                        $this->status = self::STATUS_COMPLETE;
 
103
                }
 
104
                /**
 
105
                 * Hardcoded check to return unless there is an exception
 
106
                 */
 
107
                return 1;
 
108
        }
 
109
 
 
110
        public function markFailed()
 
111
        {
 
112
                $this->status = self::STATUS_FAILED;
 
113
        }
 
114
 
 
115
        public function getStatus()
 
116
        {
 
117
                return $this->status;
 
118
        }
 
119
 
 
120
        public function rollback()
 
121
        {
 
122
                if ($this->status == self::STATUS_COMPLETE) {
 
123
                        foreach ($this->rollbackarray as $code) {
 
124
                                Ecl_Log::debug('ROLLBACK* evaluating : ' . $code);
 
125
                                if (eval($code) === FALSE)
 
126
                                        return FALSE;
 
127
                        }
 
128
                        return 1;
 
129
                }
 
130
                else
 
131
                        Ecl_Log::info("Item did not succeed, skipping rollback");
 
132
        }
 
133
 
 
134
}