CRM_Utils_AutoClean
authorTim Otten <totten@civicrm.org>
Sat, 25 Jul 2015 03:43:08 +0000 (20:43 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 15 Aug 2015 10:25:57 +0000 (03:25 -0700)
CRM/Utils/AutoClean.php [new file with mode: 0644]
tests/phpunit/CRM/Utils/AutoCleanTest.php [new file with mode: 0644]

diff --git a/CRM/Utils/AutoClean.php b/CRM/Utils/AutoClean.php
new file mode 100644 (file)
index 0000000..a4eab9b
--- /dev/null
@@ -0,0 +1,98 @@
+<?php /*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.6                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Utils_AutoClean
+ *
+ * Automatically cleanup state when the object handle is released.
+ * This is useful for unordered cleanup when a function has many
+ * different exit scenarios (eg multiple returns, exceptions).
+ */
+class CRM_Utils_AutoClean {
+  protected $callback;
+  protected $args;
+
+  /**
+   * Call a cleanup function when the current context shuts down.
+   *
+   * @code
+   * function doStuff() {
+   *   $ac = CRM_Utils_AutoClean::with(function(){
+   *     MyCleanup::doIt();
+   *   });
+   *   ...
+   * }
+   * @endcode
+   *
+   * @param mixed $callback
+   * @return CRM_Utils_AutoClean
+   */
+  public static function with($callback) {
+    $ac = new CRM_Utils_AutoClean();
+    $ac->args = func_get_args();
+    $ac->callback = array_shift($ac->args);
+    return $ac;
+  }
+
+  /**
+   * Temporarily swap values using callback functions, and cleanup
+   * when the current context shuts down.
+   *
+   * @code
+   * function doStuff() {
+   *   $ac = CRM_Utils_AutoClean::swap('My::get', 'My::set', 'tmpValue');
+   *   ...
+   * }
+   * @endcode
+   *
+   * @param mixed $getter
+   *   Function to lookup current value.
+   * @param mixed $setter
+   *   Function to set new value.
+   * @param mixed $tmpValue
+   *   The value to temporarily use.
+   * @return CRM_Utils_AutoClean
+   * @see \Civi\Core\Resolver
+   */
+  public static function swap($getter, $setter, $tmpValue) {
+    $resolver = \Civi\Core\Resolver::singleton();
+
+    $origValue = $resolver->call($getter, array());
+
+    $ac = new CRM_Utils_AutoClean();
+    $ac->callback = $setter;
+    $ac->args = array($origValue);
+
+    $resolver->call($setter, array($tmpValue));
+
+    return $ac;
+  }
+
+  public function __destruct() {
+    \Civi\Core\Resolver::singleton()->call($this->callback, $this->args);
+  }
+
+}
diff --git a/tests/phpunit/CRM/Utils/AutoCleanTest.php b/tests/phpunit/CRM/Utils/AutoCleanTest.php
new file mode 100644 (file)
index 0000000..f4ffcd0
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ * Class CRM_Utils_AutoCleanTest
+ */
+class CRM_Utils_AutoCleanTest extends CiviUnitTestCase {
+
+  public $foo;
+
+  protected function setUp() {
+    $this->useTransaction();
+    parent::setUp();
+  }
+
+  public function testAutoclean() {
+    $this->foo = 'orig';
+    $this->assertEquals('orig', $this->foo);
+    $this->nestedWithArrayCb();
+    $this->assertEquals('orig', $this->foo);
+    $this->nestedWithFuncCb();
+    $this->assertEquals('orig', $this->foo);
+    $this->nestedSwap();
+    $this->assertEquals('orig', $this->foo);
+  }
+
+  public function nestedWithArrayCb() {
+    $this->foo = 'arraycb';
+    $ac = CRM_Utils_AutoClean::with(array($this, 'setFoo'), 'orig');
+    $this->assertEquals('arraycb', $this->foo);
+  }
+
+  public function nestedWithFuncCb() {
+    $this->foo = 'funccb';
+
+    $self = $this; /* php 5.3 */
+    $ac = CRM_Utils_AutoClean::with(function () use ($self /* php 5.3 */) {
+      $self->foo = 'orig';
+    });
+
+    $this->assertEquals('funccb', $this->foo);
+  }
+
+  public function nestedSwap() {
+    $ac = CRM_Utils_AutoClean::swap(array($this, 'getFoo'), array($this, 'setFoo'), 'tmp');
+    $this->assertEquals('tmp', $this->foo);
+  }
+
+  public function getFoo() {
+    return $this->foo;
+  }
+
+  public function setFoo($value) {
+    $this->foo = $value;
+  }
+
+}