PHP Classes

How to Use a PHP Business Rules Engine to Customize Rules of Operation of a Business Using the Package Brick Engine: Scripting language to implement business rules

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-01-25 (20 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
brick-engine 1.0MIT/X Consortium ...7Parsers, PHP 7, Business
Description 

Author

This package provides a scripting language to implement business rules.

It implements an engine that can parse the code of the Brick language, which is defined as strings.

The engine allows applications to define PHP callback functions that are used to implement functions of the Brick language.

It also allows to assign values to variables that can be accessed by code in the Brick language when it is executed by the Brick engine.

Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 21 packages by
Country: Turkey Turkey
Innovation award
Innovation award
Nominee: 14x

Example

<?php

require_once __DIR__ . '/vendor/autoload.php';

use
IsaEken\BrickEngine\BrickEngine;
use
IsaEken\BrickEngine\Enums\ValueType;
use
IsaEken\BrickEngine\Runtime\Context;
use
IsaEken\BrickEngine\Value;

$contents = file_get_contents(__DIR__ . '/examples/code.txt');

$engine = new BrickEngine();
$engine->context = new Context([
   
'asd' => new Value(ValueType::Numeric, 123),
], [
   
'asd' => function (Context $context) use ($engine): Value {
       
$arguments = array_map(function ($argument) use ($context) {
            return
$context->value($argument)->data;
        },
$context->arguments);
       
$value = array_reduce($arguments, fn ($a, $b) => $a + $b, 0);

        return new
Value(ValueType::Numeric, $value);
    },
   
'echo' => function (Context $context) use ($engine): Value {
        foreach (
$context->arguments as $argument) {
            print(
sprintf("%s\n", $engine->context->value($argument)->data));
        }

        return new
Value(ValueType::Void);
    },
]);


$result = $engine->run($contents);
dd($result->value->data);


Details

BrickEngine

BrickEngine is a simple, flexible, and extensible script engine written in PHP. It allows you to manage custom business rules, e-commerce discounts, automation scenarios, or any workflow you define without "changing code."

> Status: The project is still in development and may not yet be fully ready for production use. > Support Us: If you like the project, please star it or contribute to support its development.

Why Choose BrickEngine?

Features

  • Dynamic Scripting Language: BrickEngine supports a wide range of operations through its flexible scripting language.
  • Easy Extensibility: You can add your own functions (e.g., `apply_discount`, `dump`, `json_decode`) and variables to the context. All functions and variables must be explicitly defined and bound to BrickEngine.
  • API & External Integrations: Whether for e-commerce, payment systems, or microservices, you can utilize scripts to make external API calls.
  • Clean and Readable Syntax: Easily perform conditional operations using simple `if` structures, loops, and assignments.
  • Performance and Flexibility: Centralize and manage your business rules to avoid complex branching and if-else chains.
  • Support for Various Data Types: Operate on different data types, such as strings, numbers, booleans, and arrays.
  • Loop and Conditional Structures: Includes support for control structures like `while`, `if-else`, and `for` loops.

Installation

Via Composer

composer require isaeken/brick-engine

Quick Start

The example below demonstrates a simple script that applies a discount if the cart total exceeds 100 units and then processes an e-commerce scenario by fetching a response from an external API.

<?php

require __DIR__ . '/vendor/autoload.php';

use IsaEken\BrickEngine\BrickEngine;

$script = <<<BRICK
if (cart['total'] > 100) {
    apply_discount();
    return "You have a discount!";
}

response = fetch('https://api.example.com');
response = json_decode(response);
return response['message'];
BRICK;

$engine = new BrickEngine();

// Define functions to be used within the script
$engine->context->functions['apply_discount'] = function () {
    $this->context->cart['total'] -= 10; // Apply a discount of 10 units
};

// Define variables to be used in the script
$engine->context->variables['cart'] = ['total' => 120];

$result = $engine->run($script)->value->data;
echo $result; // "You have a discount!" or API message

How It Works

  1. The `BrickEngine` class interprets and executes the script.
  2. Functions in the `context->functions` array can be directly accessed within the script.
  3. The `context->variables` array defines variables that can be accessed in the script, such as `cart['total']`.

Running in Docker

You can run the example script in a Docker container using the following commands:

docker build -t brick-engine .
docker run --rm -v "$(pwd)":/app brick-engine ./examples/example.bee

Use Cases

  • E-Commerce Rules: Create dynamic business rules for cart totals, shipping cost calculations, or payment steps.
  • Form Validation / Workflow: Process user inputs and direct them to different scenarios.
  • Content Management: Apply dynamic rule sets for news, blogs, or similar content platforms.
  • API & Microservice Integration: Manage external API calls based on specific conditions (e.g., order confirmation, payment processing).
  • Game / Application Logic: Manage simple rule sets for game servers or create rapid prototypes using mini-scripts within applications.

Roadmap

Read the full ROADMAP.md file for more details on the project's future development plans.

License

This project is licensed under the MIT License. For more information, see the LICENSE.md file.

Contact & More Information


  Files folder image Files (87)  
File Role Description
Files folder image.github (1 file, 1 directory)
Files folder imagebin (2 files)
Files folder imageexamples (4 files)
Files folder imagesrc (6 files, 9 directories)
Files folder imagetests (2 files, 2 directories)
Plain text file composer.json Data Auxiliary data
Plain text file composer.lock Data Auxiliary data
Plain text file Dockerfile Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file phpunit.xml Data Auxiliary data
Plain text file README.md Doc. Read me
Plain text file ROADMAP.md Data Auxiliary data
Plain text file test.php Example Example script

  Files folder image Files (87)  /  .github  
File Role Description
Files folder imageworkflows (1 file)
  Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files (87)  /  .github  /  workflows  
File Role Description
  Plain text file tests.yml Data Auxiliary data

  Files folder image Files (87)  /  bin  
File Role Description
  Plain text file brick Example Example script
  Plain text file repl Example Example script

  Files folder image Files (87)  /  examples  
File Role Description
  Plain text file code.txt Doc. Documentation
  Plain text file code2.txt Doc. Documentation
  Plain text file example.bee Data Auxiliary data
  Plain text file simple.txt Doc. Documentation

  Files folder image Files (87)  /  src  
File Role Description
Files folder imageContracts (4 files)
Files folder imageDataObjects (1 file)
Files folder imageEnums (1 file)
Files folder imageExceptions (13 files)
Files folder imageExpressions (9 files)
Files folder imageExtensions (4 files)
Files folder imageLexers (3 files)
Files folder imageRuntime (1 file)
Files folder imageStatements (10 files)
  Plain text file BrickEngine.php Class Class source
  Plain text file ExecutionResult.php Class Class source
  Plain text file Node.php Class Class source
  Plain text file Parser.php Class Class source
  Plain text file Runtime.php Class Class source
  Plain text file Value.php Class Class source

  Files folder image Files (87)  /  src  /  Contracts  
File Role Description
  Plain text file ExpressionInterface.php Class Class source
  Plain text file ExtensionInterface.php Class Class source
  Plain text file NodeInterface.php Class Class source
  Plain text file StatementInterface.php Class Class source

  Files folder image Files (87)  /  src  /  DataObjects  
File Role Description
  Plain text file Token.php Class Class source

  Files folder image Files (87)  /  src  /  Enums  
File Role Description
  Plain text file ValueType.php Aux. Configuration script

  Files folder image Files (87)  /  src  /  Exceptions  
File Role Description
  Plain text file ArrayKeyNotFoundException.php Class Class source
  Plain text file FunctionNotFoundException.php Class Class source
  Plain text file IgnorableException.php Class Class source
  Plain text file InternalCriticalException.php Class Class source
  Plain text file InvalidForeachTargetException.php Class Class source
  Plain text file InvalidLeftSideTargetException.php Class Class source
  Plain text file InvalidLiteralException.php Class Class source
  Plain text file InvalidStatementException.php Class Class source
  Plain text file InvalidSyntaxException.php Class Class source
  Plain text file UnexpectedCharacterException.php Class Class source
  Plain text file UnexpectedTokenException.php Class Class source
  Plain text file UnsupportedException.php Class Class source
  Plain text file VariableNotFoundException.php Class Class source

  Files folder image Files (87)  /  src  /  Expressions  
File Role Description
  Plain text file ArrayAccessExpression.php Class Class source
  Plain text file ArrayElementExpression.php Class Class source
  Plain text file ArrayLiteralExpression.php Class Class source
  Plain text file BinaryExpression.php Class Class source
  Plain text file FunctionCallExpression.php Class Class source
  Plain text file IdentifierExpression.php Class Class source
  Plain text file LiteralExpression.php Class Class source
  Plain text file ObjectLiteralExpression.php Class Class source
  Plain text file ParamDefinitionExpression.php Class Class source

  Files folder image Files (87)  /  src  /  Extensions  
File Role Description
  Plain text file ConsoleExtension.php Class Class source
  Plain text file HttpExtension.php Class Class source
  Plain text file JsonExtension.php Class Class source
  Plain text file VarDumperExtension.php Class Class source

  Files folder image Files (87)  /  src  /  Lexers  
File Role Description
  Plain text file BaseLexer.php Class Class source
  Plain text file Lexer.php Class Class source
  Plain text file ScriptLexer.php Class Class source

  Files folder image Files (87)  /  src  /  Runtime  
File Role Description
  Plain text file Context.php Class Class source

  Files folder image Files (87)  /  src  /  Statements  
File Role Description
  Plain text file AssignmentStatement.php Class Class source
  Plain text file BlockStatement.php Class Class source
  Plain text file ExpressionStatement.php Class Class source
  Plain text file ForeachStatement.php Class Class source
  Plain text file ForStatement.php Class Class source
  Plain text file FunctionDeclareStatement.php Class Class source
  Plain text file IfStatement.php Class Class source
  Plain text file ProgramStatement.php Class Class source
  Plain text file ReturnStatement.php Class Class source
  Plain text file WhileStatement.php Class Class source

  Files folder image Files (87)  /  tests  
File Role Description
Files folder imageFeature (3 files)
Files folder imageUnit (2 directories)
  Plain text file Pest.php Example Example script
  Plain text file TestCase.php Class Class source

  Files folder image Files (87)  /  tests  /  Feature  
File Role Description
  Plain text file ArrayManipulationTest.php Example Example script
  Plain text file CalculatorTest.php Example Example script
  Plain text file FibonacciTest.php Example Example script

  Files folder image Files (87)  /  tests  /  Unit  
File Role Description
Files folder imageExpressions (7 files)
Files folder imageStatements (7 files)

  Files folder image Files (87)  /  tests  /  Unit  /  Expressions  
File Role Description
  Plain text file ArrayAccessExpressionTest.php Class Class source
  Plain text file ArrayLiteralExpressionTest.php Class Class source
  Plain text file BinaryExpressionTest.php Class Class source
  Plain text file FunctionCallExpressionTest.php Class Class source
  Plain text file IdentifierExpressionTest.php Class Class source
  Plain text file LiteralExpressionTest.php Class Class source
  Plain text file ObjectLiteralExpressionTest.php Class Class source

  Files folder image Files (87)  /  tests  /  Unit  /  Statements  
File Role Description
  Plain text file AssignmentStatementTest.php Class Class source
  Plain text file BlockStatementTest.php Class Class source
  Plain text file ForeachStatementTest.php Class Class source
  Plain text file FunctionDeclareStatementTest.php Class Class source
  Plain text file IfStatementTest.php Class Class source
  Plain text file ReturnStatementTest.php Class Class source
  Plain text file WhileStatementTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0