Plugin Architecture with .NET and PHP

Discussion about the open-source Phalanger [?] project.

Plugin Architecture with .NET and PHP

Postby Remo » August 21st, 2012, 9:25 am

I'm getting into Phalanger and it's working more or less fine so far. I really like that I can take the best out of two worlds and judging from my small experience, I can easily build an application which runs on both platforms without changing any code.

However, I did have an idea which I'm trying to build (as a prototype) without much success. I wasn't sure how to explain it, which is why I've created a graphic that hopefully helps to understand my idea:

Image
http://www.foonom.com/plugin_architecture.png

I basically have to different runtimes, one for Phalanger and for PHP. Both should locate all available modules/plugins. In my case: module1 and module2. Every module should have a base class, in the graphic called module_super_class.

The reason why I'd like to have a different runtime for Phalanger is, because I'd like to dynamically load .NET assemblies instead of PHP code.

Questions:
1: If I want to compile PHP code to an assembly which I later load using some Reflection/Invoke magic, should I build this in pure mode?
2: Should I build the super class in pure mode as well? I tried that but had some issues because I've added things like [export] to the code. The plain PHP runtime didn't like that of course.
3: Can I load assemblies built with PHP? Again, should I use pure mode for this?

I hope it's clear what I'm trying to achieve!
I'd be glad to get any kind of feedback, thanks a lot!
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm

Re: Plugin Architecture with .NET and PHP

Postby Remo » August 21st, 2012, 9:44 am

I just tried to avoid the pure mode so I can write code for PHP and get a .NET assembly. Since I'd like to load Assemblies and not PHP code, I tried to use "DuckTyping" which is described here:
http://tomasp.net/blog/ducktyping-in-phalaner.aspx

But I don't seem to be able to use DuckType as it's not available. I tried to include almost everything that's lying around but no success. Maybe ducktyping would work for me...?

UPDATE: I realized that I have to download the binaries and not the setup at this location: http://phalanger.codeplex.com/downloads/get/463755 within the binary there's an assembly which has to be added to the .NET project (PhpNetCore.Utils.dll)
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm

Re: Plugin Architecture with .NET and PHP

Postby Remo » August 21st, 2012, 1:56 pm

I managed to get DuckTyping running like this:
Code: Select all
var context = PHP.Core.ScriptContext.CurrentContext;
context.Output = Console.Out;

context.Include(@"module_super_class.php", false);
context.Include(@"module1.php", false);

IModule1 m = context.NewObject<IModule1>(@"Module1");
var test = m.init();


I tried to replace context.Include with this:

Code: Select all
Assembly asm = Assembly.LoadFrom(@"module1.dll");
context.ApplicationContext.AssemblyLoader.LoadScriptLibrary(asm, "");


I can see in the debugger that this loads the assembly, but I get an exception when calling NewObject..
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm

Re: Plugin Architecture with .NET and PHP

Postby Jakub Misek » August 21st, 2012, 4:04 pm

Hi,

Do not use pure mode (not maintained), do not use DuckTyping (outdated).

Compile everything in standard mode, access global variables/functions/classes using C# 4.0 keyword dynamic. See http://www.php-compiler.net/blog/2012/n ... langer-3-0 for more details. If you have any other questions, feel free to ask.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Plugin Architecture with .NET and PHP

Postby Remo » August 22nd, 2012, 9:57 am

This a nice, works well! But there's one thing I don't really like. From what I understand, it would have been different with a "pure" assembly.

My PHP code "controller.php" looks like this:

Code: Select all
<?php
namespace core;

class Controller {
    public function getVersion() {
        return '0.02';
     }
}


I was able to run getVersion from my C# using the following code:
Code: Select all
var context = PHP.Core.ScriptContext.CurrentContext;
context.Output = Console.Out;

Assembly asm = Assembly.LoadFrom(@"<my-path>core.dll");
context.ApplicationContext.AssemblyLoader.LoadScriptLibrary(asm, "");

context.Include("controller.php", true);

dynamic global = context.Globals;
dynamic controller = global.@namespace.core.@class.Controller(); // core\Controller

string version = controller.getVersion();


Loading the assembly seems to work fine but it looks like I must call "context.Include" to get access to my class. Am I correct about this?
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm

Re: Plugin Architecture with .NET and PHP

Postby Jakub Misek » August 22nd, 2012, 1:34 pm

context.ApplicationContext.AssemblyLoader.LoadScriptLibrary
is usually declared in web.config (<phpNet><scriptLibrary><add ... />)

Yes, in standard mode you have to call context.include, since it simulates PHP app lifecycle.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 17 guests

cron

User Control Panel

Login

Who is online

In total there are 17 users online :: 0 registered, 0 hidden and 17 guests (based on users active over the past 5 minutes)
Most users ever online was 511 on April 27th, 2024, 10:11 pm

Users browsing this forum: No registered users and 17 guests