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.

9 min read • View on GitHub • More from MilliPress

A wide editorial illustration shows tangled condition branches and hooks on the left, then a clean rule ledger on the right. In the center, a small context vault opens only the drawers a rule needs, suggesting that logic can stay readable without loading every dependency up front.
MilliRules replaces branching clutter with a rule layer that asks for data only when a rule actually needs it.
Key Takeaways

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.

MilliPress, Organization · MilliRules: Fluent PHP When-Then Rules

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 engine evaluates rules, opens only the providers a rule needs, then resolves placeholders at the moment an action fires.

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.

A close-up illustration shows a rule card reaching toward a cabinet of context drawers. Only the request.url and post.id drawers open, while a placeholder string is filled at the moment the action fires. A small locked gate sits beside a second action path to show conflict prevention.
Lazy context loading keeps the engine selective, then resolves values at execution time instead of up front.

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.

ApproachWhat it optimizes forTrade-off
Nested functions and hooksDirect controlLogic spreads across files and becomes hard to reason about
Generic rule engineAbstractionOften lacks opinionated support for WordPress behavior
MilliRulesDeclarative policy with selective loadingRequires 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.

MilliPress, Organization · MilliRules: Fluent PHP When-Then Rules

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.

DimensionImperative WordPress logicMilliRules
ReadabilityBranches and callbacks accumulateRules stay grouped as policy
PerformanceOften loads too much too earlyLoads only the context a rule needs
PortabilityTied to one application shapeCore stays framework-agnostic
SafetyConflicting side effects are easy to introduceAction 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.