MilliRules: The PHP Rule Engine That Thinks Before It Loads
A fluent, package-aware engine for turning tangled if statements into readable policy, with lazy context loading, dynamic placeholders, and WordPress support that stays out of the core.
- MilliRules is most interesting as a lazy rule engine that asks for context only when a rule actually needs it.
- Its fluent API turns policy into readable when-then logic without tying the core to WordPress.
- The package layer makes WordPress feel like an adapter instead of a constraint.
- Action locking and placeholder resolution make the engine practical for real request-time behavior.
MilliRules is not trying to be a clever syntax trick. It is trying to make messy application logic boring in the best way: declarative, portable, and hard to break.
That matters most in WordPress, where rule logic tends to sprawl across hooks, helper functions, and half-remembered conditionals. MilliRules pulls that logic into a single rule layer, then avoids the usual tax by loading context only when a rule asks for it.
Why MilliRules Exists
The usual failure mode is familiar. A new business rule arrives, then another, then a third. Soon the codebase is full of nested branches, duplicated checks, and side effects hiding in callbacks.
Define rules declaratively. No nested conditionals, no callback spaghetti — just clean, reusable logic that keeps your codebase DRY.
MilliRules answers that with a fluent builder API. Instead of asking developers to scatter logic across the app, it lets them describe conditions and actions in one place, then hands execution to the engine.
The Mental Model: Policy First, Code Paths Second
Rules::create()
->whenAll()
->condition('request.url', 'starts_with', '/api/')
->condition('user.role', 'equals', 'admin')
->then()
->action('header', 'X-Rule', 'api-admin');
The interesting part is not that this reads cleanly. It is that the engine normalizes naming so teams can use either when_all() or whenAll(). That small choice makes the API feel at home in both WordPress-style code and modern PHP code.
The Real Trick: Lazy Context Loading
This is the core design choice. MilliRules does not treat context as a giant blob to preload. It treats it like a registry of providers, and only opens the ones a live rule needs.
That makes the engine feel lightweight in environments where every request can be expensive. If a rule only cares about the request URL and a post ID, there is no reason to wake up user, taxonomy, or WordPress-specific providers.
That design shows up in the implementation details too. The Context class acts like a provider registry, dot notation reaches into nested values, and placeholder resolution bridges live data into action strings right before execution.
Redirect to {request.url}
Set header X-Category to {category.key}
Cache key: {post.id}:{request.url}
So a rule definition stays static, but the action can still be dynamic. That is the bridge between declarative policy and runtime behavior.
Packages Turn WordPress Into an Adapter
MilliRules keeps WordPress at the edge of the system, not the center. The core engine stays framework-agnostic, while the WordPress package adds the environment-specific pieces a site actually needs.
| Approach | What it optimizes for | Trade-off |
|---|---|---|
| Nested functions and hooks | Direct control | Logic spreads across files and becomes hard to reason about |
| Generic rule engine | Abstraction | Often lacks opinionated support for WordPress behavior |
| MilliRules | Declarative policy with selective loading | Requires learning the rule model, but keeps the core portable |
That is the real portability story. WordPress is not baked into the engine. It is a package, which means the same core can survive outside the CMS without being rewritten around a platform assumption.
MilliRules is the rule engine behind MilliCache, our Redis-powered full-page cache for WordPress. Every cache rule, TTL setting, and flag assignment uses the same fluent API.
Immediate Execution, Then Safety
MilliRules executes actions as soon as a rule matches. That suits middleware-style behavior, where the point is to make a decision now, not assemble a list of maybe-matches for later.
The lock on actions matters here. If one rule already redirected or set a terminal outcome, the engine should not let a second rule fire a conflicting action right after it.
if ($engine->hasLockedAction('redirect')) {
return;
}
$engine->execute($rule);
That is a small detail with a big consequence. Rule engines often fail at the edges, where duplicate actions or loops create hard-to-debug behavior. The lock is the guardrail.
Where MilliRules Fits
Compared with plain PHP conditionals, MilliRules is cleaner. Compared with generic rule engines, it is more opinionated about how PHP and WordPress applications actually work.
It also reads like a serious codebase, not a quick utility. The repository includes tests, docs, static analysis, and a small surface area that suggests the project is optimized for clarity rather than feature bloat.
| Dimension | Imperative WordPress logic | MilliRules |
|---|---|---|
| Readability | Branches and callbacks accumulate | Rules stay grouped as policy |
| Performance | Often loads too much too early | Loads only the context a rule needs |
| Portability | Tied to one application shape | Core stays framework-agnostic |
| Safety | Conflicting side effects are easy to introduce | Action locking adds a control point |
The editorial takeaway is simple. MilliRules shows that declarative design in PHP does not have to mean abstraction for its own sake. It can mean fewer branches, less wasted loading, and a better boundary between policy and execution.





