Embedded script languages for PHP?
Asked Answered
W

3

7

i was wondering if anybody knows of a nice scripting language that can be embedded into php? Javascript would be favorite and although there are several attempts they are either much too shaky/slow/outdated (phpjs, j4p5) or a real pain to get up and running on shared hosts (pecl spidermonkey).

The background is: I would like to have a language that is used to control php on the server but it should also support some logic, so yaml, xml or json just isn't enough. I've looked into LUA interpreters and mediakwiki's "Winter" but they all either rely on external engines or have powerful binding whatsoever.

pecl spidermonkey binding's appears to be the most complete where you can register vars, functions and whole objects to the js-engine. Does anyone know of any system or language that might come close to what I am looking for?

best

rolf

Whiles answered 28/7, 2009 at 15:27 Comment(6)
I'm not sure what you're asking. Can you clarify what you mean by "embedded" into php?Daimyo
Are the scripts you are intending to run coming from a trusted source? If they are, then use PHP to control your PHP.Whorl
op here. yes, the scripts are trusted. I think it's better to have the control away from the core in case something in the control causes a fatal error a second, embedded parser would catch that and die instead of taking the whole engine down.Whiles
Wow .. I read it 5 times, and my head hurts.Comma
We need a 'wtf' tag.....Comma
If you are seeking to embed a scripting language into PHP, I would encourage you to consider whether you aren't Doing It Wrong.Cilium
C
2

I understand your concern. Even for trusted sources, PHP provides more access than is necessary to the whole environment of the web request. Even if the scripters are trusted and even if they can only harm themselves with a scripting error, a more constrained scripting environment would be easier for them to use and easier for you to support.

You want something that can be sandboxed off, that can only access resources you explicitly assign to its scope, and that executes in a "play within a play" runtime environment rather than in PHP's own.

One approach is to use a web templating language for user-submitted scripts. These provide a certain amount of control (variable assignment for example), and close off other options, for example you can't write an infinite loop. I've used Velocity for this purpose in Java applications; I think something like Smarty might work in PHP, but I don't have direct experience of using it for that purpose.

Another approach, if what the scripts are required to do is constrained by the domain, is to implement a Domain Specific Language (DSL). I mentioned that in this answer.

Apart from that, I don't know of any pure-PHP implementations of scripting languages. It's something I'd be interested in myself.

Clouet answered 28/7, 2009 at 17:31 Comment(1)
Thanks for the answer, I've looked around for php DSL, but there seems to be no working implementation in existence. Some blogs rambling how great it would be but nothing to see or use. If I should stumble across something meta enough I will post hereWhiles
P
4

I've not seens many engines that allow another scripting language to be enabled in PHP :-(
Even on PECL, there doesn't seem to be lots of entries (see the "languages" category, for instance : only one, which you already know)
And on PEAR, I don't even find anything that would match your criteria...

I've played with Spidermonkey a bit (see this article on my blog, in french), and it's kinda fun, yes. But it was not really stable a couple of months ago when I wrote that article -- and there have only been a few commits since. So, I understand why hosting companies would not provide it on their servers...
Even if I'd like them to : could be great to allow non-PHP developpers to develop scripts for your application !

A question though : why do you want/need another language than PHP ?

What I mean is PHP is already installed on your server, you obvisously have experience with it, and it's a quite powerful language... So why do you want/need to work with something else ?

Using PHP's eval, you can even think about executing "dynamic" PHP code (Yep, eval is eval and all that, I know ^^ )

Another option, using stuff like "exec" and the like would be, if you are on a Linux server, to launch some kind of shell-script ; but I would definitly prefer coding in PHP than shell, and I'm certainly not the only one ^^


Anyway, keeping an eye on the answers you might get, which could interest me quite a bit too ;-)

Primaveria answered 28/7, 2009 at 16:43 Comment(2)
hi pascal, thanks for you elaborate answer! I have the idea from TYPO3 which uses "typoscript" to configure itself. The concept of being able to script your application in a second language works nicely there, although typoscript is poorly designed and the parser has its limitations. eval is not an option for me and if I decided to be happy with calling exec i could as well use any other language, including js-cli. Although this would be possible it would still give me no binding with php. I could run a script, have it generate some json and use that phpWhiles
I think I your point better :-) One thing, though, about typoscript (from what I've heard : never used it myself) : bad thing with that is that you are forced to learn yet another new languages :-( (But that would be solved with Javascript, we aggree on that ;-) )Primaveria
C
2

I understand your concern. Even for trusted sources, PHP provides more access than is necessary to the whole environment of the web request. Even if the scripters are trusted and even if they can only harm themselves with a scripting error, a more constrained scripting environment would be easier for them to use and easier for you to support.

You want something that can be sandboxed off, that can only access resources you explicitly assign to its scope, and that executes in a "play within a play" runtime environment rather than in PHP's own.

One approach is to use a web templating language for user-submitted scripts. These provide a certain amount of control (variable assignment for example), and close off other options, for example you can't write an infinite loop. I've used Velocity for this purpose in Java applications; I think something like Smarty might work in PHP, but I don't have direct experience of using it for that purpose.

Another approach, if what the scripts are required to do is constrained by the domain, is to implement a Domain Specific Language (DSL). I mentioned that in this answer.

Apart from that, I don't know of any pure-PHP implementations of scripting languages. It's something I'd be interested in myself.

Clouet answered 28/7, 2009 at 17:31 Comment(1)
Thanks for the answer, I've looked around for php DSL, but there seems to be no working implementation in existence. Some blogs rambling how great it would be but nothing to see or use. If I should stumble across something meta enough I will post hereWhiles
P
1

Symfony has the ExpressionLanguage component:

The ExpressionLanguage component provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not limited to, Booleans).

...

The purpose of the component is to allow users to use expressions inside configuration for more complex logic. For some examples, the Symfony Framework uses expressions in security, for validation rules and in route matching.

An example of the language is below:

# Get the special price if
user.getGroup() in ['good_customers', 'collaborator']

# Promote article to the homepage when
article.commentCount > 100 and article.category not in ["misc"]

# Send an alert when
product.stock < 15
Paramo answered 24/4, 2019 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.