ReadinessCheckerManager.php 3.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

/**
 * Defines a chained readiness checker implementation combining multiple checks.
 */
class ReadinessCheckerManager {

  /**
   * An unsorted array of arrays of active checkers.
   *
   * An associative array. The keys are integers that indicate priority. Values
   * are arrays of ReadinessCheckerInterface objects.
   *
   * @var ReadinessCheckerInterface[][]
   */
  protected static $checkers = [];

  /**
   * Get checkers.
   *
   * @return array
   *   The registered checkers.
   */
  protected static function getCheckers() {
25
    static::$checkers['warning'][0][] = 'BlacklistPhp72Versions';
26
    static::$checkers['warning'][0][] = 'CronFrequency';
27
    static::$checkers['warning'][0][] = 'FileOwnership';
28
    static::$checkers['warning'][0][] = 'MissingProjectInfo';
29
    static::$checkers['warning'][0][] = 'ModifiedFiles';
30
    static::$checkers['warning'][0][] = 'PhpSapi';
31
    static::$checkers['error'][0][] = 'DiskSpace';
32
    static::$checkers['error'][0][] = 'OpcodeCache';
33
    static::$checkers['error'][0][] = 'PendingDbUpdates';
34
    static::$checkers['error'][0][] = 'ReadOnlyFilesystem';
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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

    return static::$checkers;
  }

  /**
   * Run checks.
   *
   * @param string $category
   *   The category of check.
   *
   * @return array
   *   An array of translatable strings.
   */
  public static function run($category) {
    $messages = [];
    if (!static::isEnabled()) {
      return $messages;
    }
    if (!isset(static::getSortedCheckers()[$category])) {
      throw new \InvalidArgumentException(sprintf('No readiness checkers exist of category "%s"', $category));
    }

    foreach (static::getSortedCheckers()[$category] as $checker) {
      $messages = array_merge($messages, $checker::run());
    }
    // Guard against variable_set stampede by checking if the values have
    // changed since previous run.
    $previous_messages = variable_get("automatic_updates.readiness_check_results.$category");
    if ($previous_messages !== $messages) {
      variable_set("automatic_updates.readiness_check_results.$category", $messages);
    }
    if (variable_get('automatic_updates.readiness_check_timestamp') !== REQUEST_TIME) {
      variable_set('automatic_updates.readiness_check_timestamp', REQUEST_TIME);
    }
    return $messages;
  }

  /**
   * {@inheritdoc}
   */
  public static function getResults($category) {
    $results = [];
    if (static::isEnabled()) {
      $results = variable_get("automatic_updates.readiness_check_results.$category", []);
    }
    return $results;
  }

  /**
   * {@inheritdoc}
   */
  public static function timestamp() {
    return variable_get('automatic_updates.readiness_check_timestamp', 0);
  }

  /**
   * {@inheritdoc}
   */
  public static function isEnabled() {
    return variable_get('automatic_updates_enable_readiness_checks', TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public static function getCategories() {
    return ['warning', 'error'];
  }

  /**
   * Sorts checkers according to priority.
   *
   * @return ReadinessCheckerInterface[]
   *   A sorted array of checker objects.
   */
  protected static function getSortedCheckers() {
    $sorted = [];
    foreach (static::getCheckers() as $category => $priorities) {
      foreach ($priorities as $checkers) {
        krsort($checkers);
        $sorted[$category] = isset($sorted[$category]) ? array_merge($sorted[$category], $checkers) : array_merge([], $checkers);
      }
    }
    return $sorted;
  }

}