在Symfony控制台中添加跨越多行的行

时间:2016-03-17 16:26:16

标签: php symfony console command-line-interface

我目前正在使用Symfony控制台开发CLI,我遇到了有关添加跨越多行的行的问题。

我的代码是

$table = new Table($output);
$table->setHeaders(['Elements','Properties']);

foreach($elements as $key => $element) {  
  if ($key != 'templates') {
    //process element
    $rowcount = 0;
    foreach($element as $name => $component){

      if(is_string($component)){

        $strings[] = $name.' = '.($component == '' ? 'empty' : $component);
        $rowcount++;
      }
    }

    //creating row
    $count = 0;
    foreach ($strings as $string) {
      if ($count == 0) {
        $frow[] = new TableCell($key, ['rowspan' => $rowcount]);
        $frow[] = $string;
      } else {
        $row[] = $string;
      }    

      $count++;
    }

    $rows = [$frow,$row];   
    var_dump($rows);
    $table->addRows($rows);
    unset($frow,$row,$strings);
  }
}        

$table->setStyle('borderless');
$table->render();

此时var_dump会创建此结果

array(2) {
      [0]=>
      array(2) {
        [0]=>
        object(Symfony\Component\Console\Helper\TableCell)#63 (2) {
          ["value":"Symfony\Component\Console\Helper\TableCell":private]=>
          string(5) "forms"
          ["options":"Symfony\Component\Console\Helper\TableCell":private]=>
          array(2) {
            ["rowspan"]=>
            int(4)
            ["colspan"]=>
            int(1)
          }
        }
        [1]=>
        string(12) "name = forms"
      }
      [1]=>
      array(3) {
        [0]=>
        string(18) "path = admin/forms"
        [1]=>
        string(14) "scope = system"
        [2]=>
        string(16) "attachto = empty"
      }
    }

CLI应该如下所示

============= ===================
Elements      Properties
============= ===================
forms          name = forms
               path = admin/forms
               scope = system
               attachto = empty

我不知道这是否太多但问题是第二列的对齐完全脱节。scope = system部分移动到第3列。

0 个答案:

没有答案