Skip to content

Namespacing #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"type": "library",
"description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).",
"license": "BSD-3-Clause",
"keywords": [
"php",
"diff"
],
"authors": [
{
"name": "Mario",
Expand Down
20 changes: 8 additions & 12 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<h1>PHP LibDiff - Examples</h1>
<hr />
<?php

// Include the diff class
require_once dirname(__FILE__).'/../lib/Diff.php';
// include autoloader
require dirname(__FILE__).'/../lib/Autoloader.php';
new \jblond\Autoloader();

// Include two sample files for comparison
$a = explode("\n", file_get_contents(dirname(__FILE__).'/a.txt'));
Expand All @@ -24,15 +24,14 @@
);

// Initialize the diff class
$diff = new Diff($a, $b, $options);
$diff = new \jblond\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(array(
$renderer = new \jblond\Diff\Renderer\Html\SideBySide(array(
'title_a' => 'Custom title for OLD version',
'title_b' => 'Custom title for NEW version',
));
Expand All @@ -43,17 +42,15 @@
<?php

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

?>
<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;
$renderer = new \jblond\Diff\Renderer\Text\Unified();
echo htmlspecialchars($diff->render($renderer));

?>
Expand All @@ -62,8 +59,7 @@
<pre><?php

// Generate a context diff
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Text/Context.php';
$renderer = new Diff_Renderer_Text_Context;
$renderer = new \jblond\Diff\Renderer\Text\Context;
echo htmlspecialchars($diff->render($renderer));
?>
</pre>
Expand Down
33 changes: 33 additions & 0 deletions lib/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace jblond;

/**
* Class Autoloader
* @package jblond
*/
class Autoloader
{

/**
* Autoloader constructor.
*/
public function __construct() {
spl_autoload_register(array($this, '__autoload'));
}

/**
* @param string $class
*/
private function __autoload($class) {
$class = str_replace('\\', '/', $class); // revert path for old PHP on Linux
$dir = str_replace('\\', '/', __DIR__);
if(file_exists($dir . '/' . $class . '.php')) {
/** @noinspection PhpIncludeInspection */
require $dir . '/' . $class . '.php';
}
else
{
echo '<b>' . $dir . '/' . $class . '.php' . '</b><br><br>';
}
}
}
12 changes: 8 additions & 4 deletions lib/Diff.php → lib/jblond/Diff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
namespace jblond;

use jblond\Diff\SequenceMatcher;

/**
* Diff
*
Expand Down Expand Up @@ -94,14 +98,14 @@ public function __construct($a, $b, $options=array())
$this->options = $this->defaultOptions;
}


/**
* Render a diff using the supplied rendering class and return it.
*
* @param \Diff_Renderer_Abstract|object $renderer An instance of the rendering object to use for generating the diff.
*
* @param object $renderer 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)
{
$renderer->diff = $this;
return $renderer->render();
Expand Down Expand Up @@ -175,7 +179,7 @@ public function getGroupedOpcodes()
}

require_once dirname(__FILE__).'/Diff/SequenceMatcher.php';
$sequenceMatcher = new Diff_SequenceMatcher($this->a, $this->b, $this->options, null);
$sequenceMatcher = new SequenceMatcher($this->a, $this->b, $this->options, null);
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']);
return $this->groupedCodes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace jblond\Diff\Renderer;

/**
* Abstract class for diff renderers in PHP DiffLib.
*
Expand Down Expand Up @@ -40,7 +42,7 @@
* @link https://github.com/JBlond/php-diff
*/

abstract class Diff_Renderer_Abstract
abstract class RendererAbstract
{
/**
* @var object Instance of the diff class that this renderer is generating the rendered diff for.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
namespace jblond\Diff\Renderer\Html;

use jblond\Diff\Renderer\RendererAbstract;

/**
* Base renderer for rendering HTML based diffs for PHP DiffLib.
*
Expand Down Expand Up @@ -45,7 +49,7 @@
/**
* Class Diff_Renderer_Html_Array
*/
class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract
class HtmlArray extends RendererAbstract
{
/**
* @var array Array of the default options that apply to this renderer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace jblond\Diff\Renderer\Html;

/**
* Inline HTML diff generator for PHP DiffLib.
*
Expand Down Expand Up @@ -45,7 +47,7 @@
/**
* Class Diff_Renderer_Html_Inline
*/
class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array
class Inline extends HtmlArray
{
/**
* Render a and return diff with changes between the two sequences
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace jblond\Diff\Renderer\Html;

/**
* Side by Side HTML diff generator for PHP DiffLib.
*
Expand Down Expand Up @@ -45,7 +47,7 @@
/**
* Class Diff_Renderer_Html_SideBySide
*/
class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array
class SideBySide extends HtmlArray
{
/**
* Render a and return diff with changes between the two sequences
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
namespace jblond\Diff\Renderer\Text;

use jblond\Diff\Renderer\RendererAbstract;

/**
* Context diff generator for PHP DiffLib.
*
Expand Down Expand Up @@ -45,7 +49,7 @@
/**
* Class Diff_Renderer_Text_Context
*/
class Diff_Renderer_Text_Context extends Diff_Renderer_Abstract
class Context extends RendererAbstract
{
/**
* @var array Array of the different op code tags and how they map to the context diff equivalent.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
namespace jblond\Diff\Renderer\Text;

use jblond\Diff\Renderer\RendererAbstract;

/**
* Unified diff generator for PHP DiffLib.
*
Expand Down Expand Up @@ -45,7 +49,7 @@
/**
* Class Diff_Renderer_Text_Unified
*/
class Diff_Renderer_Text_Unified extends Diff_Renderer_Abstract
class Unified extends RendererAbstract
{
/**
* Render and return a unified diff.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace jblond\Diff;

/**
* Sequence matcher for Diff
*
Expand Down Expand Up @@ -40,7 +42,7 @@
* @link https://github.com/JBlond/php-diff
*/

class Diff_SequenceMatcher
class SequenceMatcher
{
/**
* @var string|array Either a string or an array containing a callback function to determine if a line is "junk" or not.
Expand Down