* @license http://opensource.org/licenses/mit-license.php MIT * @modified Mark Scherer (Make it work with 2.x and clean it up into Lib + Helper) */ class DiffHelper extends AppHelper { public $helpers = ['Html']; /** * Construct function * Loads the vendor classes and sets the include path for autoloader to work * */ public function __construct($View = null, $config = []) { parent::__construct($View, $config); $this->Diff = new DiffLib(); } /** * @param string $renderType * 'unified', 'inline', 'context', 'sidebyside' * defaults to "inline" * @return bool Success */ public function renderType($type = null) { return $this->Diff->renderType($type); } /** * @param string $engineType * 'auto', 'native', 'xdiff', 'shell', 'string' * defaults to "auto" * @return bool Success */ public function engineType($type = null) { return $this->Diff->engineType($type); } /** * Compare function * Compares two strings/arrays using the specified method and renderer * * @param mixed $original * @param mixed $changed * @param array $options * - div: true/false * - class: defaults to "diff" * - escape: defaults to true * @return string output */ public function compare($original, $changed, $options = []) { $original = $this->_prep($original); $changed = $this->_prep($changed); $string = $this->Diff->compare($original, $changed, $options); if (isset($options['div']) && $options['div'] === false) { return $string; } $defaults = [ 'class' => 'diff' ]; $options += $defaults; $options['escape'] = null; return $this->Html->tag('div', $string, $options); } /** * @param string $string Either context or unified diff snippet * @param array $options * - mode (autodetect, context, unified) * @return string */ public function reverse($string, $options = []) { $string = $this->Diff->reverse($string, $options); if (isset($options['div']) && $options['div'] === false) { return $string; } $defaults = [ 'class' => 'diff' ]; $options += $defaults; $options['escape'] = null; return $this->Html->tag('div', $string, $options); } /** * Prep for security * maybe switch to do that after comparison? * * @param string $string * @param array $options * @return string */ protected function _prep($string, $options = []) { if ($this->renderer === 'inline' || isset($options['escape']) && $options['escape'] === false) { return $string; } return h($string); } }