kitano / pel-bundle
Symfony中PEL (PHP表达式语言)集成的组件包
dev-master
2013-04-17 13:54 UTC
Requires
- kitano/php-expression: dev-master
- symfony/framework-bundle: >=2.0
Requires (Dev)
- phpunit/phpunit: 3.*
This package is not auto-updated.
Last update: 2024-09-23 12:23:35 UTC
README
此组件包将Kitano PHP表达式语言集成到Symfony 2。
待办事项
- 添加symfony特定表达式和编译器(如有)
索引
状态
安装
首先,使用composer安装组件包
$ php composer.phar require kitano/pel-bundle
接下来,在app/AppKernel.php
中激活组件包
<?php // ... public function registerBundles() { $bundles = array( //... new Kitano\PelBundle\KitanoPelBundle(), ); // ... }
使用
基本使用
可以将Expression
编译器注入到您的服务中
<?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>
然后您就可以开始编译表达式了
<?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') } }
注册自定义表达式编译器
在创建您的表达式编译器(见https://github.com/Kitano/php-expression#adding-a-custom-function-compiler)后,您需要使用以下标签之一将新服务注册到依赖容器中(根据您的表达式类型而定)
kitano_pel.type_compiler
kitano_pel.function_compiler
以isNumber()
函数编译器为例
<service id="my.expression.compiler.is_number_compiler" class="My\Expression\Compiler\Func\IsNumberFunctionCompiler" public="false"> <tag name="kitano_pel.function_compiler" /> </service>
然后,您就可以开始编译您的自定义表达式了
<?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) } }
测试
安装开发依赖
$ composer install --dev
运行测试套件
$ vendor/bin/phpunit
许可
此组件包受MIT许可证的约束。请在组件包中查看完整的许可证
Resources/meta/LICENSE