概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:44:32 | 92 次阅读 | 评论: 0 | 来源: 网络整理

When building a console application it may be useful to display tabular data:

+---------------+--------------------------+------------------+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
| 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie  |
+---------------+--------------------------+------------------+

To display a table, use Table, set the headers, set the rows and then render the table:

use SymfonyComponentConsoleHelperTable;

$table = new Table($output);
$table
    ->setHeaders(array('ISBN', 'Title', 'Author'))
    ->setRows(array(
        array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
        array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
        array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
        array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
    ))
;
$table->render();

You can add a table separator anywhere in the output by passing an instance of TableSeparator as a row:

use SymfonyComponentConsoleHelperTableSeparator;

$table->setRows(array(
    array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
    array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
    new TableSeparator(),
    array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
    array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
));
+---------------+--------------------------+------------------+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
+---------------+--------------------------+------------------+
| 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie  |
+---------------+--------------------------+------------------+

The table style can be changed to any built-in styles via setStyle():

// same as calling nothing
$table->setStyle('default');

// changes the default style to compact
$table->setStyle('compact');
$table->render();

This code results in:

ISBN          Title                    Author
99921-58-10-7 Divine Comedy            Dante Alighieri
9971-5-0210-0 A Tale of Two Cities     Charles Dickens
960-425-059-0 The Lord of the Rings    J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie

You can also set the style to borderless:

$table->setStyle('borderless');
$table->render();

which outputs:

=============== ========================== ==================
 ISBN            Title                      Author
=============== ========================== ==================
 99921-58-10-7   Divine Comedy              Dante Alighieri
 9971-5-0210-0   A Tale of Two Cities       Charles Dickens
 960-425-059-0   The Lord of the Rings      J. R. R. Tolkien
 80-902734-1-6   And Then There Were None   Agatha Christie
=============== ========================== ==================

If the built-in styles do not fit your need, define your own:

use SymfonyComponentConsoleHelperTableStyle;

// by default, this is based on the default style
$style = new TableStyle();

// customize the style
$style
    ->setHorizontalBorderChar('<fg=magenta>|</>')
    ->setVerticalBorderChar('<fg=magenta>-</>')
    ->setCrossingChar(' ')
;

// use the style for this table
$table->setStyle($style);

Here is a full list of things you can customize:

小技巧

You can also register a style globally:

// register the style under the colorful name
Table::setStyleDefinition('colorful', $style);

// use it for a table
$table->setStyle('colorful');

This method can also be used to override a built-in style.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务