You are here

function features_remove_recursion in Features 7.2

Returns a deep copy of the object or array without recursion.

Properties or array values with recursive references are replaced with dummy string values.

The algorithm operates on a serialized version of the data. It was introduced in features in #2543306, mainly for performance reasons. It is taken from https://code.google.com/p/formaldehyde/source/browse/trunk/formaldehyde.php. The algorithm is also used in the node_export module.

@todo Needs unit tests.

Parameters

object|array|mixed $o: Original object or array, or an arbitrary value.

Return value

object|array|mixed A copy of the object or array with recursion removed. If the original value was not an object or array, it is returned unaltered.

1 call to features_remove_recursion()
features_sanitize in ./features.export.inc
Helper function to "sanitize" an array or object.

File

./features.export.inc, line 1587
Contains functions that export configuration into feature modules.

Code

function features_remove_recursion($o) {
  if (is_array($o) || is_object($o)) {
    $re = '#(r|R):([0-9]+);#';
    $serialize = serialize($o);
    if (preg_match($re, $serialize)) {
      $last = $pos = 0;
      while (FALSE !== ($pos = strpos($serialize, 's:', $pos))) {
        $chunk = substr($serialize, $last, $pos - $last);
        if (preg_match($re, $chunk)) {
          $length = strlen($chunk);
          $chunk = preg_replace_callback($re, '_features_remove_recursion', $chunk);
          $serialize = substr($serialize, 0, $last) . $chunk . substr($serialize, $last + ($pos - $last));
          $pos += strlen($chunk) - $length;
        }
        $pos += 2;
        $last = strpos($serialize, ':', $pos);
        $length = substr($serialize, $pos, $last - $pos);
        $last += 4 + $length;
        $pos = $last;
      }
      $serialize = substr($serialize, 0, $last) . preg_replace_callback($re, '_features_remove_recursion', substr($serialize, $last));
      $o = unserialize($serialize);
    }
  }
  return $o;
}