This bundle integrates Kitano PHP Expression language into Symfony 2.
- Add symfony specific expressions + compilers (if any)
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(),
);
// ...
}
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')
}
}
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)
}
}
Install development dependencies
$ composer install --dev
Run the test suite
$ vendor/bin/phpunit
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE