kitano/pel-bundle

Bundle for PEL (PHP Expression Language) integration in Symfony

Installs: 1 052

Dependents: 1

Suggesters: 0

Security: 0

Stars: 4

Watchers: 6

Forks: 0

Open Issues: 0

Type:symfony-bundle

dev-master 2013-04-17 13:54 UTC

This package is not auto-updated.

Last update: 2024-04-22 10:20:50 UTC


README

This bundle integrates Kitano PHP Expression language into Symfony 2.

TODO

  • Add symfony specific expressions + compilers (if any)

Index

State

Unstable. Build Status

Installation

First, install the bundle package with composer:

$ php composer.phar require kitano/pel-bundle

Next, activate the bundle into app/AppKernel.php:

<?php

// ...
    public function registerBundles()
    {
        $bundles = array(
            //...
            new Kitano\PelBundle\KitanoPelBundle(),
        );

        // ...
    }

Usage

Basic Usage

The Expression compiler can be injected into your services:

<?php

namespace My\Service;

use Pel\Expression\ExpressionCompiler;

class MyService
{
    private $expressionCompiler;

    public function __construct(ExpressionCompiler $expressionCompiler)
    {
        $this->expressionCompiler = $expressionCompiler;
    }
}
<service id="my.service.my_service" class="My\Service\MyService">
    <argument type="service" id="kitano_pel.expression.compiler" />
</service>

And then you can start compiling expressions:

<?php

namespace My\Service;

use Pel\Expression\ExpressionCompiler;
use Pel\Expression\Expression;

class MyService
{
    // ...

    public function someMethod()
    {
        $evaluator = eval($this->expressionCompiler->compileExpression(new Expression("['foo', 'bar']")));
        $result = call_user_func($evaluator, array()));
        // $result => array('foo', 'bar')
    }
}

Registering custom expression compiler

After having created your expression compiler (see https://github.com/Kitano/php-expression#adding-a-custom-function-compiler), you need to register a new service into the Dependency Container with one of the following tag (depending on your expression type):

  • kitano_pel.type_compiler
  • kitano_pel.function_compiler

If we take the isNumber() function compiler example:

<service id="my.expression.compiler.is_number_compiler" class="My\Expression\Compiler\Func\IsNumberFunctionCompiler" public="false">
    <tag name="kitano_pel.function_compiler" />
</service>

Then, you can start compiling your custom expressions:

<?php

namespace My\Service;

use Pel\Expression\ExpressionCompiler;
use Pel\Expression\Expression;

class MyService
{
    // ...

    public function someMethod()
    {
        $evaluator = eval($this->expressionCompiler->compileExpression(new Expression("isNumber('1234')")));
        $result = call_user_func($evaluator, array()));
        // $result => bool(true)
    }
}

Testing

Install development dependencies

$ composer install --dev

Run the test suite

$ vendor/bin/phpunit

License

This bundle is under the MIT license. See the complete license in the bundle:

Resources/meta/LICENSE