Embed phalanger & inject an object in scripting context
12 posts
• Page 1 of 2 • 1, 2
Embed phalanger & inject an object in scripting context
Hi,
Do you have an example on how to embed phalanger in a c# application, inject a custom CLR object and then execute some php script ?
Best regards,
Nelson
Do you have an example on how to embed phalanger in a c# application, inject a custom CLR object and then execute some php script ?
Best regards,
Nelson
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Hi,
To use Phalanger in C# application, add reference to PhpNetCore.dll and make use of PHP.Core.ScriptContext object.
There is also an API taking advantage of C# dynamic http://www.php-compiler.net/blog/2012/n ... langer-3-0 (section Globals)
To use Phalanger in C# application, add reference to PhpNetCore.dll and make use of PHP.Core.ScriptContext object.
- Code: Select all
using PHP.Core;
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ScriptContext context = ScriptContext.CurrentContext;
// redirect PHP output to the console:
context.Output = Console.Out; // Unicode text output
context.OutputStream = Console.OpenStandardOutput(); // byte stream output
// include the Library.php script, which initializes the
// whole library (it is also possible to include more
// scripts if necessary by repeating this call with various
// relative script paths):
context.Include("Library.php", true);
// call function f():
context.Call("f");
// create an instance of type C, passes array
// ("a" => 1, "b" => 2) as an argument to the C's ctor:
var c = (PhpObject)context.NewObject("C", PhpArray.Keyed("a", 1, "b", 2));
// var_dump the object:
PhpVariable.Dump(c);
// call static method
var foo = new PhpCallback("C", "foo", context);
var ret1 = foo.Invoke(null, new object[]{/*arg1, arg2, ...*/});
// call instance method
var bar = new PhpCallback(c, "bar"); // PhpCallback of instance method
var ret2 = bar.Invoke(null, new object[]{/*arg1, arg2, ...*/});
}
}
}
There is also an API taking advantage of C# dynamic http://www.php-compiler.net/blog/2012/n ... langer-3-0 (section Globals)
- Code: Select all
dynamic globals = context.Globals;
globals.foo(arg1,arg2);// function call
dynamic c = globals.@class.C(arg1,arg2) // new C;
globals.x = 123;// $x = 123
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Embed phalanger & inject an object in scripting context
But I do not see how to execute inline php without any file (doing a php eval). Is the dynamic object the only way to define a variable ?
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Eval is described at http://www.php-compiler.net/blog/2011/php-code-c-sharp (change 2.1.0.0 to 3.0.0.0)
Global variables can be accessed thru context.GlobalVariables
Thanks,
Global variables can be accessed thru context.GlobalVariables
Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Embed phalanger & inject an object in scripting context
The following code :
Gives me an error :
- Code: Select all
_context = PHP.Core.ScriptContext.CurrentContext;
PHP.Core.Operators.SetVariable(_context, null, "window", browser.Window);
string script = "$window->Alert(\"php!\");";
PHP.Core.DynamicCode.Eval(
script,
false,/*phalanger internal stuff*/
_context,
null,/*local variables*/
null,/*reference to "$this"*/
null,/*current class context*/
"php",/*file name, used for debug and cache key*/
1, 1,/*position in the file used for debug and cache key*/
-1,/*something internal*/
null/*current namespace, used in CLR mode*/
);
Gives me an error :
- Code: Select all
<br/>
<b>Error</b>: Call to a member function Alert() on a non-object.<br/>
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Does the dynamic api works with mono 3+ ? I am getting an error...
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Mono supports all the dynamic features.
Could you please var_dump($window); ? It seams $window is NULL.
Thanks,
Could you please var_dump($window); ? It seams $window is NULL.
Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Embed phalanger & inject an object in scripting context
- Code: Select all
var script = "var_dump($window);";
Eval(script); // eval wrapping phalanger's eval
returns
- Code: Select all
<br/>
<b>Error</b>: Call to undefined function: 'var_dump'.<br/>
but
- Code: Select all
PhpVariable.Dump(output, browser.Window);
returns
- Code: Select all
DomWindow(KimonoEngine.Dom.DomWindow)
Also , ApplicationContext.Default.GetLoadedAssemblies().Count returns 0. Would it be possible that the configuration file is not loaded ? If yes, is it possible to load it manually ?
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Found it : the config file was not loaded (my fault).
Now :
- $window->anything() works
- var_dump() doesn't work
- $browser->Window returns
Now :
- $window->anything() works
- var_dump() doesn't work
- $browser->Window returns
- Code: Select all
System.InvalidCastException: Cannot cast from source type to destination type.
at PHP.Core.PhpVariable.IsEmpty (object) <0x001b1>
at PHP.Core.Binders.PhpGetMemberBinder.<FallbackInvokeMember>b__6 (object) <0x00013>
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,PHP.Core.Reflection.DTypeDesc) <0x0003f>
at System.Dynamic.UpdateDelegates.UpdateAndExecute2<object, PHP.Core.Reflection.DTypeDesc, object> (System.Runtime.CompilerServices.CallSite,object,PHP.Core.Reflection.DTypeDesc) <0x003c7>
at <php^0>.<Global>.<php^0>.<Main> (PHP.Core.ScriptContext,System.Collections.Generic.Dictionary`2<string, object>,PHP.Core.Reflection.DObject,PHP.Core.Reflection.DTypeDesc,bool) <0x005e1>
at PHP.Core.DynamicCode.EvalInternal (string,string,string,PHP.Core.EvalKinds,PHP.Core.ScriptContext,System.Collections.Generic.Dictionary`2<string, object>,PHP.Core.Reflection.DObject,PHP.Core.Reflection.DTypeDesc,PHP.Core.Reflection.SourceCodeDescriptor,bool,PHP.Core.NamingContext) <0x00360>
at PHP.Core.DynamicCode.Eval (string,bool,PHP.Core.ScriptContext,System.Collections.Generic.Dictionary`2<string, object>,PHP.Core.Reflection.DObject,PHP.Core.Reflection.DTypeDesc,string,int,int,int,PHP.Core.NamingContext) <0x000b3>
at Kimono.Host.PhpInterpreter.Eval (string) <0x0002f>
at Kimono.Host.PhpInterpreter..ctor (Kimono.Dom.IDomBrowser) <0x0004f>
at Kimono.Sandbox.Run (Kimono.Dom.IDomBrowser) <0x000af>
- nelson4070
- Posts: 7
- Joined: April 19th, 2013, 1:04 am
Re: Embed phalanger & inject an object in scripting context
Great. If builtin PHP function does not work, add following into your config file, into <phpNet><clessLibrary> section
thanks!
- Code: Select all
<add assembly="PhpNetClassLibrary, Version=3.0.0.0, Culture=neutral, PublicKeyToken=4af37afe3cde05fb" section="bcl" />
thanks!
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
12 posts
• Page 1 of 2 • 1, 2
Return to PHP/C# interoperability
Who is online
Users browsing this forum: No registered users and 1 guest