一个最不为人知的Symfony命令行功能是,它可以提供可选的输出格式,包括XML、JSON、Mrakdown等等。举例来说,要得到JSON格式的Twig检查(lint)结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
$  ./bin/console lint:twig app/Resources/ --format=json
 
[
    {
        "file": "app/Resources/views/base.html.twig",
        "valid": true
    },
    {
        "file": "app/Resources/views/blog/index.html.twig",
        "valid": true
    },
    ...
]

你也可以得到XML格式的任意路由信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$  ./bin/console debug:router homepage --format=xml
 
<?xml version="1.0" encoding="UTF-8"?>
<route name="homepage" class="Symfony\Component\Routing\Route">
  <path regex="#^/(?P&lt;_locale&gt;en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl)?$#s">/{_locale}</path>
  <defaults>
    <default key="_controller">FrameworkBundle:Template:template</default>
    <default key="template">default/homepage.html.twig</default>
    <default key="_locale">en</default>
  </defaults>
  <requirements>
    <requirement key="_locale">en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl</requirement>
  </requirements>
  <options>
    <option key="compiler_class">Symfony\Component\Routing\RouteCompiler</option>
  </options>
</route>

在Symfony 3.3中我们改进了上述命令的某些descriptors。例如,当使用JSON来描述程序时,现在你将得到程序的完整名称和版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    "application": {
        "name": "My Symfony application",
        "version": "v1.0"
    },
    "commands": [
        {
            "name": "help",
            "usage": [
                "help [--format FORMAT] [--raw] [--] [<command_name>]"
            ]
        },
        ...
    ]
}

此外,当使用Markdown作为output格式时,内容的格式化将更加完美。

之前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
UNKNOWN
=======
 
* help
* list
 
help
----
 
* Description: Displays help for a command
* Usage:
 
  * `help [--format FORMAT] [--raw] [--] [<command_name>]`
 
...

之后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Console Tool
============
 
* `help`
* `list`
 
`help`
------
 
Displays help for a command
 
### Usage
 
* `help [--format FORMAT] [--raw] [--] [<command_name>]`
 
...