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

发布于 2015-08-27 16:46:15 | 169 次阅读 | 评论: 0 | 来源: 网络整理

The ExpressionLanguage component provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not limited to, Booleans).

Installation

You can install the component in 2 different ways:

How can the Expression Engine Help Me?

The purpose of the component is to allow users to use expressions inside configuration for more complex logic. For some examples, the Symfony2 Framework uses expressions in security, for validation rules and in route matching.

Besides using the component in the framework itself, the ExpressionLanguage component is a perfect candidate for the foundation of a business rule engine. The idea is to let the webmaster of a website configure things in a dynamic way without using PHP and without introducing security problems:

# Get the special price if
user.getGroup() in ['good_customers', 'collaborator']

# Promote article to the homepage when
article.commentCount > 100 and article.category not in ["misc"]

# Send an alert when
product.stock < 15

Expressions can be seen as a very restricted PHP sandbox and are immune to external injections as you must explicitly declare which variables are available in an expression.

Usage

The ExpressionLanguage component can compile and evaluate expressions. Expressions are one-liners that often return a Boolean, which can be used by the code executing the expression in an if statement. A simple example of an expression is 1 + 2. You can also use more complicated expressions, such as someArray[3].someMethod('bar').

The component provides 2 ways to work with expressions:

  • evaluation: the expression is evaluated without being compiled to PHP;
  • compile: the expression is compiled to PHP, so it can be cached and evaluated.

The main class of the component is ExpressionLanguage:

use SymfonyComponentExpressionLanguageExpressionLanguage;

$language = new ExpressionLanguage();

echo $language->evaluate('1 + 2'); // displays 3

echo $language->compile('1 + 2'); // displays (1 + 2)

Expression Syntax

See The Expression Syntax to learn the syntax of the ExpressionLanguage component.

Passing in Variables

You can also pass variables into the expression, which can be of any valid PHP type (including objects):

use SymfonyComponentExpressionLanguageExpressionLanguage;

$language = new ExpressionLanguage();

class Apple
{
    public $variety;
}

$apple = new Apple();
$apple->variety = 'Honeycrisp';

echo $language->evaluate(
    'fruit.variety',
    array(
        'fruit' => $apple,
    )
);

This will print “Honeycrisp”. For more information, see the The Expression Syntax entry, especially Working with Objects and Working with Arrays.

Caching

The component provides some different caching strategies, read more about them in Caching Expressions Using Parser Caches.

最新网友评论  共有(0)条评论 发布评论 返回顶部

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