1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: App::uses("BaseShellHelper", "Console/Helper");
16:
17: 18: 19: 20:
21: class TableShellHelper extends BaseShellHelper {
22:
23: 24: 25: 26: 27:
28: protected $_defaultConfig = array(
29: 'headers' => true,
30: 'rowSeparator' => false,
31: 'headerStyle' => 'info',
32: );
33:
34: 35: 36: 37: 38: 39:
40: protected function _calculateWidths($rows) {
41: $widths = array();
42: foreach ($rows as $line) {
43: for ($i = 0, $len = count($line); $i < $len; $i++) {
44: $columnLength = mb_strlen($line[$i]);
45: if ($columnLength > (isset($widths[$i]) ? $widths[$i] : 0)) {
46: $widths[$i] = $columnLength;
47: }
48: }
49: }
50: return $widths;
51: }
52:
53: 54: 55: 56: 57: 58:
59: protected function _rowSeparator($widths) {
60: $out = '';
61: foreach ($widths as $column) {
62: $out .= '+' . str_repeat('-', $column + 2);
63: }
64: $out .= '+';
65: $this->_consoleOutput->write($out);
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: protected function _render($row, $widths, $options = array()) {
77: $out = '';
78: foreach ($row as $i => $column) {
79: $pad = $widths[$i] - mb_strlen($column);
80: if (!empty($options['style'])) {
81: $column = $this->_addStyle($column, $options['style']);
82: }
83: $out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
84: }
85: $out .= '|';
86: $this->_consoleOutput->write($out);
87: }
88:
89: 90: 91: 92: 93: 94:
95: public function output($rows) {
96: $config = $this->config();
97: $widths = $this->_calculateWidths($rows);
98: $this->_rowSeparator($widths);
99: if ($config['headers'] === true) {
100: $this->_render(array_shift($rows), $widths, array('style' => $config['headerStyle']));
101: $this->_rowSeparator($widths);
102: }
103: foreach ($rows as $line) {
104: $this->_render($line, $widths);
105: if ($config['rowSeparator'] === true) {
106: $this->_rowSeparator($widths);
107: }
108: }
109: if ($config['rowSeparator'] !== true) {
110: $this->_rowSeparator($widths);
111: }
112: }
113:
114: 115: 116: 117: 118: 119: 120:
121: protected function _addStyle($text, $style) {
122: return '<' . $style . '>' . $text . '</' . $style . '>';
123: }
124: }