You are here

public function HackedDiffController::hackedProjectDiff in Hacked! 8.2

Shows a diff report for a specific file in a project.

Parameters

$project: The hackedProject instance.

\Drupal\hacked\hackedProject $project:

Return value

array

1 string reference to 'HackedDiffController::hackedProjectDiff'
hacked.routing.yml in ./hacked.routing.yml
hacked.routing.yml

File

src/Controller/HackedDiffController.php, line 55

Class

HackedDiffController
Controller routines for hacked routes.

Namespace

Drupal\hacked\Controller

Code

public function hackedProjectDiff(hackedProject $project) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('diff')) {
    return [
      '#markup' => $this
        ->t('The diff module is required to use this feature.'),
    ];
  }
  $file = \Drupal::request()
    ->get('file');
  $project
    ->identify_project();

  // Find a better way to do this:
  //    $breadcrumb = array(
  //      l('Home', '<front>'),
  //      l('Administer', 'admin'),
  //      l('Reports', 'admin/reports'),
  //      l('Hacked', 'admin/reports/hacked'),
  //      l($project->title(), 'admin/reports/hacked/' . $project->name),
  //    );
  //    drupal_set_breadcrumb($breadcrumb);
  if ($project
    ->file_is_diffable($file)) {
    $original_file = $project
      ->file_get_location('remote', $file);
    $installed_file = $project
      ->file_get_location('local', $file);

    /** @var hackedFileHasher $hasher */
    $hasher = hacked_get_file_hasher();
    $build = [
      '#theme' => 'table',
      '#header' => [
        t('Original'),
        '',
        t('Current'),
        '',
      ],
      '#rows' => $this->entityComparison
        ->getRows($hasher
        ->fetch_lines($original_file), $hasher
        ->fetch_lines($installed_file), TRUE),
    ];

    // Add the CSS for the diff.
    $build['#attached']['library'][] = 'diff/diff.general';
    $theme = $this->config
      ->get('general_settings.theme');
    if ($theme) {
      if ($theme == 'default') {
        $build['#attached']['library'][] = 'diff/diff.default';
      }
      elseif ($theme == 'github') {
        $build['#attached']['library'][] = 'diff/diff.github';
      }
    }
    elseif ($theme == NULL) {
      $build['#attached']['library'][] = 'diff/diff.github';
    }
    return $build;
  }
  return [
    '#markup' => $this
      ->t('Cannot hash binary file or file not found: %file', array(
      '%file' => $file,
    )),
  ];
}