symfony - 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();
它对我来说看起来很丑......有人有其他解决方案吗?
解决方案
编辑:你正在做的事情实际上很好。我想一想,绝对没问题。无论如何,我会在下面留下我的评论。
我不喜欢您尝试将完整配置注入服务。当您说它是可变的时,为什么还要打扰..但是为了争论,假设您必须....在这一点上,您尝试做的事情可能是创建一个 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);
}
}
推荐阅读
- docker - 如何使用 VSCode 的远程开发扩展在容器外的 Docker 容器中打开文件
- swift - UICollectionView 列表中的折叠部分
- database - 如何克隆 Grakn 密钥空间
- javascript - 角度路由:路由总是重定向到 404 页面
- r - 无论操作系统如何,如何使 CRAN 包只下载一次数据?
- python - 在python中向JSON结构添加/附加键
- javascript - React,功能组件使用定义之前使用的变量,循环依赖
- nim-lang - 如何从 Nim 的异常中获取堆栈跟踪?
- vert.x - 一旦控制权传递给blockingHandler,vertx.eventloop线程会发生什么?
- java - 415 Unsupported Media Exception 即使在为 SOAP 1.2 调用设置 application/soap+xml 之后