PHPackages                             vouchedfor/rules-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. vouchedfor/rules-bundle

ActiveSymfony-bundle[Framework](/categories/framework)

vouchedfor/rules-bundle
=======================

Rules engine for Symfony

4.3(6y ago)27.2k↓36.5%[2 PRs](https://github.com/vouchedfor/RulesBundle/pulls)MITPHPCI passing

Since Aug 14Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/vouchedfor/RulesBundle)[ Packagist](https://packagist.org/packages/vouchedfor/rules-bundle)[ Docs](https://github.com/vouchedfor/RulesBundle)[ RSS](/packages/vouchedfor-rules-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (6)Used By (0)

RulesBundle
===========

[](#rulesbundle)

This bundle creates the facility to add a basic rules engine. It's inspired by this article:

The bundle creates a Rule entity which is comprised of three elements:

```
1. Event - the dispatched event that causes this rule to be invoked (e.g. 'user:registered')
2. Conditions - a json array of expressions. (e.g. '["data.age > 67", "data.weight > 200"]') See [http://symfony.com/doc/current/components/expression_language.html](http://symfony.com/doc/current/components/expression_language.html) for more on the ExpressionLanguage component.
3. Actions - a json array of events to be dispatched. (e.g. '["user:mark-as-retired", "user:send-email|Retirement"]'.  Note that it is possible to send a single parameter by adding a '|' after the name of the action/event to be dispatched)

```

Installation
------------

[](#installation)

First, install it with composer:

```
composer require vouchedfor/rules-bundle:dev-master

```

Then, add the following in your **AppKernel** bundles (note that 'new DataDog\\AuditBundle\\DataDogAuditBundle(),' may have been added previously).

```
// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        ...
        new VouchedFor\AuditUiBundle\VouchedForRulesBundle(),
        ...
    );
    ...
}

```

Create the 'rule' database table used by the bundle:

Using [Doctrine Migrations Bundle](http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html):

```
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate

```

Using Doctrine Schema:

```
php app/console doctrine:schema:update --force

```

Usage
-----

[](#usage)

Create a class that extends VouchedFor\\RulesBundle\\Event\\RuleEvent.

Add the events you want to listen to as tags for a RuleListener service (in this example, 'user:mark-as-retired' and 'user:send-email'):

```
// app/config/services.yml
app.rule.listener:
    class: VouchedFor\RulesBundle\EventListener\RuleListener
    arguments:
      - "@doctrine.orm.entity_manager"
      - "@event_dispatcher"
    tags:
      - { name: kernel.event_listener, event: user:registered, method: handleEvent }
      - { name: kernel.event_listener, event: user:updated, method: handleEvent }

```

Add a listener/subscriber to listen to the dispatched actions when a rule passes. For example:

```
