$_defaultConfig
$_defaultConfig : array
Default config for this helper.
Create a visually pleasing ASCII art table from 2 dimensional array data.
Console Helpers allow you to package up reusable blocks of Console output logic. For example creating tables, progress bars or ascii art.
$_io : \Cake\Console\ConsoleIo
ConsoleIo instance.
__construct(\Cake\Console\ConsoleIo $io, array $config = array())
Constructor.
\Cake\Console\ConsoleIo | $io | The ConsoleIo instance to use. |
array | $config | The settings for this helper. |
setConfig(string|array $key, mixed|null $value = null, boolean $merge = true) : $this
Sets the config.
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
string|array | $key | The key to set, or a complete array of configs. |
mixed|null | $value | The value to set. |
boolean | $merge | Whether to recursively merge or overwrite existing config, defaults to true. |
When trying to set a key that is invalid.
getConfig(string|null $key = null, mixed $default = null) : mixed
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
string|null | $key | The key to get or null for the whole config. |
mixed | $default | The return value when the key does not exist. |
Config value being read.
config(string|array|null $key = null, mixed|null $value = null, boolean $merge = true) : mixed
Gets/Sets the config.
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
string|array|null | $key | The key to get/set, or a complete array of configs. |
mixed|null | $value | The value to set. |
boolean | $merge | Whether to recursively merge or overwrite existing config, defaults to true. |
When trying to set a key that is invalid.
Config value being read, or the object itself on write operations.
configShallow(string|array $key, mixed|null $value = null) : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
string|array | $key | The key to set, or a complete array of configs. |
mixed|null | $value | The value to set. |
None found |
_configRead(string|null $key) : mixed
Reads a config key.
string|null | $key | Key to read. |
None found |
_configWrite(string|array $key, mixed $value, boolean|string $merge = false) : void
Writes a config key.
string|array | $key | Key to write to. |
mixed | $value | Value to write. |
boolean|string | $merge | True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false. |
if attempting to clobber existing config
None found |
_configDelete(string $key) : void
Deletes a single config key.
string | $key | Key to delete. |
if attempting to clobber existing config
None found |
_calculateWidths(array $rows) : array
Calculate the column widths
array | $rows | The rows on which the columns width will be calculated on. |
None found |
_rowSeparator(array $widths) : void
Output a row separator.
array | $widths | The widths of each column to output. |
None found |
_render(array $row, array $widths, array $options = array()) : void
Output a row.
array | $row | The row to output. |
array | $widths | The widths of each column to output. |
array | $options | Options to be passed. |
None found |
_addStyle(string $text, string $style) : string
Add style tags
string | $text | The text to be surrounded |
string | $style | The style to be applied |
None found |
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 3.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Shell\Helper;
use Cake\Console\Helper;
/**
* Create a visually pleasing ASCII art table
* from 2 dimensional array data.
*/
class TableHelper extends Helper
{
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [
'headers' => true,
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Calculate the column widths
*
* @param array $rows The rows on which the columns width will be calculated on.
* @return array
*/
protected function _calculateWidths($rows)
{
$widths = [];
foreach ($rows as $line) {
foreach (array_values($line) as $k => $v) {
$columnLength = mb_strwidth($v);
if ($columnLength >= (isset($widths[$k]) ? $widths[$k] : 0)) {
$widths[$k] = $columnLength;
}
}
}
return $widths;
}
/**
* Output a row separator.
*
* @param array $widths The widths of each column to output.
* @return void
*/
protected function _rowSeparator($widths)
{
$out = '';
foreach ($widths as $column) {
$out .= '+' . str_repeat('-', $column + 2);
}
$out .= '+';
$this->_io->out($out);
}
/**
* Output a row.
*
* @param array $row The row to output.
* @param array $widths The widths of each column to output.
* @param array $options Options to be passed.
* @return void
*/
protected function _render(array $row, $widths, $options = [])
{
if (count($row) === 0) {
return;
}
$out = '';
foreach (array_values($row) as $i => $column) {
$pad = $widths[$i] - mb_strwidth($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
$out .= '|';
$this->_io->out($out);
}
/**
* Output a table.
*
* Data will be output based on the order of the values
* in the array. The keys will not be used to align data.
*
* @param array $rows The data to render out.
* @return void
*/
public function output($rows)
{
if (!is_array($rows) || count($rows) === 0) {
return;
}
$config = $this->getConfig();
$widths = $this->_calculateWidths($rows);
$this->_rowSeparator($widths);
if ($config['headers'] === true) {
$this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
$this->_rowSeparator($widths);
}
if (!$rows) {
return;
}
foreach ($rows as $line) {
$this->_render($line, $widths);
if ($config['rowSeparator'] === true) {
$this->_rowSeparator($widths);
}
}
if ($config['rowSeparator'] !== true) {
$this->_rowSeparator($widths);
}
}
/**
* Add style tags
*
* @param string $text The text to be surrounded
* @param string $style The style to be applied
* @return string
*/
protected function _addStyle($text, $style)
{
return '<' . $style . '>' . $text . '</' . $style . '>';
}
}