/// <summary> /// Walks the object graph rooted in this node. /// </summary> /// <param name="callback">The callback method.</param> /// <param name="context">Current <see cref="ScriptContext"/>.</param> public void Walk(PhpWalkCallback callback, ScriptContext context) { IPhpObjectGraphNode node = value as IPhpObjectGraphNode; if (node != null) { value = callback(node, context); node = value as IPhpObjectGraphNode; if (node != null) { node.Walk(callback, context); } } }
/// <summary> /// Converts instances of user-defined PHP classes to <see cref="stdClass"/>es. Also converts all /// strings to the <see cref="System.String"/> representation. /// </summary> /// <param name="node"></param> /// <param name="context">Current <see cref="ScriptContext"/>.</param> /// <returns></returns> internal static object PreCallObjectTransform(IPhpObjectGraphNode node, ScriptContext context) { DObject obj = node as DObject; if (obj != null && !obj.GetType().FullName.StartsWith(Namespaces.Library)) { // simulate "marshal-by-ref" semantics for user-defined classes stdClass std = new stdClass(context); string id = Guid.NewGuid().ToString(); std.SetProperty(ObjectIdentityFieldName, id, null); (objectIdentities ?? (objectIdentities = new Dictionary<string, DObject>()))[id] = obj; return std; } if (node is PhpString) return ((IPhpConvertible)node).ToString(); return node; }
/// <summary> /// Converts <see cref="stdClass"/>es with identity field present into their corresponding user-defined /// class instances. /// </summary> /// <param name="node"></param> /// <param name="context">Current <see cref="ScriptContext"/>.</param> /// <returns></returns> internal static object PostCallObjectTransform(IPhpObjectGraphNode node, ScriptContext context) { stdClass std = node as stdClass; if (std != null && objectIdentities != null) { string id = PhpVariable.AsString(std.GetProperty(ObjectIdentityFieldName, null)); if (id != null) { DObject obj; if (objectIdentities.TryGetValue(id, out obj)) return obj; } } return node; }
/// <summary> /// Walks the object graph using specified callback function. /// </summary> /// <param name="context"></param> /// <param name="callback"></param> /// <param name="node">Object graph, can be null.</param> public static void TransformParameterGraph(ScriptContext context, PhpWalkCallback callback, IPhpObjectGraphNode node) { if (node != null) node.Walk(callback, context); }