phil / rules-bundle
Store named business rules and check them anywhere in Symfony: conditions as tagged services, evaluated with the facts of the request or the user.
Package info
github.com/parler-haut-interagir-librement/rules-bundle
Type:symfony-bundle
pkg:composer/phil/rules-bundle
Requires
- php: >=8.5
- ext-mbstring: *
- doctrine/dbal: ^4.6
- doctrine/orm: ^3.8
- phil/rules: ^0.1@dev
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- symfony/config: ^8.2
- symfony/dependency-injection: ^8.2
- symfony/service-contracts: ^3.8
Requires (Dev)
- doctrine/doctrine-bundle: ^3.4
- doctrine/persistence: ^4.2
- phpunit/phpunit: ^13.4
- psr/log: ^3.0
- symfony/clock: ^8.2
- symfony/console: ^8.2
- symfony/event-dispatcher: ^8.2
- symfony/event-dispatcher-contracts: ^3.8
- symfony/framework-bundle: ^8.2
- symfony/http-kernel: ^8.2
- symfony/security-bundle: ^8.2
- symfony/security-core: ^8.2
Suggests
- doctrine/doctrine-bundle: For the named rules stored with Doctrine
- symfony/console: For phil:rules:lint, phil:rules:list and phil:rules:types
- symfony/security-core: For the attribute rule:<name> of is_granted()
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 02:53:56 UTC
README
Symfony integration of rules: named stored rules with conditions as tagged services.
phil/rules-bundle brings the rules of phil/rules into a Symfony application. A rule is stored once under a stable
name — course:intro/prerequisites, members-only, free-shipping — and every part of the application asks for it by
that name: a voter, a price book, a list of lessons. The library alone suits an application without Symfony.
- Scopes: each scope accepts its own condition types, so that a price rule cannot name a condition of access. The
generic comparisons of phil/rules serve every scope; a package or the application adds its types with
#[AsConditionType]or a tag. - Stored by name, safely edited: rules are checked against their scope before they are kept in Doctrine, in their canonical JSON form with a fingerprint. Each change is made on the revision its editor read; one made on an older revision is refused rather than written over another.
- Fast in a worker: a rule is read and compiled once per request, kept for the rest of it, and forgotten between two requests, so that a revision stored elsewhere is read on the next one.
- In Symfony Security:
is_granted('rule:<name>', subject)evaluates the rule with the facts the application's providers give about the subject and the person signed in. A missing, broken or failing rule refuses. - Checked from the command line:
phil:rules:lintchecks rule files against a scope,phil:rules:listfinds the stored rules a scope no longer accepts, andphil:rules:typesdescribes the types for a rule builder.
Status
| Part | State |
|---|---|
PhilRulesBundle and its configuration phil_rules |
Implemented and tested |
Scopes and their condition types, #[AsConditionType] |
Implemented and tested |
| Named rules stored with Doctrine, revisions, events | Implemented and tested |
Fact providers and the voter rule:<name> |
Implemented and tested |
The commands phil:rules:lint, phil:rules:list, phil:rules:types |
Implemented and tested |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements, installing, the table, a first rule. |
| Configuration | Every option, with an example. |
| Named rules | Scopes, condition types, storing and revising, facts, the voter, the commands. |
| Extension points | The services to replace, the tags to add to, and when they are stable. |
| Testing | The bundle's tests and its benchmark. |
| Reference | Every class and service of the bundle. |
Quick start
// config/bundles.php Phil\RulesBundle\PhilRulesBundle::class => ['all' => true],
# config/packages/phil_rules.yaml phil_rules: scopes: [course.access]
$rules->define('course:intro/prerequisites', 'course.access', '{"version": 1, "condition": {"all": [ {"type": "lesson.completed", "operands": {"lesson": "welcome"}}, {"type": "fact.equals", "operands": {"fact": "role", "value": "student"}} ]}}');
{% if is_granted('rule:course:intro/prerequisites', course) %}
Requirements
PHP 8.5 or later with the extension mbstring, Symfony 8.2 or later, and Doctrine ORM 3 for the stored rules.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.