3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Class CRM_Utils_AutoClean
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).
41 class CRM_Utils_AutoClean
{
46 * Call a cleanup function when the current context shuts down.
49 * function doStuff() {
50 * $ac = CRM_Utils_AutoClean::with(function(){
57 * @param mixed $callback
58 * @return CRM_Utils_AutoClean
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
);
68 * Temporarily swap values using callback functions, and cleanup
69 * when the current context shuts down.
72 * function doStuff() {
73 * $ac = CRM_Utils_AutoClean::swap('My::get', 'My::set', 'tmpValue');
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
87 public static function swap($getter, $setter, $tmpValue) {
88 $resolver = \Civi\Core\Resolver
::singleton();
90 $origValue = $resolver->call($getter, []);
92 $ac = new CRM_Utils_AutoClean();
93 $ac->callback
= $setter;
94 $ac->args
= [$origValue];
96 $resolver->call($setter, [$tmpValue]);
101 public function __destruct() {
102 \Civi\Core\Resolver
::singleton()->call($this->callback
, $this->args
);
106 * Prohibit (de)serialization of CRM_Utils_AutoClean.
108 * The generic nature of AutoClean makes it a potential target for escalating
109 * serialization vulnerabilities, and there's no good reason for serializing it.
111 public function __sleep() {
112 throw new \
RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for serialization.");
116 * Prohibit (de)serialization of CRM_Utils_AutoClean.
118 * The generic nature of AutoClean makes it a potential target for escalating
119 * serialization vulnerabilities, and there's no good reason for deserializing it.
121 public function __wakeup() {
122 throw new \
RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for deserialization.");