PHP 的嵌入式脚本语言?

发布于 2024-07-29 13:47:52 字数 386 浏览 5 评论 0原文

我想知道是否有人知道一种可以嵌入到 php 中的好脚本语言? Javascript 是最受欢迎的,尽管有几次尝试,但它们要么太不稳定/太慢/太过时(phpjs、j4p5),要么在共享主机上启动和运行非常痛苦(pecl Spidermonkey)。

背景是:我想要一种用于控制服务器上的 php 的语言,但它还应该支持一些逻辑,所以 yaml、xml 或 json 是不够的。 我研究过 LUA 解释器和 mediakwiki 的“Winter”,但它们要么依赖外部引擎,要么具有强大的绑定。

pecl Spidermonkey 绑定似乎是最完整的,您可以在其中将变量、函数和整个对象注册到 js 引擎。 有谁知道任何系统或语言可能接近我正在寻找的东西?

最好的

罗尔夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2024-08-05 13:47:53

我还没有看到很多引擎允许在 PHP 中启用另一种脚本语言:-(

即使在 PECL 上,似乎也没有很多条目(请参阅 “语言”类别,例如:只有一种,你已经知道了)

在 PEAR 上,我什至找不到任何符合您标准的东西...

我玩过 Spidermonkey 一下(参见 我的博客上的这篇文章,法语),是的,这有点有趣。 但几个月前,当我写那篇文章时,它还不太稳定——此后只进行了几次提交。 所以,我明白为什么托管公司不会在他们的服务器上提供它......

即使我希望他们:允许非 PHP 开发人员为您的应用程序开发脚本也很棒!

但有一个问题:为什么您想要/需要 PHP 之外的另一种语言?

我的意思是 PHP 已经安装在你的服务器上,你显然有使用它的经验,而且它是一种非常强大的语言......那么你为什么想要/需要使用其他东西呢?

使用 PHP 的 eval,您甚至可以考虑执行“动态”PHP 代码(是的,eval 就是 eval 等等,我知道 ^^ )

另一种选择,使用“exec”之类的东西如果您在 Linux 服务器上,则启动某种 shell 脚本; 但我肯定更喜欢用 PHP 编码而不是 shell,而且我当然不是唯一一个 ^^

不管怎样,请留意你可能得到的答案,这也可能让我很感兴趣;-)

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 ;-)

北陌 2024-08-05 13:47:53

我理解你的担忧。 即使对于可信源,PHP 也提供了比整个 Web 请求环境所需的更多访问权限。 即使脚本编写者是值得信赖的,即使他们只会因脚本错误而伤害自己,但更受约束的脚本环境将更容易让他们使用,也更容易让您支持。

您想要一些可以沙箱关闭的东西,只能访问您明确分配给其范围的资源,并且在“游戏中的游戏”运行时环境中执行,而不是在 PHP 自己的运行时环境中执行。

一种方法是对用户提交的脚本使用 Web 模板语言。 它们提供一定程度的控制(例如变量分配),并关闭其他选项,例如您不能编写无限循环。 我在 Java 应用程序中使用 Velocity 来实现此目的; 我认为像 Smarty 这样的东西可能在 PHP 中工作,但我没有直接使用它来实现此目的的经验。

如果脚本需要执行的操作受到域的限制,另一种方法是实现域特定语言 (DSL)。 我在这个答案中提到了这一点。

除此之外,我不知道有任何脚本语言的纯 PHP 实现。 这是我自己感兴趣的事情。

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.

拥抱影子 2024-08-05 13:47:53

Symfony 有 ExpressionLanguage 组件:

ExpressionLanguage组件提供了一个可以编译的引擎
并评估表达式。 表达式是一个单行表达式,返回一个
值(主要但不限于布尔值)。

...

该组件的目的是允许用户在配置中使用表达式来实现更复杂的逻辑。 对于某些示例,Symfony 框架在安全性、验证规则和路由匹配中使用表达式。

该语言的示例如下:

# 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文