leongrdic/smplang
SMPLang is a small PHP language/parser package for defining and evaluating simple expressions in your app. Useful for lightweight DSLs, rules, filters, or templating-like syntax, with an emphasis on minimal setup and easy integration into Laravel projects.
smplang is a lightweight, expression-evaluation DSL (Domain-Specific Language) designed to replace eval() safely. It fits well in Laravel applications where:
eval() security risks).__call/__get, useful for:
user->getAttribute('dynamic_property')).App\Exceptions\Handler.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Security | High | Validate all input expressions (whitelist allowed functions/objects). Use a sandboxed environment (e.g., restrict globals). |
| Performance | Medium | Benchmark against native PHP for complex expressions. Cache compiled expressions if reused. |
| Compatibility | Low | Test with Laravel’s autoloading (PSR-4). May need to mock __call/__get for strict mode. |
| Maintenance | Low | MIT license + active repo (last release 2022). Fork if stalled. |
| Debugging | Medium | Override exceptions to log context (e.g., input expression, stack trace). |
2 + 2) vs. complex object traversal (user->orders->sum('amount'))?date_diff(), array_filter())?new \App\Services\Calculator())?+, -, user->*).null/false or throw Laravel-specific exceptions?Smplang as a singleton/binder in AppServiceProvider.Smplang::evaluate($expression) for clean syntax.__get/__call to expose model attributes dynamically.whereRaw() or custom scopes.Smplang::evaluate($userInput)).Validator::extend() rules with expressions (e.g., "age > 18 && hasLicense == true").eval() or create_function() in legacy code.smplang.tap() to sandbox expressions (e.g., restrict file_* functions).__call/__get don’t conflict).new class { public function __call($name) { ... } }).EvaluatingExpression events for observability.Smplang::compile($expr)).if (user->tier == 'premium && balance > 0')).order->items->sum('price')).Smplang::addFunction('discount', fn($price) => $price * 0.9)).Smplang::resolve('user->getFreshOrders()')).leongrdic/smplang for PHP 8.1+ compatibility.phpunit tests for edge cases).Handler to log SmplangException details.Smplang::debug($expr) method to dump parsed AST.smplang exceptions to user-friendly messages (e.g., "Invalid expression: user->nonexistent").Smplang::cache($expr, 60)).queue:work (e.g., SmplangJob::dispatch($expr)).user->orders->items->...).function foo() { foo() }).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malicious Expression | RCE (Remote Code Exec) | Whitelist functions/objects. |
| Invalid Syntax | App Crashes | Graceful fallbacks (e.g., return null). |
| Circular References | Infinite Loop | Set max evaluation depth. |
| Dependency Unavailability | Broken Logic | Mock unresolved objects in tests. |
| PHP Version Incompatibility | Deployment Blockers | Use Laravel’s php-version policy. |
How can I help you explore Laravel packages today?