从同一个 yaml 文件中解析自定义标签 (!tag) 和常量 (!php/const)

卢卡·朱拉斯

yaml 文件:

- name: hero-block
  title: Hero Block
  description: A Hero Block Section block
  category: landing-pages
  icon: welcome-view-site
  example:
    attributes:
      mode: preview
      data:
        headline: !php/const LIPSUM
        subheadline: !php/const LIPSUM
        custom: !my_tag echo 1

php文件

function yaml_parse($relname) {
  return Yaml::parse(
    file_get_contents(get_template_directory() . '/flat_data/' .$relname. '.yaml'),
    Yaml::PARSE_CONSTANT); // PARSE_CUSTOM_TAGS
}

用于解析的标志是 Int 类型。想不出任何方法来实现所需的输出。非常感谢任何提示。

朱利安·B。

TLDR:使用Yaml::PARSE_CONSTANT + Yaml::PARSE_CUSTOM_TAGS

此函数&$flags参数值使用按位运算符。

更多信息在这里:

https://www.php.net/manual/en/language.operators.bitwise.php https://www.w3resource.com/php/operators/bitwise-operators.php

所以这段代码:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Yaml\Yaml;

class DemoController extends AbstractController
{
    public const LIPSUM = 'Lorem';

    #[Route('/')]
    public function index()
    {
        $yaml   = <<<EOF
- name: hero-block
  title: Hero Block
  description: A Hero Block Section block
  category: landing-pages
  icon: welcome-view-site
  example:
    attributes:
      mode: preview
      data:
        headline: !php/const App\Controller\DemoController::LIPSUM
        subheadline: !php/const App\Controller\DemoController::LIPSUM
        custom: !my_tag echo 1
EOF;
        $parsed = Yaml::parse($yaml, Yaml::PARSE_CONSTANT + Yaml::PARSE_CUSTOM_TAGS);

        $tagName  = $parsed[0]['example']['attributes']['data']['custom']->getTag();
        $tagValue = $parsed[0]['example']['attributes']['data']['custom']->getValue();

        return $this->json(['tagName' => $tagName, 'tagValue' => $tagValue, 'parsedYaml' => $parsed]);
    }
}

将返回这个:

{
    "tagName": "my_tag",
    "tagValue": "echo 1",
    "parsedYaml": [
        {
            "name": "hero-block",
            "title": "Hero Block",
            "description": "A Hero Block Section block",
            "category": "landing-pages",
            "icon": "welcome-view-site",
            "example": {
                "attributes": {
                    "mode": "preview",
                    "data": {
                        "headline": "Lorem",
                        "subheadline": "Lorem",
                        "custom": {
                            "tag": "my_tag",
                            "value": "echo 1"
                        }
                    }
                }
            }
        }
    ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

后端和前端在同一个 kubernetes yaml 文件中?

在同一个 yaml 文件中访问 yaml 映射中的项目

FileInputStream 和 FileOutputStream:读写同一个文件

从同一个YAML定义文件创建多个Pod

可以在同一个类中定义普通构造函数和常量构造函数吗?

收益和收益在同一个函数中

在同一个操作中聚合和连接同一个表 - python

如何在同一个 RestTemplate 中使用自定义 ResponseErrorHandler 和 ClientHttpRequestFactory?

一个列表和一个表单放到同一个django模板中

在同一个应用程序中为同一个模型和同一个存储库使用多个 mongo 数据库

如何在同一个 Razor 页面中使用同一个视图组件和 Javascript 文件的多个实例?

Java:FileOutputStream和FileInputStream一起在同一个文件上

如何在同一个乳胶文件中同时包含Agda和Isabelle代码?

在同一个PHP文件中同时执行INSERT和SELECT

P标签和H2标签在同一个div内联

混合Ruby和bash命令-mv返回“ x和y是同一个文件”

在 WPF 中的同一个按钮中绑定命令和 DragEnter 和 Drop 事件

从同一个.proto文件生成Python和Go代码-导入问题

在同一个文本文件上使用StreamReader和Writer

如何使用 lotusscript 读取和写入同一个 csv 文件?

COUNT 和 SELECT 到 mySQL 中的同一个查询中

在同一个 Mysql 查询中的 MySql GROUP BY 和 SUM() 中

SQL 选择和计数在同一个查询中

如何让 alert 和 location.href 在同一个脚本中工作

在同一个类中具有静态和非静态方法是否错误?

wx.Frame 和线程在同一个类中

GET 和 POST 在同一个 Flask 方法中

同时在 IDEA 和 PyCharm 中为同一个项目工作

在同一个表中打印关系和相关性的重要性

在同一个应用程序中同时包含crashlytics和Google Analytics(分析)