首页 > 解决方案 > Symfony yaml 配置:执行纯 php 代码

问题描述

Symfony 框架 4.4

这是我的config/services.yaml文件:

parameters:
    ...
    app.dogstatsd.config:
        host: 'some_host_name.domain.com'
        global_tags:
            node: 'hostname'
            env: '%env(APP_ENV)%'

services:
    ...
    DataDog\DogStatsd:
        arguments: ['%app.dogstatsd.config%']


我需要node等于 PHP gethostname() 函数的结果。

在服务器端::

$ hostname
homestead

那么,如何从 yaml-config 中获取主机名值?

我找到了解决方案:

    env(HOSTNAME): '../config/.runtime-evaluated.php'
    app.dogstatsd.config:
        host: 'some_host_name.domain.com'
        global_tags:
            node: '%env(require:HOSTNAME)%'
            env: '%env(APP_ENV)%'

在哪里

$ cat config/.runtime-evaluated.php'
<?php

declare(strict_types=1);

return gethostname();

它对我来说看起来很丑......有人有其他解决方案吗?

标签: symfony

解决方案


编辑:你正在做的事情实际上很好。我想一想,绝对没问题。无论如何,我会在下面留下我的评论。

我不喜欢您尝试将完整配置注入服务。当您说它是可变的时,为什么还要打扰..但是为了争论,假设您必须....在这一点上,您尝试做的事情可能是创建一个 DependencyInjection Extension 允许您扩展另一个捆绑配置选项的值。

也许在这里看看:https ://symfony.com/doc/4.1/bundles/prepend_extension.html

例如,您可以做的是创建一个文件.. 选择您自己的命名和首选捆绑包以将其添加到....

// src/AppDDoggerBundle/DI/DDWhateverYouWantToCallMeExtension.php

public function prepend(ContainerBundle $container){
   // Get existing DD Bundle Config.. 
   // (I don't know what the Bundle Alias is called)
   $container->loadFromExtension('ddstat', [
      'key' => 'value'
   ]);
   
}

使用帮助服务来访问 DDStatt 不是更有意义吗?

例如...

<?php

/**
 * Created by Helpful Stackoverflow User.
 * User: layke
 * Date: 02/12/20
 * Time: 22:17.
 */

namespace App\DataDogBundle\Service;

    use DataDog\DogStatsd;

    /**
     * Class DDStatter.
     *
     * @method timing(string $stat, float $time, float $sampleRate, array | string $tags)
     * @method microtiming(string $stat, float $time, float $sampleRate, array | string $tags)
     * @method gauge(string $stat, float $value, float $sampleRate, array | string $tags)
     * @method histogram(string $stat, float $value, float $sampleRate, array | string $tags)
     * @method distribution(string $stat, float $value, float $sampleRate, array | string $tags)
     * @method set(string $stat, float $value, float $sampleRate, array | string $tags)
     * @method increment(string | array $stat, float $sampleRate, array | string $tags, float $value)
     * @method decrement(string | array $stat, float $sampleRate, array | string $tags, float $value)
     * @method updateStats(string | array $stat, int $delta, float $sampleRate, array | string $tags)
     * @method serialize_tags(array | string $tags)
     * @method normalize_tags(mixed $tags)
     * @method send(array $data, float $sampleRate, array | string $tags)
     */
    class DDStatter
    {
        private $statd;

        /**
         * DDStatter constructor.
         */
        public function __construct(?string $dataDogApiKey, ?string $dataDogAppKey)
        {
            $this->statd = new DogStatsd([
                'api_key' => $dataDogApiKey,
                'app_key' => $dataDogAppKey,
            ]);
        }

        public function __call($method, $args)
        {
            return call_user_func_array([$this->statd, $method], $args);
        }
    }

推荐阅读