3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Utils_AutoClean
21 * Automatically cleanup state when the object handle is released.
22 * This is useful for unordered cleanup when a function has many
23 * different exit scenarios (eg multiple returns, exceptions).
25 class CRM_Utils_AutoClean
{
30 * Call a cleanup function when the current context shuts down.
33 * function doStuff() {
34 * $ac = CRM_Utils_AutoClean::with(function(){
41 * @param mixed $callback
42 * @return CRM_Utils_AutoClean
44 public static function with($callback) {
45 $ac = new CRM_Utils_AutoClean();
46 $ac->args
= func_get_args();
47 $ac->callback
= array_shift($ac->args
);
52 * Temporarily set the active locale. Cleanup locale when the autoclean handle disappears.
54 * @param string|null $newLocale
56 * @return \CRM_Utils_AutoClean|null
58 public static function swapLocale(?
string $newLocale) {
59 $oldLocale = $GLOBALS['tsLocale'] ??
NULL;
60 if ($oldLocale === $newLocale) {
64 $i18n = \CRM_Core_I18n
::singleton();
65 $i18n->setLocale($newLocale);
66 return static::with(function() use ($i18n, $oldLocale) {
67 $i18n->setLocale($oldLocale);
72 * Temporarily swap values using callback functions, and cleanup
73 * when the current context shuts down.
76 * function doStuff() {
77 * $ac = CRM_Utils_AutoClean::swap('My::get', 'My::set', 'tmpValue');
82 * @param mixed $getter
83 * Function to lookup current value.
84 * @param mixed $setter
85 * Function to set new value.
86 * @param mixed $tmpValue
87 * The value to temporarily use.
88 * @return CRM_Utils_AutoClean
89 * @see \Civi\Core\Resolver
91 public static function swap($getter, $setter, $tmpValue) {
92 $resolver = \Civi\Core\Resolver
::singleton();
94 $origValue = $resolver->call($getter, []);
96 $ac = new CRM_Utils_AutoClean();
97 $ac->callback
= $setter;
98 $ac->args
= [$origValue];
100 $resolver->call($setter, [$tmpValue]);
105 public function __destruct() {
106 \Civi\Core\Resolver
::singleton()->call($this->callback
, $this->args
);
110 * Prohibit (de)serialization of CRM_Utils_AutoClean.
112 * The generic nature of AutoClean makes it a potential target for escalating
113 * serialization vulnerabilities, and there's no good reason for serializing it.
115 public function __sleep() {
116 throw new \
RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for serialization.");
120 * Prohibit (de)serialization of CRM_Utils_AutoClean.
122 * The generic nature of AutoClean makes it a potential target for escalating
123 * serialization vulnerabilities, and there's no good reason for deserializing it.
125 public function __wakeup() {
126 throw new \
RuntimeException("CRM_Utils_AutoClean is a runtime helper. It is not intended for deserialization.");