Skip to content
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

Allow caching of the calculated diffs using a doctrine cache provider #33

Merged
merged 4 commits into from
Mar 10, 2016
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
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
"php": ">=5.3.3",
"ezyang/htmlpurifier": "^4.7"
},
"require-dev": {
"phpunit/phpunit": "~4.8",
"doctrine/cache": "~1.0"
},
"suggest": {
"doctrine/cache": "Used for caching the calculated diffs using a Doctrine Cache Provider"
},
"autoload": {
"psr-0": { "Caxy\\HtmlDiff": "lib/" }
},
"autoload-dev": {
"psr-4": { "Caxy\\Tests\\": "tests/Caxy/Tests" }
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
Expand Down
36 changes: 36 additions & 0 deletions lib/Caxy/HtmlDiff/AbstractDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ abstract class AbstractDiff
*/
protected $newWords = array();

/**
* @var DiffCache[]
*/
private $diffCaches = array();

/**
* AbstractDiff constructor.
*
Expand Down Expand Up @@ -81,6 +86,37 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
$this->content = '';
}

/**
* @return bool|string
*/
abstract public function build();

/**
* @return DiffCache|null
*/
protected function getDiffCache()
{
if (!$this->hasDiffCache()) {
return null;
}

$hash = spl_object_hash($this->getConfig()->getCacheProvider());

if (!array_key_exists($hash, $this->diffCaches)) {
$this->diffCaches[$hash] = new DiffCache($this->getConfig()->getCacheProvider());
}

return $this->diffCaches[$hash];
}

/**
* @return bool
*/
protected function hasDiffCache()
{
return null !== $this->getConfig()->getCacheProvider();
}

/**
* @return HtmlDiffConfig
*/
Expand Down
112 changes: 112 additions & 0 deletions lib/Caxy/HtmlDiff/DiffCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Caxy\HtmlDiff;

use Doctrine\Common\Cache\Cache;

/**
* Class DiffCache
* @package Caxy\HtmlDiff
*/
class DiffCache
{
/**
* @var Cache
*/
protected $cacheProvider;

/**
* DiffCache constructor.
*
* @param Cache $cacheProvider
*/
public function __construct(Cache $cacheProvider)
{
$this->cacheProvider = $cacheProvider;
}

/**
* @return Cache
*/
public function getCacheProvider()
{
return $this->cacheProvider;
}

/**
* @param Cache $cacheProvider
*
* @return DiffCache
*/
public function setCacheProvider($cacheProvider)
{
$this->cacheProvider = $cacheProvider;

return $this;
}

/**
* @param string $oldText
* @param string $newText
*
* @return bool
*/
public function contains($oldText, $newText)
{
return $this->cacheProvider->contains($this->getHashKey($oldText, $newText));
}

/**
* @param string $oldText
* @param string $newText
*
* @return string
*/
public function fetch($oldText, $newText)
{
return $this->cacheProvider->fetch($this->getHashKey($oldText, $newText));
}

/**
* @param string $oldText
* @param string $newText
* @param string $data
* @param int $lifeTime
*
* @return bool
*/
public function save($oldText, $newText, $data, $lifeTime = 0)
{
return $this->cacheProvider->save($this->getHashKey($oldText, $newText), $data, $lifeTime);
}

/**
* @param string $oldText
* @param string $newText
*
* @return bool
*/
public function delete($oldText, $newText)
{
return $this->cacheProvider->delete($this->getHashKey($oldText, $newText));
}

/**
* @return array|null
*/
public function getStats()
{
return $this->cacheProvider->getStats();
}

/**
* @param string $oldText
* @param string $newText
*
* @return string
*/
protected function getHashKey($oldText, $newText)
{
return sprintf('%s_%s', md5($oldText), md5($newText));
}
}
10 changes: 10 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function getInsertSpaceInReplace()
*/
public function build()
{
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

return $this->content;
}

$this->splitInputsToWords();
$this->replaceIsolatedDiffTags();
$this->indexNewWords();
Expand All @@ -100,6 +106,10 @@ public function build()
$this->performOperation( $item );
}

if ($this->hasDiffCache()) {
$this->getDiffCache()->save($this->oldText, $this->newText, $this->content);
}

return $this->content;
}

Expand Down
25 changes: 25 additions & 0 deletions lib/Caxy/HtmlDiff/HtmlDiffConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class HtmlDiffConfig
*/
protected $useTableDiffing = true;

/**
* @var null|\Doctrine\Common\Cache\Cache
*/
protected $cacheProvider;

/**
* @return HtmlDiffConfig
*/
Expand Down Expand Up @@ -416,6 +421,26 @@ public function setUseTableDiffing($useTableDiffing)
return $this;
}

/**
* @param null|\Doctrine\Common\Cache\Cache $cacheProvider
*
* @return $this
*/
public function setCacheProvider(\Doctrine\Common\Cache\Cache $cacheProvider = null)
{
$this->cacheProvider = $cacheProvider;

return $this;
}

/**
* @return null|\Doctrine\Common\Cache\Cache
*/
public function getCacheProvider()
{
return $this->cacheProvider;
}

/**
* @param string $tag
*
Expand Down
14 changes: 13 additions & 1 deletion lib/Caxy/HtmlDiff/ListDiffNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,24 @@ public static function create($oldText, $newText, HtmlDiffConfig $config = null)

public function build()
{
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

return $this->content;
}

$this->splitInputsToWords();

return $this->diffLists(
$this->content = $this->diffLists(
$this->buildDiffList($this->oldWords),
$this->buildDiffList($this->newWords)
);

if ($this->hasDiffCache()) {
$this->getDiffCache()->save($this->oldText, $this->newText, $this->content);
}

return $this->content;
}

protected function diffLists(DiffList $oldList, DiffList $newList)
Expand Down
10 changes: 10 additions & 0 deletions lib/Caxy/HtmlDiff/Table/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCas
*/
public function build()
{
if ($this->hasDiffCache() && $this->getDiffCache()->contains($this->oldText, $this->newText)) {
$this->content = $this->getDiffCache()->fetch($this->oldText, $this->newText);

return $this->content;
}

$this->buildTableDoms();

$this->diffDom = new \DOMDocument();
Expand All @@ -100,6 +106,10 @@ public function build()

$this->diffTableContent();

if ($this->hasDiffCache()) {
$this->getDiffCache()->save($this->oldText, $this->newText, $this->content);
}

return $this->content;
}

Expand Down