Set version to 5.20.beta1
[civicrm-core.git] / CRM / Utils / AutoClean.php
CommitLineData
50bfb460
SB
1<?php
2/*
b812aefb 3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
b812aefb 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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
be2fb01f 90 $origValue = $resolver->call($getter, []);
b812aefb
TO
91
92 $ac = new CRM_Utils_AutoClean();
93 $ac->callback = $setter;
be2fb01f 94 $ac->args = [$origValue];
b812aefb 95
be2fb01f 96 $resolver->call($setter, [$tmpValue]);
b812aefb
TO
97
98 return $ac;
99 }
100
101 public function __destruct() {
102 \Civi\Core\Resolver::singleton()->call($this->callback, $this->args);
103 }
104
b56a4a96
TO
105 /**
106 * Prohibit (de)serialization of CRM_Utils_AutoClean.
107 *
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.
110 */
111 public function __sleep() {
112 throw new \RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for serialization.");
113 }
114
115 /**
116 * Prohibit (de)serialization of CRM_Utils_AutoClean.
117 *
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.
120 */
121 public function __wakeup() {
122 throw new \RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for deserialization.");
123 }
124
b812aefb 125}