commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Utils / AutoClean.php
1 <?php /*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.6 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 /**
28 * Class CRM_Utils_AutoClean
29 *
30 * Automatically cleanup state when the object handle is released.
31 * This is useful for unordered cleanup when a function has many
32 * different exit scenarios (eg multiple returns, exceptions).
33 */
34 class CRM_Utils_AutoClean {
35 protected $callback;
36 protected $args;
37
38 /**
39 * Call a cleanup function when the current context shuts down.
40 *
41 * @code
42 * function doStuff() {
43 * $ac = CRM_Utils_AutoClean::with(function(){
44 * MyCleanup::doIt();
45 * });
46 * ...
47 * }
48 * @endcode
49 *
50 * @param mixed $callback
51 * @return CRM_Utils_AutoClean
52 */
53 public static function with($callback) {
54 $ac = new CRM_Utils_AutoClean();
55 $ac->args = func_get_args();
56 $ac->callback = array_shift($ac->args);
57 return $ac;
58 }
59
60 /**
61 * Temporarily swap values using callback functions, and cleanup
62 * when the current context shuts down.
63 *
64 * @code
65 * function doStuff() {
66 * $ac = CRM_Utils_AutoClean::swap('My::get', 'My::set', 'tmpValue');
67 * ...
68 * }
69 * @endcode
70 *
71 * @param mixed $getter
72 * Function to lookup current value.
73 * @param mixed $setter
74 * Function to set new value.
75 * @param mixed $tmpValue
76 * The value to temporarily use.
77 * @return CRM_Utils_AutoClean
78 * @see \Civi\Core\Resolver
79 */
80 public static function swap($getter, $setter, $tmpValue) {
81 $resolver = \Civi\Core\Resolver::singleton();
82
83 $origValue = $resolver->call($getter, array());
84
85 $ac = new CRM_Utils_AutoClean();
86 $ac->callback = $setter;
87 $ac->args = array($origValue);
88
89 $resolver->call($setter, array($tmpValue));
90
91 return $ac;
92 }
93
94 public function __destruct() {
95 \Civi\Core\Resolver::singleton()->call($this->callback, $this->args);
96 }
97
98 }