1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Resource transaction model
30: *
31: * @todo need collect conection by name
32: * @category Mage
33: * @package Mage_Core
34: * @author Magento Core Team <core@magentocommerce.com>
35: */
36: class Mage_Core_Model_Resource_Transaction
37: {
38: /**
39: * Objects which will be involved to transaction
40: *
41: * @var array
42: */
43: protected $_objects = array();
44:
45: /**
46: * Transaction objects array with alias key
47: *
48: * @var array
49: */
50: protected $_objectsByAlias = array();
51:
52: /**
53: * Callbacks array.
54: *
55: * @var array
56: */
57: protected $_beforeCommitCallbacks = array();
58: /**
59: * Begin transaction for all involved object resources
60: *
61: * @return Mage_Core_Model_Resource_Transaction
62: */
63: protected function _startTransaction()
64: {
65: foreach ($this->_objects as $object) {
66: $object->getResource()->beginTransaction();
67: }
68: return $this;
69: }
70:
71: /**
72: * Commit transaction for all resources
73: *
74: * @return Mage_Core_Model_Resource_Transaction
75: */
76: protected function _commitTransaction()
77: {
78: foreach ($this->_objects as $object) {
79: $object->getResource()->commit();
80: }
81: return $this;
82: }
83:
84: /**
85: * Rollback transaction
86: *
87: * @return Mage_Core_Model_Resource_Transaction
88: */
89: protected function _rollbackTransaction()
90: {
91: foreach ($this->_objects as $object) {
92: $object->getResource()->rollBack();
93: }
94: return $this;
95: }
96:
97: /**
98: * Run all configured object callbacks
99: *
100: * @return Mage_Core_Model_Resource_Transaction
101: */
102: protected function _runCallbacks()
103: {
104: foreach ($this->_beforeCommitCallbacks as $callback) {
105: call_user_func($callback);
106: }
107: return $this;
108: }
109:
110: /**
111: * Adding object for using in transaction
112: *
113: * @param Mage_Core_Model_Abstract $object
114: * @param string $alias
115: * @return Mage_Core_Model_Resource_Transaction
116: */
117: public function addObject(Mage_Core_Model_Abstract $object, $alias='')
118: {
119: $this->_objects[] = $object;
120: if (!empty($alias)) {
121: $this->_objectsByAlias[$alias] = $object;
122: }
123: return $this;
124: }
125:
126: /**
127: * Add callback function which will be called before commit transactions
128: *
129: * @param callback $callback
130: * @return Mage_Core_Model_Resource_Transaction
131: */
132: public function addCommitCallback($callback)
133: {
134: $this->_beforeCommitCallbacks[] = $callback;
135: return $this;
136: }
137:
138: /**
139: * Initialize objects save transaction
140: *
141: * @return Mage_Core_Model_Resource_Transaction
142: * @throws Exception
143: */
144: public function save()
145: {
146: $this->_startTransaction();
147: $error = false;
148:
149: try {
150: foreach ($this->_objects as $object) {
151: $object->save();
152: }
153: } catch (Exception $e) {
154: $error = $e;
155: }
156:
157: if ($error === false) {
158: try {
159: $this->_runCallbacks();
160: } catch (Exception $e) {
161: $error = $e;
162: }
163: }
164:
165: if ($error) {
166: $this->_rollbackTransaction();
167: throw $error;
168: } else {
169: $this->_commitTransaction();
170: }
171:
172: return $this;
173: }
174:
175: /**
176: * Initialize objects delete transaction
177: *
178: * @return Mage_Core_Model_Resource_Transaction
179: * @throws Exception
180: */
181: public function delete()
182: {
183: $this->_startTransaction();
184: $error = false;
185:
186: try {
187: foreach ($this->_objects as $object) {
188: $object->delete();
189: }
190: } catch (Exception $e) {
191: $error = $e;
192: }
193:
194: if ($error === false) {
195: try {
196: $this->_runCallbacks();
197: } catch (Exception $e) {
198: $error = $e;
199: }
200: }
201:
202: if ($error) {
203: $this->_rollbackTransaction();
204: throw $error;
205: } else {
206: $this->_commitTransaction();
207: }
208: return $this;
209: }
210:
211: }
212: