static void Main() { // sideload assembly containing compiled PHP code: Context.AddScriptReference(Assembly.Load(new AssemblyName("phplib"))); // create host for PHP code (Runtime Context): using (var ctx = Context.CreateConsole()) { // declare global function in PHP runtime context ctx.DeclareFunction(RoutineInfo.CreateUserRoutine("is_valid_url", new Func <string, bool>((url) => { return(new Uri(url).IsAbsoluteUri); }))); // call the global code from "class-user.php" which declares class ctx.Include("", "inc/class-user.php"); // dynamic u1 = ctx.Create("User", "Jacob", "https://example.org", "*****@*****.**"); u1.Authenticate(); var u2 = new DotNetUser(ctx, "Jacob", "https://example.org", "*****@*****.**"); u2.Authenticate(); } }
public static string create_function(Context ctx, string args, string code) { // prepare function script var data = ScriptingContext.EnsureContext(ctx); var source = string.Format(_createFunctionTemplate, _lambdaFuncName, args, code); // create script that declares the lambda function var script = ctx.ScriptingProvider.CreateScript(new Context.ScriptOptions() { Context = ctx, EmitDebugInformation = Debugger.IsAttached, // CONSIDER Location = new Location(Path.Combine(ctx.RootPath, "runtime-created function"), 0, 0), // TODO: pass from calling script IsSubmission = true }, source); var methods = script.GetGlobalRoutineHandle(_lambdaFuncName).ToArray(); if (methods.Length == 0) { return(null); } var lambdaName = string.Format(_lambdaFormatString, ++data.LastLambdaIndex); var routine = RoutineInfo.CreateUserRoutine(lambdaName, methods); ctx.DeclareFunction(routine); return(lambdaName); }
public void Configure(WpApp app) { this.app = app; Del MyDel = RegisterMyWidget; var routineInfo = RoutineInfo.CreateUserRoutine("RegisterMyWidget", MyDel); app.Context.Call("add_action", (PhpValue)"widgets_init", routineInfo); }
public override IPhpCallable AsCallable(ref PhpValue me, RuntimeTypeHandle callerCtx) { var obj = me.Object; if (obj is IPhpCallable) { return((IPhpCallable)obj); // classes with __invoke() magic method implements IPhpCallable } if (obj is Delegate d) { return(RoutineInfo.CreateUserRoutine(d.GetMethodInfo().Name, d)); } return(PhpCallback.CreateInvalid()); }
static void Main() { // sideload assembly containing compiled PHP code: Context.AddScriptReference(Assembly.Load(new AssemblyName("phplib"))); // create host for PHP code (Runtime Context): using (var ctx = Context.CreateConsole()) { // declare a global variable in PHP runtime context ctx.Globals["x"] = (PhpValue)"Hello from C#"; // declare global function in PHP runtime context ctx.DeclareFunction(RoutineInfo.CreateUserRoutine("foo", new Func <string>(() => { return("Hello from lambda!"); }))); // call the global code from "main.php" ctx.Include("", "main.php"); // call PHP function defined in 'main.php' ctx.Call("demo", new object[] { }); } }
/// <summary> /// Declare user function into the PHP runtime context. /// </summary> /// <param name="name">Global PHP function name.</param> /// <param name="delegate">Delegate to represent the PHP function.</param> public void DeclareFunction(string name, Delegate @delegate) => _functions.DeclarePhpRoutine(RoutineInfo.CreateUserRoutine(name, @delegate));