2012-08-10 68 views
1

我在項目中使用PHP-FineDiff作爲服務。然而,當我嘗試使用例如恢復差異來創建diff時,效果很好。 FineDiff::renderToTextFromOpcodes(),沒有任何反應。在Symfony2服務中回調失敗

我在代碼中挖了一點,發現回調不起作用。註釋掉一些

if (!is_callable($callback)) { 
    return; 
} 

後,我遇到了錯誤:

Warning: call_user_func() expects parameter 1 to be a valid callback, class 'FineDiff' not found in /var/www/sopos-blog/src/Sopos/WikiBundle/Services/FineDiff.php line 321.

這些都是一些代碼相關線路:

public static function renderToTextFromOpcodes($from, $opcodes) { 
    ob_start(); 
    FineDiff::renderFromOpcodes($from, $opcodes, array('FineDiff','renderToTextFromOpcode')); 
    return ob_get_clean(); 
} 

// ... 

public static function renderFromOpcodes($from, $opcodes, $callback) { 
    if (!is_callable($callback)) { 
     return; 
     } 
    $opcodes_len = strlen($opcodes); 
    $from_offset = $opcodes_offset = 0; 
    while ($opcodes_offset < $opcodes_len) { 
     $opcode = substr($opcodes, $opcodes_offset, 1); 
     $opcodes_offset++; 
     $n = intval(substr($opcodes, $opcodes_offset)); 
     if ($n) { 
      $opcodes_offset += strlen(strval($n)); 
      } 
     else { 
      $n = 1; 
      } 
     if ($opcode === 'c') { // copy n characters from source 
      call_user_func($callback, 'c', $from, $from_offset, $n, ''); 
      $from_offset += $n; 
      } 

// ... 

回答

1

我猜你的類的FQCN爲Sopos\WikiBundle\Services\FineDiff,所以回調必須是這些:

$arrayCallback = array(__NAMESPACE__.'\FineDiff','renderToTextFromOpcode'); 
$stringCallback = __NAMESPACE__.'\FineDiff::renderToTextFromOpcode'; 

您也可以用真實名稱空間替換__NAMESPACE__

+0

謝謝@Florent,它現在可以工作! – Michael 2012-08-10 15:38:44

0

Symfony2無法自動載入類FineDiff。將名稱空間添加到回調函數中:

public static function renderToTextFromOpcodes($from, $opcodes) { 
    ob_start(); 
    FineDiff::renderFromOpcodes($from, $opcodes, array(__NAMESPACE__.'\FineDiff','renderToTextFromOpcode')); 
    return ob_get_clean(); 
}