如何在 Symfony 中注册表达式语言 [英] How to register a Expression Language in Symfony

查看:24
本文介绍了如何在 Symfony 中注册表达式语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个具有安全功能的提供程序.按照 文档,我创建了自己的 ExpressionLanguage类并注册提供者.

I already created a provider with a security function. Following the doc, i created my own ExpressionLanguage class and registered the provider.

namespace AppBundle\ExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface;

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 AppExpressionLanguageProvider());

        parent::__construct($parser, $providers);
    }
}

我正在使用与 在文档中.但是现在,我不知道如何注册要在我的 Symfony 项目中加载的 ExpressionLanguage 类.

I'm using the same function lowercase that is in the doc. But now, i have no ideia how to register the ExpressionLanguage class to be loaded in my Symfony project.

每次尝试使用注释中的自定义函数加载页面时,我都会收到此错误:

I get this error every time i try to load a page with the custom function in the annotation:

位置 26 附近不存在函数小写".

The function "lowercase" does not exist around position 26.

我使用的是 Symfony 2.7.5.

推荐答案

标签 security.expression_language_provider 仅用于向 symfonys 安全组件中使用的表达式语言添加语言提供程序,或者更具体地说,在ExpressionVoter.

The tag security.expression_language_provider is only used to add language providers to the expression language used in symfonys security component, or more specifically in the ExpressionVoter.

FrameworkBundle 的@Security-Annotation 使用表达式语言的不同实例,它不知道您创建的语言提供程序.

The @Security-Annotation of the FrameworkBundle uses a different instance of the expression language, which has no knowledge of the language providers you created.

为了能够在@Security-Annotation 中使用自定义语言提供程序,我使用以下编译器传递解决了这个问题:

To be able to use custom language providers in the @Security-Annotation, I solved this using the following compiler pass:

<?php

namespace ApiBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

/**
 * This compiler pass adds language providers tagged
 * with security.expression_language_provider to the
 * expression language used in the framework extra bundle.
 *
 * This allows to use custom expression language functions
 * in the @Security-Annotation.
 *
 * Symfony\Bundle\FrameworkBundle\DependencyInection\Compiler\AddExpressionLanguageProvidersPass
 * does the same, but only for the security.expression_language
 * which is used in the ExpressionVoter.
 */
class AddExpressionLanguageProvidersPass implements CompilerPassInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        if ($container->has('sensio_framework_extra.security.expression_language')) {
            $definition = $container->findDefinition('sensio_framework_extra.security.expression_language');
            foreach ($container->findTaggedServiceIds('security.expression_language_provider') as $id => $attributes) {
                $definition->addMethodCall('registerProvider', array(new Reference($id)));
            }
        }
    }
}

这样,ExpressionVoter 和 FrameworkBundle 使用的表达式语言都使用相同的语言提供程序进行配置.

This way, the expression languages used by the ExpressionVoter and FrameworkBundle are both configured using the same language providers.

这篇关于如何在 Symfony 中注册表达式语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆