概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:52:24 | 196 次阅读 | 评论: 0 | 来源: 网络整理

The ExpressionLanguage can be extended by adding custom functions. For instance, in the Symfony Framework, the security has custom functions to check the user’s role.

注解

If you want to learn how to use functions in an expression, read “Working with Functions”.

Registering Functions

Functions are registered on each specific ExpressionLanguage instance. That means the functions can be used in any expression executed by that instance.

To register a function, use register(). This method has 3 arguments:

  • name - The name of the function in an expression;
  • compiler - A function executed when compiling an expression using the function;
  • evaluator - A function executed when the expression is evaluated.
use SymfonyComponentExpressionLanguageExpressionLanguage;

$language = new ExpressionLanguage();
$language->register('lowercase', function ($str) {
    return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
}, function ($arguments, $str) {
    if (!is_string($str)) {
        return $str;
    }

    return strtolower($str);
});

echo $language->evaluate('lowercase("HELLO")');

This will print hello. Both the compiler and evaluator are passed an arguments variable as their first argument, which is equal to the second argument to evaluate() or compile() (e.g. the “values” when evaluating or the “names” if compiling).

Using Expression Providers

2.6 新版功能: Expression providers were introduced in Symfony 2.6.

When you use the ExpressionLanguage class in your library, you often want to add custom functions. To do so, you can create a new expression provider by creating a class that implements ExpressionFunctionProviderInterface.

This interface requires one method: getFunctions(), which returns an array of expression functions (instances of ExpressionFunction) to register.

use SymfonyComponentExpressionLanguageExpressionFunction;
use SymfonyComponentExpressionLanguageExpressionFunctionProviderInterface;

class StringExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
    public function getFunctions()
    {
        return array(
            new ExpressionFunction('lowercase', function ($str) {
                return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
            }, function ($arguments, $str) {
                if (!is_string($str)) {
                    return $str;
                }

                return strtolower($str);
            }),
        );
    }
}

You can register providers using registerProvider() or by using the second argument of the constructor:

use SymfonyComponentExpressionLanguageExpressionLanguage;

// using the constructor
$language = new ExpressionLanguage(null, array(
    new StringExpressionLanguageProvider(),
    // ...
));

// using registerProvider()
$language->registerProvider(new StringExpressionLanguageProvider());

小技巧

It is recommended to create your own ExpressionLanguage class in your library. Now you can add the extension by overriding the constructor:

use SymfonyComponentExpressionLanguageExpressionLanguage as BaseExpressionLanguage;
use SymfonyComponentExpressionLanguageParserCacheParserCacheInterface;

class ExpressionLanguage extends BaseExpressionLanguage
{
    public function __construct(ParserCacheInterface $parser = null, array $providers = array())
    {
        // prepend the default provider to let users override it easily
        array_unshift($providers, new StringExpressionLanguageProvider());

        parent::__construct($parser, $providers);
    }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务