|
| 1 | +# Evalue |
| 2 | + |
| 3 | +Store and execute PHP code snippets using Evalue class. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- store PHP code for multiple executions; |
| 8 | +- provide context variables during construction and execution; |
| 9 | +- support for complex PHP code snippets; |
| 10 | +- automatic stripping of `<?php` tags; |
| 11 | +- variable scope isolation between runs; |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require maximaster/evalue |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Basic Usage |
| 24 | + |
| 25 | +```php |
| 26 | +use Maximaster\Evalue\Evalue; |
| 27 | + |
| 28 | +// Create an Evalue instance with PHP code |
| 29 | +$evalue = new Evalue('return $number * 2;'); |
| 30 | + |
| 31 | +// Run the code with context |
| 32 | +$result = $evalue->run(['number' => 21]); // Returns 42 |
| 33 | +``` |
| 34 | + |
| 35 | +### With Constructor Context |
| 36 | + |
| 37 | +```php |
| 38 | +$evalue = new Evalue( |
| 39 | + 'return $greeting . " " . $name;', |
| 40 | + ['greeting' => 'Hello'] |
| 41 | +); |
| 42 | + |
| 43 | +// Run with additional context |
| 44 | +$result = $evalue->run(['name' => 'John']); // Returns "Hello John" |
| 45 | +``` |
| 46 | + |
| 47 | +### Complex Code Examples |
| 48 | + |
| 49 | +```php |
| 50 | +// Multi-line code |
| 51 | +$code = <<<'PHP' |
| 52 | + $sum = 0; |
| 53 | + for($i = 1; $i <= $max; $i++) { |
| 54 | + $sum += $i; |
| 55 | + } |
| 56 | + return $sum; |
| 57 | +PHP; |
| 58 | + |
| 59 | +$evalue = new Evalue($code); |
| 60 | +$result = $evalue->run(['max' => 5]); // Returns 15 |
| 61 | +``` |
| 62 | + |
| 63 | +### Context Overriding |
| 64 | + |
| 65 | +```php |
| 66 | +$evalue = new Evalue( |
| 67 | + 'return $name;', |
| 68 | + ['name' => 'John'] |
| 69 | +); |
| 70 | + |
| 71 | +// Constructor context can be overridden in run() |
| 72 | +$result = $evalue->run(['name' => 'Jane']); // Returns "Jane" |
| 73 | +``` |
| 74 | + |
| 75 | +## Security |
| 76 | + |
| 77 | +⚠️ **Warning**: This library uses PHP's `eval()` function. Only use it with trusted code. Never execute user-provided |
| 78 | +code without proper validation and sanitization. |
| 79 | + |
| 80 | +## Context Rules |
| 81 | + |
| 82 | +Variable names in the context must follow PHP's variable naming conventions: |
| 83 | + |
| 84 | +- Must start with a letter or underscore |
| 85 | +- Can contain letters, numbers, underscores |
| 86 | +- Supports extended ASCII characters (e.g., `π`, `área`, `über`) |
| 87 | + |
| 88 | +Invalid examples: |
| 89 | +```php |
| 90 | +$evalue->run([ |
| 91 | + '123name' => 'invalid', // Invalid: starts with number |
| 92 | + 'my-var' => 'invalid', // Invalid: contains hyphen |
| 93 | + 'my var' => 'invalid' // Invalid: contains space |
| 94 | +]); |
| 95 | +``` |
| 96 | + |
| 97 | +## Development / Contribution |
| 98 | + |
| 99 | +```bash |
| 100 | +composer test # runs kahlan tests. |
| 101 | +composer lint # runs static analysis to check the code. |
| 102 | +composer ci # both test & lint. |
| 103 | +composer fix # automatically fixes some lint errors. |
| 104 | +``` |
0 commit comments