Development

/branches/1.3/lib/command/sfFormatter.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/command/sfFormatter.class.php

Revision 30008, 2.5 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed usage of shell_exec when the function is disabled (closes #8758)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfFormatter provides methods to format text to be displayed on a console.
13  *
14  * @package    symfony
15  * @subpackage command
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfFormatter
20 {
21   protected
22     $size = null;
23
24   function __construct($maxLineSize = null)
25   {
26     if (null === $maxLineSize)
27     {
28       if (function_exists('shell_exec'))
29       {
30         // this is tricky because "tput cols 2>&1" is not accurate
31         $maxLineSize = ctype_digit(trim(shell_exec('tput cols 2>&1'))) ? (integer) shell_exec('tput cols') : 78;
32       }
33       else
34       {
35         $maxLineSize = 78;
36       }
37     }
38
39     $this->size = $maxLineSize;
40   }
41
42   /**
43    * Sets a new style.
44    *
45    * @param string $name    The style name
46    * @param array  $options An array of options
47    */
48   public function setStyle($name, $options = array())
49   {
50   }
51
52   /**
53    * Formats a text according to the given parameters.
54    *
55    * @param  string $text         The test to style
56    * @param  mixed  $parameters   An array of parameters
57    *
58    * @return string The formatted text
59    */
60   public function format($text = '', $parameters = array())
61   {
62     return $text;
63   }
64
65   /**
66    * Formats a message within a section.
67    *
68    * @param string  $section  The section name
69    * @param string  $text     The text message
70    * @param integer $size     The maximum size allowed for a line
71    */
72   public function formatSection($section, $text, $size = null)
73   {
74     if (!$size)
75     {
76       $size = $this->size;
77     }
78
79     $section = sprintf('>> %-9s ', $section);
80
81     return $section.$this->excerpt($text, $size - strlen($section));
82   }
83
84   /**
85    * Truncates a line.
86    *
87    * @param string  $text The text
88    * @param integer $size The maximum size of the returned string
89    *
90    * @return string The truncated string
91    */
92   public function excerpt($text, $size = null)
93   {
94     if (!$size)
95     {
96       $size = $this->size;
97     }
98
99     if (strlen($text) < $size)
100     {
101       return $text;
102     }
103
104     $subsize = floor(($size - 3) / 2);
105
106     return substr($text, 0, $subsize).'...'.substr($text, -$subsize);
107   }
108
109   /**
110    * Sets the maximum line size.
111    *
112    * @param integer $size The maximum line size for a message
113    */
114   public function setMaxLineSize($size)
115   {
116     $this->size = $size;
117   }
118 }
119
Note: See TracBrowser for help on using the browser.