定制 smuuf/primi 二次开发

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

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

smuuf/primi

Composer 安装命令:

composer require smuuf/primi

包简介

A scripting language written in PHP & executable within PHP.

README 文档

README

Primi

Primi

A scripting language written in PHP & interpreted in PHP.

Primi is meant for PHP developers who want to allow their clients to write their own custom logic. Primi allows you (the developer) to execute untrusted code (provided simply as a string) inside a sandbox, safely separated from its surroundings.

Code Climate Packagist
Maintainability Test Coverage Latest Stable Version License Total Downloads

Quick access

Rationale

Primi - as things sometimes go in life - began as an answer to a practical problem: I needed some general-purpose (ie. not too much domain-specific) scripting language that my other app's users could use to write their simple custom logic. I needed some universally usable and primitive scripting thing, with familiar syntax (PHP-like x C-like x JS-like) and one that could be safely executed inside pure PHP environment (no external depedencies on v8js, v8 and whatnot - meaning Javascript is out of the game...).

Thus, Primi was (mostly as an experiment) created.

Installation

You can either use Primi as a standalone package (a) - for its development, making contributions, debugging it, or to just play with it. Or you can use Primi in your own projects (b) by installing it as a Composer dependency.

You'll want either one of these:

  • git clone https://github.com/smuuf/primi.git (standalone use)
  • composer require smuuf/primi (using Primi as a library in your own project)

a) Standalone installation

  1. Clone this repo.
    • git clone https://github.com/smuuf/primi.git
  2. Install Composer dependencies.
    • composer install
  3. Run something with Primi CLI.
    • chmod +x ./primi && ./primi -s -c "a = 1 + 2 / 3;"

Convenient installation Oneliner™:

git clone https://github.com/smuuf/primi.git && cd primi && composer install && chmod +x ./primi && ./primi -s -c "msg = 'Primi works.';"

Extra stuff:

  • Register Primi's CLI executable for current user so typing primi will behave like a binary (otherwise you'd need to write ./primi and would have to be in the right directory):

    ./bin/registerbin
    

    Note: This will add an alias in .bashrc for current user.

  • Run tests (tests are located inside ./tests/ directory):

    ./bin/test
    
  • Rebuild parser (when you modify Primi's grammar definitions, you will want to rebuild the parser to reflect the changes):

    ./bin/buildparser
    

b) Using Primi as a library

  1. First, install Primi Composer package: composer require smuuf/primi
  2. Then use it like this:
<?php

require __DIR__ . "/vendor/autoload.php";

$context = new \Smuuf\Primi\Context;
$interpreter = new \Smuuf\Primi\Interpreter($context);

try {

    // Let the interpreter run a source code.
    $interpreter->run('a = 1; b = a + 2; c = "some string"; d = c + " extra thing";');

    // Get defined variables from primary context and print them.
    foreach ($context->getVariables() as $name => $value) {
        printf("%s (%s) ... %s\n", $name, $value::TYPE, $value->getCoreValue());
    }

} catch (\Smuuf\Primi\ErrorException $e) {
    die($e->getMessage());
}

Running this code would output:

a (number) ... 1
b (number) ... 3
c (string) ... some string
d (string) ... some string extra thing

REPL mode

Primi provides a convenient "sandbox" REPL mode which is launched by executing ./primi without any argument. You can use this for messing around with the language or to test any new stuff you might be trying to implement (e.g. your own Primi extensions written in PHP).

REPL example usage

In this mode, all statements are executed when entered and the result value of the last expression is returned. REPL commands history is preserved between separate sessions (history is stored in ~/.primi_history file).

Language reference

The basics of the language syntax and data types are found here: https://github.com/smuuf/primi/blob/master/docs/language_reference.md

smuuf/primi 适用场景与选型建议

smuuf/primi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.37k 次下载、GitHub Stars 达 132, 最近一次更新时间为 2017 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 78.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 133
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 132
  • Watchers: 4
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-19