Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Made lib "old" PSR-0 compliant and made render() method accept string…
… render name instead of class.
  • Loading branch information
rafalp committed Mar 17, 2012
commit 6decbad617ea170e44801cbf8d8a7302dc84c595
28 changes: 12 additions & 16 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
<hr />
<?php

// Include the diff class
require_once dirname(__FILE__).'/../lib/Diff.php';

// Simple autoloader
function __autoload($class)
{
$libPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR;
require_once $libPath . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
}

// Include two sample files for comparison
$a = explode("\n", file_get_contents(dirname(__FILE__).'/a.txt'));
$b = explode("\n", file_get_contents(dirname(__FILE__).'/b.txt'));
Expand All @@ -27,44 +31,36 @@
);

// Initialize the diff class
$diff = new Diff($a, $b, $options);
$diff = new Diff_Diff($a, $b, $options);

?>
<h2>Side by Side Diff</h2>
<?php

// Generate a side by side diff
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Html/SideBySide.php';
$renderer = new Diff_Renderer_Html_SideBySide;
echo $diff->Render($renderer);
echo $diff->render('Html_SideBySide');

?>
<h2>Inline Diff</h2>
<?php

// Generate an inline diff
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Html/Inline.php';
$renderer = new Diff_Renderer_Html_Inline;
echo $diff->render($renderer);
echo $diff->render('Html_Inline');

?>
<h2>Unified Diff</h2>
<pre><?php

// Generate a unified diff
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Text/Unified.php';
$renderer = new Diff_Renderer_Text_Unified;
echo htmlspecialchars($diff->render($renderer));
echo htmlspecialchars($diff->render('Text_Unified'));

?>
</pre>
<h2>Context Diff</h2>
<pre><?php

// Generate a context diff
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Text/Context.php';
$renderer = new Diff_Renderer_Text_Context;
echo htmlspecialchars($diff->render($renderer));
echo htmlspecialchars($diff->render('Text_Context'));
?>
</pre>
</body>
Expand Down
28 changes: 24 additions & 4 deletions lib/Diff.php → lib/Diff/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @link http://github.com/chrisboulton/php-diff
*/

class Diff
class Diff_Diff
{
/**
* @var array The "old" sequence to use as the basis for the comparison.
Expand Down Expand Up @@ -94,15 +94,36 @@ public function __construct($a, $b, $options=array())
/**
* Render a diff using the supplied rendering class and return it.
*
* @param object $renderer An instance of the rendering object to use for generating the diff.
* @param string|object $renderer An instance of the rendering object to use for generating the diff.
* @return mixed The generated diff. Exact return value depends on the rendered.
*/
public function render(Diff_Renderer_Abstract $renderer)
public function render($renderer)
{
if (!is_object($renderer))
{
$renderer = 'Diff_Renderer_' . $renderer;
$renderer = new $renderer();
}

$renderer->diff = $this;
return $renderer->render();
}

/**
* Render a diff using the named rendering class and return it.
*
* @param string $renderer An instance of the rendering object to use for generating the diff.
* @return mixed The generated diff. Exact return value depends on the rendered.
*/
public function renderText($renderer)
{
$renderer = 'Diff_Renderer_Text_' . $renderer;
$renderer = new $renderer();

$renderer->diff = $this;
return $renderer->render();
}

/**
* Get a range of lines from $start to $end from the first comparison string
* and return them as an array. If no values are supplied, the entire string
Expand Down Expand Up @@ -170,7 +191,6 @@ public function getGroupedOpcodes()
return $this->groupedCodes;
}

require_once dirname(__FILE__).'/Diff/SequenceMatcher.php';
$sequenceMatcher = new Diff_SequenceMatcher($this->a, $this->b, null, $this->options);
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']);
return $this->groupedCodes;
Expand Down
2 changes: 0 additions & 2 deletions lib/Diff/Renderer/Html/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* @link http://github.com/chrisboulton/php-diff
*/

require_once dirname(__FILE__).'/../Abstract.php';

class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract
{
/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* @link http://github.com/chrisboulton/php-diff
*/

require_once dirname(__FILE__).'/Array.php';

class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array
{
/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* @link http://github.com/chrisboulton/php-diff
*/

require_once dirname(__FILE__).'/Array.php';

class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array
{
/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Diff/Renderer/Text/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* @link http://github.com/chrisboulton/php-diff
*/

require_once dirname(__FILE__).'/../Abstract.php';

class Diff_Renderer_Text_Context extends Diff_Renderer_Abstract
{
/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Diff/Renderer/Text/Unified.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* @link http://github.com/chrisboulton/php-diff
*/

require_once dirname(__FILE__).'/../Abstract.php';

class Diff_Renderer_Text_Unified extends Diff_Renderer_Abstract
{
/**
Expand Down