Pull merge.
[yaffs-website] / web / modules / contrib / hacked / src / Controller / HackedDiffController.php
1 <?php
2
3 namespace Drupal\hacked\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\diff\DiffEntityComparison;
7 use Drupal\hacked\hackedFileHasher;
8 use Drupal\hacked\hackedProject;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Controller routines for hacked routes.
13  */
14 class HackedDiffController extends ControllerBase {
15
16   /**
17    * Wrapper object for writing/reading configuration from diff.plugins.yml
18    */
19   protected $config;
20
21   /**
22    * The diff entity comparison service.
23    */
24   protected $entityComparison;
25
26   /**
27    * Constructs a HackedDiffController object.
28    *
29    * @param DiffEntityComparison $entity_comparison
30    *   DiffEntityComparison service.
31    */
32   public function __construct(DiffEntityComparison $entity_comparison) {
33     $this->config = $this->config('diff.settings');
34     $this->entityComparison = $entity_comparison;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static(
42       $container->get('diff.entity_comparison')
43     );
44   }
45
46   /**
47    * Shows a diff report for a specific file in a project.
48    *
49    * @param                              $project
50    *   The hackedProject instance.
51    *
52    * @param \Drupal\hacked\hackedProject $project
53    * @return array
54    */
55   public function hackedProjectDiff(hackedProject $project) {
56     if (!\Drupal::moduleHandler()->moduleExists('diff')) {
57       return [
58         '#markup' => $this->t('The diff module is required to use this feature.')
59       ];
60     }
61
62     $file = \Drupal::request()->get('file');
63     $project->identify_project();
64
65     // Find a better way to do this:
66 //    $breadcrumb = array(
67 //      l('Home', '<front>'),
68 //      l('Administer', 'admin'),
69 //      l('Reports', 'admin/reports'),
70 //      l('Hacked', 'admin/reports/hacked'),
71 //      l($project->title(), 'admin/reports/hacked/' . $project->name),
72 //    );
73 //    drupal_set_breadcrumb($breadcrumb);
74
75     if ($project->file_is_diffable($file)) {
76       $original_file = $project->file_get_location('remote', $file);
77       $installed_file = $project->file_get_location('local', $file);
78
79       /** @var hackedFileHasher $hasher */
80       $hasher = hacked_get_file_hasher();
81
82       $build = [
83         '#theme'    => 'table',
84         '#header'   => [t('Original'), '', t('Current'), ''],
85         '#rows'     => $this->entityComparison->getRows($hasher->fetch_lines($original_file), $hasher->fetch_lines($installed_file), TRUE),
86       ];
87
88       // Add the CSS for the diff.
89       $build['#attached']['library'][] = 'diff/diff.general';
90       $theme = $this->config->get('general_settings.theme');
91       if ($theme) {
92         if ($theme == 'default') {
93           $build['#attached']['library'][] = 'diff/diff.default';
94         }
95         elseif ($theme == 'github') {
96           $build['#attached']['library'][] = 'diff/diff.github';
97         }
98       }
99       // If the setting could not be loaded or is missing use the default theme.
100       elseif ($theme == NULL) {
101         $build['#attached']['library'][] = 'diff/diff.github';
102       }
103       return $build;
104     }
105     return [
106       '#markup' => $this->t('Cannot hash binary file or file not found: %file', array('%file' => $file))
107     ];
108   }
109
110   /**
111    * Menu title callback for the hacked site report page.
112    */
113   public function hackedProjectDiffTitle(hackedProject $project) {
114     $file = \Drupal::request()->get('file');
115     return $this->t('Hacked status for @file in project @project', [
116       '@project' => $project->title(),
117       '@file'    => $file,
118     ]);
119   }
120
121 }