Hooks.php 3.91 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\lightning_core\Commands;

5
6
7
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Drupal\Core\Extension\ProfileExtensionList;
8
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
9
use Drush\Commands\DrushCommands;
10
use Symfony\Component\Console\Event\ConsoleCommandEvent;
11
12
13
14
15
16

/**
 * Implements Drush command hooks.
 */
class Hooks extends DrushCommands {

17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /**
   * The profile extension list service.
   *
   * @var \Drupal\Core\Extension\ProfileExtensionList
   */
  private $profileList;

  /**
   * The install_profile parameter.
   *
   * @var string
   */
  private $installProfile;

31
32
33
34
35
36
37
  /**
   * The plugin cache clearer service.
   *
   * @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface
   */
  private $pluginCacheClearer;

38
39
40
41
42
43
44
  /**
   * Hooks constructor.
   *
   * @param \Drupal\Core\Extension\ProfileExtensionList $profile_list
   *   The profile extension list service.
   * @param string $install_profile
   *   The install_profile parameter.
45
46
   * @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
   *   The plugin cache clearer service.
47
   */
48
  public function __construct(ProfileExtensionList $profile_list, $install_profile, CachedDiscoveryClearerInterface $plugin_cache_clearer) {
49
50
    $this->profileList = $profile_list;
    $this->installProfile = $install_profile;
51
    $this->pluginCacheClearer = $plugin_cache_clearer;
52
53
  }

54
  /**
55
   * Clears all plugin caches before database updates begin.
56
   *
57
58
   * A common cause of errors during database updates is update hooks
   * inadvertently using stale data from the myriad caches in Drupal core and
59
60
61
62
63
64
65
   * contributed modules. To migitate this, we do a bit of cache pruning before
   * database updates begin.
   *
   * drupal_flush_all_caches() is extremely aggressive because it rebuilds the
   * router and other things, but it's a bit too much of a sledgehammer for our
   * purposes. A good compromise is to clear all plugin discovery caches (which
   * will include entity type definitions).
66
67
68
69
   *
   * @hook pre-command updatedb
   */
  public function preUpdate() {
70
    $this->pluginCacheClearer->clearCachedDefinitions();
71
72
  }

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  /**
   * Defines the base-profile field to the core:status command.
   *
   * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
   *   The command event.
   *
   * @see ::setBaseProfileFieldValue()
   *
   * @hook command-event core:status
   */
  public function defineExtraStatusFields(ConsoleCommandEvent $event) {
    $options = $event->getCommand()->getDefinition()->getOptions();

    $default_fields = $options['fields']->getDefault();
    $default_fields .= ',base-profile';
    $options['fields']->setDefault($default_fields);
  }

  /**
   * Sets the base-profile field value in core:status.
   *
   * @param mixed $result
   *   The result of the core:status command before alteration.
   * @param \Consolidation\AnnotatedCommand\CommandData $command_data
   *   The Drush command data.
   *
   * @see ::defineExtraStatusFields()
   *
   * @hook alter core:status
   */
  public function setBaseProfileFieldValue($result, CommandData $command_data) {
    $info = $this->profileList->get($this->installProfile);

    if (isset($info->info['base profile'])) {
      $base_profile = $info->info['base profile'];
    }
    else {
      $base_profile = t('This profile does not extend a base profile.');
    }

    $formatter_options = $command_data->formatterOptions();
    // Add a field label for our new 'base-profile' field. This is required
    // in order for this field to be selectable; otherwise, our structured
    // data formatters will remove it.
    $field_labels = $formatter_options->get(FormatterOptions::FIELD_LABELS);
    $field_labels['base-profile'] = t('Base Profile');
    $formatter_options->setFieldLabels($field_labels);
    // Add our data to the new field.
    $result['base-profile'] = $base_profile;
    return $result;
  }

125
}