This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Components/Console/Output/Formatter.php

82 lines
2.0 KiB
PHP
Raw Normal View History

2010-01-04 14:42:28 +00:00
<?php
2010-01-09 11:57:15 +00:00
namespace Symfony\Components\Console\Output;
2010-01-04 14:42:28 +00:00
/*
* This file is part of the symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* The Formatter class provides helpers to format messages.
*
* @package symfony
* @subpackage console
2010-01-04 14:42:28 +00:00
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Formatter
{
/**
* 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
*/
static public function formatSection($section, $message, $style = 'info')
{
return sprintf("<%s>[%s]</%s> %s", $style, $section, $style, $message);
}
/**
* 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
*/
static public function formatBlock($messages, $style, $large = false)
{
if (!is_array($messages))
{
$messages = array($messages);
}
$len = 0;
$lines = array();
foreach ($messages as $message)
{
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
$len = max(static::strlen($message) + ($large ? 4 : 2), $len);
}
$messages = $large ? array(str_repeat(' ', $len)) : array();
foreach ($lines as $line)
{
$messages[] = $line.str_repeat(' ', $len - static::strlen($line));
}
if ($large)
{
$messages[] = str_repeat(' ', $len);
}
foreach ($messages as &$message)
{
$message = sprintf('<%s>%s</%s>', $style, $message, $style);
}
return implode("\n", $messages);
}
static protected function strlen($string)
{
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
}
}