本文整理汇总了PHP中Diff类的典型用法代码示例。如果您正苦于以下问题:PHP Diff类的具体用法?PHP Diff怎么用?PHP Diff使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Diff类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: diff
/**
* Get the difference between two files
*
* @static
* @access private
* @param string $a The path to the existing file
* @param string $b The path to the new (replacement) file
* @return string The unified diff between the two
*/
private static function diff($a, $b)
{
$a_code = explode("\n", file_get_contents($a));
$b_code = explode("\n", file_get_contents($b));
$diff = new \Diff($b_code, $a_code, array('ignoreWhitespace' => TRUE, 'ignoreNewLines' => TRUE));
return $diff->render(new \Diff_Renderer_Text_Unified());
}
示例2: format
/**
* @param Diff $diff A Diff object.
*
* @return array[] List of associative arrays, each describing a difference.
*/
public function format($diff)
{
$oldline = 1;
$newline = 1;
$retval = [];
foreach ($diff->getEdits() as $edit) {
switch ($edit->getType()) {
case 'add':
foreach ($edit->getClosing() as $line) {
$retval[] = ['action' => 'add', 'new' => $line, 'newline' => $newline++];
}
break;
case 'delete':
foreach ($edit->getOrig() as $line) {
$retval[] = ['action' => 'delete', 'old' => $line, 'oldline' => $oldline++];
}
break;
case 'change':
foreach ($edit->getOrig() as $key => $line) {
$retval[] = ['action' => 'change', 'old' => $line, 'new' => $edit->getClosing($key), 'oldline' => $oldline++, 'newline' => $newline++];
}
break;
case 'copy':
$oldline += count($edit->getOrig());
$newline += count($edit->getOrig());
}
}
return $retval;
}
示例3: saveTextRevision
public function saveTextRevision($moduleId, $moduleContentTypeId, $moduleContentId, $oldText, $newText, $textfieldIndex = 0, $revision = 0)
{
global $dbi, $login, $settings;
if (!$settings->enableRevisioning) {
return;
}
// Get latest revision number and increment
$maxRevision = 0;
if (empty($revision)) {
$result = $dbi->query("SELECT MAX(revision) FROM " . revisionTableName . " WHERE moduleId=" . $dbi->quote($moduleId) . " AND moduleContentTypeId=" . $dbi->quote($moduleContentTypeId) . " AND moduleContentId=" . $dbi->quote($moduleContentId));
if ($result->rows()) {
list($maxRevision) = $result->fetchrow_array();
$maxRevision++;
}
} else {
$maxRevision = $revision;
}
// Parse text strings
$newText = parseString($newText);
$oldText = parseString($oldText);
// Calculate diff
$diff = new Diff();
$result = $diff->stringDiff($newText, $oldText, " ");
$changes = $diff->sequentialChanges($result);
if (sizeof($changes) > 0) {
$serializedChanges = serialize($changes);
// Insert diff
$dbi->query("INSERT INTO " . revisionTableName . "(moduleId,moduleContentTypeId,moduleContentId,textfieldIndex,diff,revision,userId,timestamp) VALUES(" . $dbi->quote($moduleId) . "," . $dbi->quote($moduleContentTypeId) . "," . $dbi->quote($moduleContentId) . "," . $dbi->quote($textfieldIndex) . "," . $dbi->quote($serializedChanges) . "," . $dbi->quote($maxRevision) . "," . $dbi->quote($login->id) . ",NOW())");
}
return $maxRevision;
}
示例4: parseFileDiff
/**
* @param Diff $diff
* @param array $lines
*/
private function parseFileDiff(Diff $diff, array $lines)
{
$chunks = array();
foreach ($lines as $line) {
if (preg_match('/^@@\\s+-(?P<start>\\d+)(?:,\\s*(?P<startrange>\\d+))?\\s+\\+(?P<end>\\d+)(?:,\\s*(?P<endrange>\\d+))?\\s+@@/', $line, $match)) {
$chunk = new Chunk($match['start'], isset($match['startrange']) ? max(1, $match['startrange']) : 1, $match['end'], isset($match['endrange']) ? max(1, $match['endrange']) : 1);
$chunks[] = $chunk;
$diffLines = array();
continue;
}
if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
$type = Line::UNCHANGED;
if ($match['type'] == '+') {
$type = Line::ADDED;
} elseif ($match['type'] == '-') {
$type = Line::REMOVED;
}
$diffLines[] = new Line($type, $match['line']);
if (isset($chunk)) {
$chunk->setLines($diffLines);
}
}
}
$diff->setChunks($chunks);
}
示例5: buildPatch
public function buildPatch($originalFile, $modfiedFileName, PatchBuffer $buffer)
{
if (!$buffer->isModified()) {
return '';
}
$diff = new \Diff($buffer->getOriginalContents(), $buffer->getModifiedContents());
$renderer = new \Diff_Renderer_Text_Unified();
return "--- {$originalFile}\n" . "+++ {$modfiedFileName}\n" . $diff->render($renderer);
}
示例6: testReplace
public function testReplace()
{
$old = ['first', 'equal'];
$new = ['second', 'equal'];
$diff = new \Diff($old, $new);
$renderer = new \VisualAppeal\Connect\Extensions\DiffRenderer();
$render = @$diff->render($renderer);
$this->assertContains('diff-change-replace', $render);
}
示例7: test_format
function test_format()
{
$diff = new Diff(DIFF_SPACE);
$one = 'a b c d e f g';
$two = 'a c d e t t f';
$res = $diff->compare($one, $two);
$out = $diff->format($res);
$expected = array(array('', 'a'), array('-', 'b'), array('', 'c'), array('', 'd'), array('', 'e'), array('+', 't'), array('+', 't'), array('', 'f'), array('-', 'g'));
$this->assertEquals($out, $expected);
}
示例8: paintDiff
function paintDiff($stringA, $stringB)
{
$diff = new Diff(explode("\n", $stringA), explode("\n", $stringB));
if ($diff->isEmpty()) {
echo '<p>Erreur diff : bizarre, aucune différence d\'aprés la difflib...</p>';
} else {
$fmt = new HtmlUnifiedDiffFormatter();
echo $fmt->format($diff);
}
}
示例9: testPrintAsJson_usingSlimVersion_shouldReturnSimpleJson
public function testPrintAsJson_usingSlimVersion_shouldReturnSimpleJson()
{
$object = 'EmpresaConf';
$property = 'descricao';
$value1 = 'Hosanna';
$value2 = 'Hosanna Tecnologia';
$diff = new Diff($object, $property, $value1, $value2);
$expectedJson = sprintf('{"%s%s%s":["%s","%s"]}', $object, Diff::SEPARATOR, $property, $value1, $value2);
$this->assertEquals($expectedJson, $diff->printAsJson(true));
}
示例10: compareVersions
/**
* Show difference between two versions.
*
* @param \App\Version
* @param \App\Version
* @return void
*/
protected function compareVersions(Version $before, Version $after)
{
// Parse source
$beforeArray = explode("\n", $before->source);
$afterArray = explode("\n", $after->source);
// Compare versions
$diff = new \Diff($beforeArray, $afterArray, $options = []);
// Load view
return view('version.compare', ['title' => _('Versions'), 'subtitle' => _('Compare'), 'before' => $before, 'after' => $after, 'sideBySideDiff' => $diff->Render(new \Diff_Renderer_Html_SideBySide()), 'inlineDiff' => $diff->render(new \Diff_Renderer_Html_Inline())]);
}
示例11: showDiff
protected function showDiff($str1, $str2)
{
$diff = new Diff(explode("\n", $str1), explode("\n", $str2));
if ($diff->isEmpty()) {
$this->fail("No difference ???");
} else {
$fmt = new UnifiedDiffFormatter();
$this->fail($fmt->format($diff));
}
}
示例12: paintDiff
function paintDiff($stringA, $stringB)
{
$diff = new Diff(explode("\n", $stringA), explode("\n", $stringB));
if ($diff->isEmpty()) {
$this->_response->addContent('<p>Erreur diff : bizarre, aucune différence d\'aprés la difflib...</p>');
} else {
$fmt = new UnifiedDiffFormatter();
$this->_response->addContent($fmt->format($diff));
}
}
示例13: compare
public function compare(IResource $resourceA, IResource $resourceB)
{
$data_a = $resourceA->getCleanFields();
$data_b = $resourceB->getCleanFields();
$a = explode("\n", $data_a['snippet']);
$b = explode("\n", $data_b['snippet']);
$d = new \Diff($a, $b, []);
$renderer = new \Diff_Renderer_Html_SideBySide();
$diffc = $d->render($renderer);
return !empty($diffc);
}
示例14: diff
function diff($path, $action, $title, $content)
{
$Head = '<meta name="robots" content="noindex, nofollow" />';
$content['PageNav']->Active("Page History");
if (is_numeric($action[1])) {
$pageQuery = mysql_query("SELECT `PageID`,`AccountID`,`EditTime`,`Name`,`Description`,`Title`,`Content` FROM `Wiki_Edits` WHERE `ID`='{$action['1']}' and `Archived` = 0");
list($PageID, $AccountID, $PageEditTime, $PageName, $PageDescription, $PageTitle, $pageContent) = mysql_fetch_array($pageQuery);
$previousQuery = mysql_query("Select `ID`, `Content` from `Wiki_Edits` where `ID` < '{$action['1']}' and `PageID`='{$PageID}' and `Archived` = 0 order by `ID` desc limit 1");
list($previousID, $previousContent) = mysql_fetch_array($previousQuery);
$nextQuery = mysql_query("Select `ID` from `Wiki_Edits` where `ID` > '{$action['1']}' and `PageID`='{$PageID}' and `Archived` = 0 order by `ID` limit 1");
list($nextID) = mysql_fetch_array($nextQuery);
if (!empty($previousID)) {
$previousPath = FormatPath("/{$path}/?diff/{$previousID}");
$content['Title'] = "<a href='{$previousPath}' title='Previous Revision'>⟨</a> ";
}
$content['Title'] .= FishFormat($PageTitle);
if (!empty($nextID)) {
$nextPath = FormatPath("/{$path}/?diff/{$nextID}");
$content['Title'] .= " <a href='{$nextPath}' title='Next Revision'>⟩</a>";
}
$content['Body'] .= <<<JavaScript
<script>
\$(document).ready(function ()
{
\$('body').on('keydown', function(event)
{
event.stopImmediatePropagation()
if(event.keyCode == 37) // Previous
location.href = '{$previousPath}';
else if(event.keyCode == 39) // Next
location.href = '{$nextPath}';
});
});
</script>
JavaScript;
$old = explode("\n", html_entity_decode($previousContent, ENT_QUOTES));
$new = explode("\n", html_entity_decode($pageContent, ENT_QUOTES));
// Initialize the diff class
$diff = new Diff($old, $new);
require_once dirname(__FILE__) . '/../libraries/Diff/Renderer/Html/SideBySide.php';
$renderer = new Diff_Renderer_Html_SideBySide();
$content['Body'] .= $diff->Render($renderer);
date_default_timezone_set('America/New_York');
$PageEditTime = formatTime($PageEditTime);
$content['Footer'] = "This page is an old revision made by <b><a href='/names?id={$AccountID}'>{$PageName}</a></b> on {$PageEditTime}.";
if ($PageDescription) {
$content['Footer'] .= "<br />'{$PageDescription}'";
}
}
return array($title, $content);
}
示例15: getDiffHtml
public function getDiffHtml()
{
$old = explode("\n", $this->old_value);
$new = explode("\n", $this->new_value);
foreach ($old as $i => $line) {
$old[$i] = rtrim($line, "\r\n");
}
foreach ($new as $i => $line) {
$new[$i] = rtrim($line, "\r\n");
}
$diff = new \Diff($old, $new);
return $diff->render(new \Diff_Renderer_Html_Inline());
}