Back to Patcher class

Method applyHunk

protected void
applyHunk
(mixed &$lines, mixed $src, mixed $dst, mixed $srcLine, mixed $srcSize, mixed $dstLine, mixed $dstSize)
Apply the patch
Parameters
  • array $lines The udiff array of lines
  • string $src The source file
  • string $dst The destination file
  • string $srcLine The beginning of the patch for the source file
  • string $srcSize The size of the patch for the source file
  • string $dstLine The beginning of the patch for the destination file
  • string $dstSize The size of the patch for the destination file
Returns
  • void
Since
  • 3.0.0
-
  • \RuntimeException
Class: Patcher
Project: Joomla

Method applyHunk - Source code

/**
 * Apply the patch
 *
 * @param   array   $lines    The udiff array of lines
 * @param   string  $src      The source file
 * @param   string  $dst      The destination file
 * @param   string  $srcLine  The beginning of the patch for the source file
 * @param   string  $srcSize  The size of the patch for the source file
 * @param   string  $dstLine  The beginning of the patch for the destination file
 * @param   string  $dstSize  The size of the patch for the destination file
 *
 * @return  void
 *
 * @since   3.0.0
 * @throws  \RuntimeException
 */
protected function applyHunk(&$lines, $src, $dst, $srcLine, $srcSize, $dstLine, $dstSize)
{
    $srcLine--;
    $dstLine--;
    $line = current($lines);
    // Source lines (old file)
    $source = array();
    // New lines (new file)
    $destin = array();
    $src_left = $srcSize;
    $dst_left = $dstSize;
    do {
        if (!isset($line[0])) {
            $source[] = '';
            $destin[] = '';
            $src_left--;
            $dst_left--;
        } elseif ($line[0] == '-') {
            if ($src_left == 0) {
                throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXPECTED_REMOVE_LINE', key($lines)));
            }
            $source[] = substr($line, 1);
            $src_left--;
        } elseif ($line[0] == '+') {
            if ($dst_left == 0) {
                throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXPECTED_ADD_LINE', key($lines)));
            }
            $destin[] = substr($line, 1);
            $dst_left--;
        } elseif ($line != '\\ No newline at end of file') {
            $line = substr($line, 1);
            $source[] = $line;
            $destin[] = $line;
            $src_left--;
            $dst_left--;
        }
        if ($src_left == 0 && $dst_left == 0) {
            // Now apply the patch, finally!
            if ($srcSize > 0) {
                $src_lines =& $this->getSource($src);
                if (!isset($src_lines)) {
                    throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_UNEXISTING_SOURCE', Path::removeRoot($src)));
                }
            }
            if ($dstSize > 0) {
                if ($srcSize > 0) {
                    $dst_lines =& $this->getDestination($dst, $src);
                    $src_bottom = $srcLine + \count($source);
                    for ($l = $srcLine; $l < $src_bottom; $l++) {
                        if ($src_lines[$l] != $source[$l - $srcLine]) {
                            throw new \RuntimeException(Text::sprintf('JLIB_FILESYSTEM_PATCHER_FAILED_VERIFY', Path::removeRoot($src), $l));
                        }
                    }
                    array_splice($dst_lines, $dstLine, \count($source), $destin);
                } else {
                    $this->destinations[$dst] = $destin;
                }
            } else {
                $this->removals[] = $src;
            }
            next($lines);
            return;
        }
        $line = next($lines);
    } while ($line !== false);
    throw new \RuntimeException('Unexpected EOF');
}