定制 aftermarketpl/php2js 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aftermarketpl/php2js

Composer 安装命令:

composer require aftermarketpl/php2js

包简介

Quick and dirty PHP to JavaScript converter.

关键字:

README 文档

README

This project aims to create a fairly complete PHP to JavaScript transpiler.

Its primary purpose is to aid internal development of AfterMarket.pl, but since it may also be of an interest to the general public, it is released under Apache license and available as a composer package.

Project aims

The most important design goals of the converter are as follows:

  • It is intended for development of new code, rather than converting existing code. It will not magically convert all your existing legacy PHP code to JavaScript, because it would require effort that is well beyond the scope of the project. But if you write your PHP code bearing in mind the transpiler's limitations, it should be robust enough for most of your needs.
  • The resulting JavaScript code is meant to be run in a browser, not in a server environment. This means that it does not use any fancy Node.js modules or features, which also further limits the range of accepted PHP constructs and features.
  • The resulting JavaScript code should be human-readable and easily correspond to the input PHP code, so that it can be easily inspected and debugged by humans.

Limitations

Since it is not a general purpose transpiler, there are limits on what the transpiler accepts. If it encounters PHP code which it cannot transpile properly, it will normally throw an exception, although in some edge cases it can produce JavaScript code from PHP code it should theoretically not support; such code is not guaranteed to run properly.

At this point the transpiler will convert most of functional PHP code, which does not contain any class definitions or usage. Thus, the following sample PHP code will translate nicely to JavaScript:

function myFunc($param)
{
    return $param + 1;
}
$d = $a ? $b + myFunc($c) : $b ** $c;

The resulting JavaScript is:

var b, c, d, a;
function myFunc(param)
{
    return param + 1;
}
d = a ? b + myFunc(c) : Math.pow(b, c);

The produced code is easily readable by humans, and contains only minimum overhead.

PHP syntax not yet accepted:

The following PHP constructs are not supported at the moment, and will likely not be supported at all, although we may implement some of them if time permits:

Computed variable and function names

Seriously, you really shouldn't be doing this anyway.

$a = $$b;
$a = $b(); // May be supported in the future

String subscripting with brackets

This mainly stems from the fact that the transpiler does not know whether the variable is an array or a string. With PHP 7 type hinting, this may be improved in the future if the type of the variable can be deduced.

$a = "string";
$b = $a[1]; // Use substr() instead

Some array operations

Again, with the plus operator the transpiler does not know if the variable is a numeric or an array. PHP 7 type hinting may improve this as well.

$array = array();
$a = $array + $array2;

Some binary and assigmment operators

Some of these operators may be supported with ugly JavaScript, so the situation may improve.

$a = $b <=> $c; // May be supported in the future
$a = $b xor $c;
$a = $b ?: $c; // May be supported in the future
$a ??= $b;
$a **= $c;

Variable references

They cannot be easily reproduced in JavaScript in general.

$a = &$b;
function func(&$a) {}

Multi-level break and continue

They cannot be easily reproduced in JavaScript in general.

continue 2;
break $a;

Classes and exceptions:

They are at the moment not supported at all, but we intend to work on them so you can expect a fairly broad support in the future.

PHP standard library

PHP is not just a language; it is also a quite extensive library of standard functions such as substr() or preg_match(). We intend to implement a broad range of these functions, but since it requires a significant effort, it will take us some time to get there. Some functions will never be supported because they make no sense in a browser environment (such as file I/O). Some functions require too much effort compared to expected gain, so we may choose not to implement them (although you can do it, and we will gladly incpororate your code!). In general, we want to focus on string and array functions first because that's what we use in our own code.

As a teaser, a few string functions are implemented already, so you can try code such as this:

$a = strlen($b);
$a = substr($b, $c, 1);
$a = strtolower($b);

The resulting JavaScript will be:

a = (b).length;
a = (b).charAt(c);
a = (b).toLowerCase();

aftermarketpl/php2js 适用场景与选型建议

aftermarketpl/php2js 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「javascript」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aftermarketpl/php2js 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aftermarketpl/php2js 我们能提供哪些服务?
定制开发 / 二次开发

基于 aftermarketpl/php2js 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-04-14