2010-01-04 14:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-04 14:42:28 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-01-04 14:42:28 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Console\Helper;
|
|
|
|
|
2010-01-04 14:42:28 +00:00
|
|
|
/**
|
|
|
|
* The Formatter class provides helpers to format messages.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-01-04 14:42:28 +00:00
|
|
|
*/
|
2010-01-11 11:24:08 +00:00
|
|
|
class FormatterHelper extends Helper
|
2010-01-04 14:42:28 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Formats a message within a section.
|
|
|
|
*
|
|
|
|
* @param string $section The section name
|
|
|
|
* @param string $message The message
|
|
|
|
* @param string $style The style to apply to the section
|
|
|
|
*/
|
|
|
|
public function formatSection($section, $message, $style = 'info')
|
2010-01-04 14:42:28 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
|
2010-01-04 14:42:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Formats a message as a block of text.
|
|
|
|
*
|
|
|
|
* @param string|array $messages The message to write in the block
|
|
|
|
* @param string $style The style to apply to the whole block
|
|
|
|
* @param Boolean $large Whether to return a large block
|
|
|
|
*
|
|
|
|
* @return string The formatter message
|
|
|
|
*/
|
|
|
|
public function formatBlock($messages, $style, $large = false)
|
2010-01-04 14:42:28 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($messages)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$messages = array($messages);
|
|
|
|
}
|
2010-01-04 14:42:28 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$len = 0;
|
|
|
|
$lines = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($messages as $message) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
|
|
|
|
$len = max($this->strlen($message) + ($large ? 4 : 2), $len);
|
|
|
|
}
|
|
|
|
|
|
|
|
$messages = $large ? array(str_repeat(' ', $len)) : array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($lines as $line) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($large) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$messages[] = str_repeat(' ', $len);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($messages as &$message) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$message = sprintf('<%s>%s</%s>', $style, $message, $style);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode("\n", $messages);
|
2010-01-04 14:42:28 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-07 00:34:24 +00:00
|
|
|
/**
|
|
|
|
* Returns the length of a string, uses mb_strlen if it is available.
|
|
|
|
*
|
|
|
|
* @param string $string The string to check its length
|
|
|
|
*
|
|
|
|
* @return integer The length of the string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function strlen($string)
|
2010-01-04 14:42:28 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
|
2010-01-04 14:42:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the helper's canonical name
|
2011-02-07 00:34:24 +00:00
|
|
|
*
|
|
|
|
* @return string The canonical name of the helper
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getName()
|
2010-01-04 14:42:28 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return 'formatter';
|
2010-01-04 14:42:28 +00:00
|
|
|
}
|
2010-01-11 13:46:11 +00:00
|
|
|
}
|