dougang6821 2016-08-08 06:53
浏览 86
已采纳

Symfony YAML格式转换

I have some service definition that looks like this:

MyService:
    class: Some\Class\Here
    factory:
        - SomeFactoryHere
        - method
    calls:
        - [add, ["@=service('AnotherService1').create(service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"]]

IMHO, this can be more readable if converted to something like this:

MyService:
    class: Some\Class\Here
    factory:
        - SomeFactoryHere
        - method
    calls:
        -
          add,
          "@=service('AnotherService1').create(
              service('AnotherService2'),
              service('AnotherService3'),
              service('AnotherService3')
          )"

But, this is not valid YAML (Symfony parser fails), and my question is how this config can be converted to something like above?

UPD#1

Look at Symfony YAML format conversion: "calls" with string params - important nuances are there.

  • 写回答

2条回答 默认 最新

  • drbfxb977777 2016-08-08 08:18
    关注

    The best way to break up your string

    "@=service('AnotherService1').create(service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"
    

    is by using a stripped folded block scalar. The limitation for this is that you cannot escape any special characters with backslashes, but those are not in your example (the reason you need "" around your scalar is because it starts with an @ which is a reserved character).

    Then you also have to correctly re-represent the structure that you have, as @flyx already indicated: the value for calls is a sequence, the first element of which is a list of the scalar add and sequence consisting of the aforementioned long scalar that you want to break up for readability:

    import yaml
    
    yaml_str = """\
    MyService:
        class: Some\Class\Here
        factory:
            - SomeFactoryHere
            - method
        calls:
            - - add
              - - >-
                  @=service('AnotherService1').create(
                  service('AnotherService2'),
                  service('AnotherService3'),
                  service('AnotherService3'))
    """
    
    data = yaml.safe_load(yaml_str)
    print(data)
    

    gives:

    "@=service('AnotherService1').create( service('AnotherService2'), service('AnotherService3'), service('AnotherService3'))"
    

    Please note that this gives an extra space between .create( and service(.

    The YAML parser that Symphony uses seems not to be able to parse the above (although it is correct). You can alternatively try:

    MyService:
        class: Some\Class\Here
        factory:
            - SomeFactoryHere
            - method
        calls:
            - 
              - add
              - 
                - >-
                  @=service('AnotherService1').create(
                  service('AnotherService2'),
                  service('AnotherService3'),
                  service('AnotherService3'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程