定制 maximaster/evalue 二次开发

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

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

maximaster/evalue

最新稳定版本:1.0.0

Composer 安装命令:

composer require maximaster/evalue

包简介

Eval as class.

README 文档

README

Store and execute PHP code snippets using Evalue class.

Features

  • store PHP code for multiple executions;
  • provide context variables during construction and execution;
  • support for complex PHP code snippets;
  • automatic stripping of <?php tags;
  • variable scope isolation between runs;

Installation

composer require maximaster/evalue

CI

Usage

Basic Usage

use Maximaster\Evalue\Evalue;

// Create an Evalue instance with PHP code
$evalue = new Evalue('return $number * 2;');

// Run the code with context
$result = $evalue->run(['number' => 21]); // Returns 42

With Constructor Context

$evalue = new Evalue(
    'return $greeting . " " . $name;',
    ['greeting' => 'Hello']
);

// Run with additional context
$result = $evalue->run(['name' => 'John']); // Returns "Hello John"

Complex Code Examples

// Multi-line code
$code = <<<'PHP'
    $sum = 0;
    for($i = 1; $i <= $max; $i++) {
        $sum += $i;
    }
    return $sum;
PHP;

$evalue = new Evalue($code);
$result = $evalue->run(['max' => 5]); // Returns 15

Context Overriding

$evalue = new Evalue(
    'return $name;',
    ['name' => 'John']
);

// Constructor context can be overridden in run()
$result = $evalue->run(['name' => 'Jane']); // Returns "Jane"

Security

⚠️ Warning: This library uses PHP's eval() function. Only use it with trusted code. Never execute user-provided code without proper validation and sanitization.

Context Rules

Variable names in the context must follow PHP's variable naming conventions:

  • Must start with a letter or underscore
  • Can contain letters, numbers, underscores
  • Supports extended ASCII characters (e.g., π, área, über)

Invalid examples:

$evalue->run([
    '123name' => 'invalid',    // Invalid: starts with number
    'my-var' => 'invalid',     // Invalid: contains hyphen
    'my var' => 'invalid'      // Invalid: contains space
]);

Development / Contribution

composer test   # runs kahlan tests.
composer lint   # runs static analysis to check the code.
composer ci     # both test & lint.
composer fix    # automatically fixes some lint errors.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-17