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