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