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/Helper/FormatterHelper.php

91 lines
2.2 KiB
PHP
Raw Normal View History

2010-01-04 14:42:28 +00:00
<?php
namespace Symfony\Components\Console\Helper;
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 FormatterHelper extends Helper
2010-01-04 14:42:28 +00:00
{
/**
* Returns the helper's canonical name
*/
public function getName()
{
return 'formatter';
}
2010-01-04 14:42:28 +00: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
{
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
*/
public function formatBlock($messages, $style, $large = false)
2010-01-04 14:42:28 +00:00
{
if (!is_array($messages))
{
$messages = array($messages);
}
$len = 0;
$lines = array();
foreach ($messages as $message)
{
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
$len = max($this->strlen($message) + ($large ? 4 : 2), $len);
2010-01-04 14:42:28 +00:00
}
$messages = $large ? array(str_repeat(' ', $len)) : array();
foreach ($lines as $line)
{
$messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
2010-01-04 14:42:28 +00:00
}
if ($large)
{
$messages[] = str_repeat(' ', $len);
}
foreach ($messages as &$message)
{
$message = sprintf('<%s>%s</%s>', $style, $message, $style);
}
return implode("\n", $messages);
}
protected function strlen($string)
2010-01-04 14:42:28 +00:00
{
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
}
}