Skip to content

Commit d3ca447

Browse files
committed
feat: add basic functionality
0 parents  commit d3ca447

14 files changed

Lines changed: 704 additions & 0 deletions

File tree

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
charset = utf-8
7+
end_of_line = LF
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
max_line_length = 120
11+
12+
[COMMIT_EDITMSG]
13+
max_line_length = 0

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Healthchecks
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
php: ['8.1', '8.2', '8.3']
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php }}
23+
coverage: none
24+
25+
- name: Validate composer.json
26+
run: composer validate --strict
27+
28+
- name: Cache Composer packages
29+
id: composer-cache
30+
uses: actions/cache@v3
31+
with:
32+
path: vendor
33+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
34+
restore-keys: |
35+
${{ runner.os }}-php-
36+
37+
- name: Install dependencies
38+
run: composer install --prefer-dist --no-progress
39+
40+
- name: Lint code
41+
run: composer lint
42+
43+
- name: Test code
44+
run: composer test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.devbox
2+
/vendor

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
![CI](https://github.com/maximaster/evalue/workflows/CI/badge.svg)
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+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "maximaster/evalue",
3+
"description": "Eval as class.",
4+
"type": "library",
5+
"license": "MIT",
6+
"require": {
7+
"php": ">=8.1"
8+
},
9+
"require-dev": {
10+
"kahlan/kahlan": "^6.0",
11+
"symplify/easy-coding-standard": "^12.5",
12+
"phpstan/phpstan": "^2.1"
13+
},
14+
"scripts": {
15+
"test": "kahlan",
16+
"lint": [
17+
"ecs",
18+
"phpstan"
19+
],
20+
"fix": "ecs --fix",
21+
"qa": [
22+
"composer test",
23+
"composer lint"
24+
]
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Maximaster\\Evalue\\": "src/"
29+
}
30+
},
31+
"authors": [
32+
{
33+
"name": "Лавриненко Максим",
34+
"email": "maxim.lavrinenko@maximaster.ru"
35+
}
36+
]
37+
}

0 commit comments

Comments
 (0)