Symfony2在YAML配置中使用PHP类常量?

时间:2023-01-15 09:27:48

I know this is probably not possible, but is there a clean way to use a PHP class constant within a YAML config/services/etc. file for Symfony2?

我知道这可能是不可能的,但有一种干净的方法在YAML config / services / etc中使用PHP类常量。 Symfony2的文件?

For example, if I have this:

例如,如果我有这个:

namespace My\Bundle\DependencyInjection;

class MyClass
{
    const MY_CONST = 'cookies';
}

Is something like this possible (in a .yml file):

是这样的(在.yml文件中):

services:
    my_service:
        class: Some\Class
        arguments:
            - %My\Bundle\DependencyInjection\MyClass::MY_CONST%

That'd go a long way in helping maintain consistency between the two.

这对于帮助保持两者之间的一致性有很大帮助。

6 个解决方案

#1


15  

In versions before Symfony 3.2, injecting PHP-constants only works with XML:

在Symfony 3.2之前的版本中,注入PHP常量仅适用于XML:

<parameter key="my_service.my_const" type="constant">My\Bundle\DependencyInjection\MyClass::MY_CONST</parameter>

If you want to keep yor yml files, you could just import the xml-file into your services.yml. Mixing config styles might be a bit ugly, but as far as I know this is the only way to do it.

如果要保留yml文件,可以将xml文件导入services.yml。混合配置样式可能有点难看,但据我所知,这是唯一的方法。

If this doesn't work for you, my comment to your question applies.

如果这不适合您,我对您的问题的评论适用。

#2


19  

For Symfony >=2.4 you can use expression language

对于Symfony> = 2.4,您可以使用表达式语言

Example:

例:

'@=constant("Symfony\\Bridge\\Monolog\\Logger::INFO")'

#3


16  

As of Symfony 3.2, it's also possible to use PHP constants in YAML files using a special !php/const: syntax:

从Symfony 3.2开始,也可以使用特殊的!php / const:语法在YAML文件中使用PHP常量:

parameters:
    bar: !php/const:PHP_INT_MAX

See the Symfony blog post for more details.

有关更多详细信息,请参阅Symfony博客文章。


Please note: In Symfony 4 the syntax was slightly changed. You have to use a space instead of a double colon, for example:

请注意:在Symfony 4中,语法略有改变。您必须使用空格而不是双冒号,例如:

parameters:
    bar: !php/const PHP_INT_MAX

Otherwise, you will get a FileLoaderLoadException with the message "Notice: Uninitialized string offset".

否则,您将收到FileLoaderLoadException,并显示消息“Notice:Uninitialized string offset”。

#4


1  

It should be possible to insert argument as plain 'text' in config and inside class __construct($constant1) then request constant through variable by constant($constant1) function.

应该可以在config和class __construct($ constant1)中插入参数作为普通'text',然后通过常量($ constant1)函数通过变量请求常量。

But I am not sure about global constants here, because they may not be defined at time of using, but it shouldn't be a problem for class constants with whole namespace, because namespace locator is already defined at moment of calling the class.

但是我不确定这里的全局常量,因为它们可能在使用时没有被定义,但它对于具有整个命名空间的类常量应该不是问题,因为在调用类时已经定义了命名空间定位器。

Example config:

示例配置:

services:
    my_service:
        class: Some\Class
    arguments:
        - 'My\Bundle\DependencyInjection\MyClass::MY_CONST'

and example class method:

和示例类方法:

public function __construct($arg1)
{
    $myRequiredConstantValue = constant($arg1);
}

#5


0  

This is possible, but only when using eval'd code, which is generally frowned upon.

这是可能的,但仅限于使用eval'd代码时,这通常是不受欢迎的。

Given a class constant Permission::ACCESS_NONE e.g. you could do something like this in the yaml file:

给定一个类常量Permission :: ACCESS_NONE,例如你可以在yaml文件中做这样的事情:

permission: ACCESS_NONE

and the following in the code where you parse the yaml:

以及解析yaml的代码中的以下内容:

eval("return \The\Complete\Namespace\To\Permission::".$context['permission'].";");

eval(“return \ The \ Complete \ Namespace \ To \ Permission ::”。$ context ['permission']。“;”);

This is of course butt-ugly code but it does work.

这当然是非常丑陋的代码,但确实有效。

#6


-5  

This should work

这应该工作

arguments:
 - <?php echo My\Bundle\DependencyInjection\MyClass::MY_CONST."\n" ?>

#1


15  

In versions before Symfony 3.2, injecting PHP-constants only works with XML:

在Symfony 3.2之前的版本中,注入PHP常量仅适用于XML:

<parameter key="my_service.my_const" type="constant">My\Bundle\DependencyInjection\MyClass::MY_CONST</parameter>

If you want to keep yor yml files, you could just import the xml-file into your services.yml. Mixing config styles might be a bit ugly, but as far as I know this is the only way to do it.

如果要保留yml文件,可以将xml文件导入services.yml。混合配置样式可能有点难看,但据我所知,这是唯一的方法。

If this doesn't work for you, my comment to your question applies.

如果这不适合您,我对您的问题的评论适用。

#2


19  

For Symfony >=2.4 you can use expression language

对于Symfony> = 2.4,您可以使用表达式语言

Example:

例:

'@=constant("Symfony\\Bridge\\Monolog\\Logger::INFO")'

#3


16  

As of Symfony 3.2, it's also possible to use PHP constants in YAML files using a special !php/const: syntax:

从Symfony 3.2开始,也可以使用特殊的!php / const:语法在YAML文件中使用PHP常量:

parameters:
    bar: !php/const:PHP_INT_MAX

See the Symfony blog post for more details.

有关更多详细信息,请参阅Symfony博客文章。


Please note: In Symfony 4 the syntax was slightly changed. You have to use a space instead of a double colon, for example:

请注意:在Symfony 4中,语法略有改变。您必须使用空格而不是双冒号,例如:

parameters:
    bar: !php/const PHP_INT_MAX

Otherwise, you will get a FileLoaderLoadException with the message "Notice: Uninitialized string offset".

否则,您将收到FileLoaderLoadException,并显示消息“Notice:Uninitialized string offset”。

#4


1  

It should be possible to insert argument as plain 'text' in config and inside class __construct($constant1) then request constant through variable by constant($constant1) function.

应该可以在config和class __construct($ constant1)中插入参数作为普通'text',然后通过常量($ constant1)函数通过变量请求常量。

But I am not sure about global constants here, because they may not be defined at time of using, but it shouldn't be a problem for class constants with whole namespace, because namespace locator is already defined at moment of calling the class.

但是我不确定这里的全局常量,因为它们可能在使用时没有被定义,但它对于具有整个命名空间的类常量应该不是问题,因为在调用类时已经定义了命名空间定位器。

Example config:

示例配置:

services:
    my_service:
        class: Some\Class
    arguments:
        - 'My\Bundle\DependencyInjection\MyClass::MY_CONST'

and example class method:

和示例类方法:

public function __construct($arg1)
{
    $myRequiredConstantValue = constant($arg1);
}

#5


0  

This is possible, but only when using eval'd code, which is generally frowned upon.

这是可能的,但仅限于使用eval'd代码时,这通常是不受欢迎的。

Given a class constant Permission::ACCESS_NONE e.g. you could do something like this in the yaml file:

给定一个类常量Permission :: ACCESS_NONE,例如你可以在yaml文件中做这样的事情:

permission: ACCESS_NONE

and the following in the code where you parse the yaml:

以及解析yaml的代码中的以下内容:

eval("return \The\Complete\Namespace\To\Permission::".$context['permission'].";");

eval(“return \ The \ Complete \ Namespace \ To \ Permission ::”。$ context ['permission']。“;”);

This is of course butt-ugly code but it does work.

这当然是非常丑陋的代码,但确实有效。

#6


-5  

This should work

这应该工作

arguments:
 - <?php echo My\Bundle\DependencyInjection\MyClass::MY_CONST."\n" ?>