@@ -99,7 +99,7 @@ public function setDispatcher(EventDispatcherInterface $dispatcher)
99
99
* @param InputInterface $input An Input instance
100
100
* @param OutputInterface $output An Output instance
101
101
*
102
- * @return integer 0 if everything went fine, or an error code
102
+ * @return int 0 if everything went fine, or an error code
103
103
*
104
104
* @throws \Exception When doRun returns Exception
105
105
*
@@ -159,7 +159,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
159
159
* @param InputInterface $input An Input instance
160
160
* @param OutputInterface $output An Output instance
161
161
*
162
- * @return integer 0 if everything went fine, or an error code
162
+ * @return int 0 if everything went fine, or an error code
163
163
*/
164
164
public function doRun (InputInterface $ input , OutputInterface $ output )
165
165
{
@@ -270,7 +270,7 @@ public function getHelp()
270
270
/**
271
271
* Sets whether to catch exceptions or not during commands execution.
272
272
*
273
- * @param bool $boolean Whether to catch exceptions or not during commands execution
273
+ * @param bool $boolean Whether to catch exceptions or not during commands execution
274
274
*
275
275
* @api
276
276
*/
@@ -282,7 +282,7 @@ public function setCatchExceptions($boolean)
282
282
/**
283
283
* Sets whether to automatically exit after a command execution or not.
284
284
*
285
- * @param bool $boolean Whether to automatically exit after a command execution or not
285
+ * @param bool $boolean Whether to automatically exit after a command execution or not
286
286
*
287
287
* @api
288
288
*/
@@ -449,7 +449,7 @@ public function get($name)
449
449
*
450
450
* @param string $name The command name or alias
451
451
*
452
- * @return Boolean true if the command exists, false otherwise
452
+ * @return bool true if the command exists, false otherwise
453
453
*
454
454
* @api
455
455
*/
@@ -674,8 +674,8 @@ public static function getAbbreviations($names)
674
674
/**
675
675
* Returns a text representation of the Application.
676
676
*
677
- * @param string $namespace An optional namespace name
678
- * @param bool $raw Whether to return raw command list
677
+ * @param string $namespace An optional namespace name
678
+ * @param bool $raw Whether to return raw command list
679
679
*
680
680
* @return string A string representing the Application
681
681
*
@@ -691,8 +691,8 @@ public function asText($namespace = null, $raw = false)
691
691
/**
692
692
* Returns an XML representation of the Application.
693
693
*
694
- * @param string $namespace An optional namespace name
695
- * @param bool $asDom Whether to return a DOM or an XML string
694
+ * @param string $namespace An optional namespace name
695
+ * @param bool $asDom Whether to return a DOM or an XML string
696
696
*
697
697
* @return string|\DOMDocument An XML string representing the Application
698
698
*
@@ -708,34 +708,22 @@ public function asXml($namespace = null, $asDom = false)
708
708
/**
709
709
* Renders a caught exception.
710
710
*
711
- * @param \Exception $e An exception instance
711
+ * @param \Exception $e An exception instance
712
712
* @param OutputInterface $output An OutputInterface instance
713
713
*/
714
714
public function renderException ($ e , $ output )
715
715
{
716
- $ strlen = function ($ string ) {
717
- if (!function_exists ('mb_strlen ' )) {
718
- return strlen ($ string );
719
- }
720
-
721
- if (false === $ encoding = mb_detect_encoding ($ string )) {
722
- return strlen ($ string );
723
- }
724
-
725
- return mb_strlen ($ string , $ encoding );
726
- };
727
-
728
716
do {
729
717
$ title = sprintf (' [%s] ' , get_class ($ e ));
730
- $ len = $ strlen ($ title );
718
+ $ len = $ this -> stringWidth ($ title );
731
719
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
732
720
$ width = $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 1 : (defined ('HHVM_VERSION ' ) ? 1 << 31 : PHP_INT_MAX );
733
721
$ formatter = $ output ->getFormatter ();
734
722
$ lines = array ();
735
723
foreach (preg_split ('/\r?\n/ ' , $ e ->getMessage ()) as $ line ) {
736
- foreach (str_split ($ line , $ width - 4 ) as $ line ) {
724
+ foreach ($ this -> splitStringByWidth ($ line , $ width - 4 ) as $ line ) {
737
725
// pre-format lines to get the right string length
738
- $ lineLength = $ strlen (preg_replace ('/\[[^m]*m/ ' , '' , $ formatter ->format ($ line ))) + 4 ;
726
+ $ lineLength = $ this -> stringWidth (preg_replace ('/\[[^m]*m/ ' , '' , $ formatter ->format ($ line ))) + 4 ;
739
727
$ lines [] = array ($ line , $ lineLength );
740
728
741
729
$ len = max ($ lineLength , $ len );
@@ -744,7 +732,7 @@ public function renderException($e, $output)
744
732
745
733
$ messages = array ('' , '' );
746
734
$ messages [] = $ emptyLine = $ formatter ->format (sprintf ('<error>%s</error> ' , str_repeat (' ' , $ len )));
747
- $ messages [] = $ formatter ->format (sprintf ('<error>%s%s</error> ' , $ title , str_repeat (' ' , max (0 , $ len - $ strlen ($ title )))));
735
+ $ messages [] = $ formatter ->format (sprintf ('<error>%s%s</error> ' , $ title , str_repeat (' ' , max (0 , $ len - $ this -> stringWidth ($ title )))));
748
736
foreach ($ lines as $ line ) {
749
737
$ messages [] = $ formatter ->format (sprintf ('<error> %s %s</error> ' , $ line [0 ], str_repeat (' ' , $ len - $ line [1 ])));
750
738
}
@@ -890,7 +878,7 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
890
878
* @param InputInterface $input An Input instance
891
879
* @param OutputInterface $output An Output instance
892
880
*
893
- * @return integer 0 if everything went fine, or an error code
881
+ * @return int 0 if everything went fine, or an error code
894
882
*/
895
883
protected function doRunCommand (Command $ command , InputInterface $ input , OutputInterface $ output )
896
884
{
@@ -1125,4 +1113,53 @@ private function findAlternatives($name, $collection, $abbrevs, $callback = null
1125
1113
1126
1114
return array_keys ($ alternatives );
1127
1115
}
1116
+
1117
+ private function stringWidth ($ string )
1118
+ {
1119
+ if (!function_exists ('mb_strwidth ' )) {
1120
+ return strlen ($ string );
1121
+ }
1122
+
1123
+ if (false === $ encoding = mb_detect_encoding ($ string )) {
1124
+ return strlen ($ string );
1125
+ }
1126
+
1127
+ return mb_strwidth ($ string , $ encoding );
1128
+ }
1129
+
1130
+ private function splitStringByWidth ($ string , $ width )
1131
+ {
1132
+ // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
1133
+ // additionally, array_slice() is not enough as some character has doubled width.
1134
+ // we need a function to split string not by character count but by string width
1135
+
1136
+ if (!function_exists ('mb_strwidth ' )) {
1137
+ return str_split ($ string , $ width );
1138
+ }
1139
+
1140
+ if (false === $ encoding = mb_detect_encoding ($ string )) {
1141
+ return str_split ($ string , $ width );
1142
+ }
1143
+
1144
+ $ utf8String = mb_convert_encoding ($ string , 'utf8 ' , $ encoding );
1145
+ $ lines = array ();
1146
+ $ line = '' ;
1147
+ foreach (preg_split ('//u ' , $ utf8String ) as $ char ) {
1148
+ // test if $char could be appended to current line
1149
+ if (mb_strwidth ($ line .$ char ) <= $ width ) {
1150
+ $ line .= $ char ;
1151
+ continue ;
1152
+ }
1153
+ // if not, push current line to array and make new line
1154
+ $ lines [] = str_pad ($ line , $ width );
1155
+ $ line = $ char ;
1156
+ }
1157
+ if (strlen ($ line )) {
1158
+ $ lines [] = count ($ lines ) ? str_pad ($ line , $ width ) : $ line ;
1159
+ }
1160
+
1161
+ mb_convert_variables ($ encoding , 'utf8 ' , $ lines );
1162
+
1163
+ return $ lines ;
1164
+ }
1128
1165
}
0 commit comments