opcache.drush.inc::opcache_drush_command()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @file
4
 * Contains Drush commands and hook implementations for the OPcache module.
5
 */
6
7
use OPCache\OPCache;
8
use OPCache\OPCacheConfiguration;
9
use OPCache\OPCacheStatus;
10
11
/**
12
 * Implements hook_drush_command().
13
 */
14
function opcache_drush_command() {
15
  $commands = array(
16
    'opcache-configuration' => array(
17
      'description' => dt('Get current OPcache configuration.'),
18
    ),
19
    'opcache-invalidate' => array(
20
      'description' => dt('Invalidate scripts in OPcache.'),
21
      'arguments' => array(
22
        'scripts' => dt('Required. A space-delimited list of scripts to invalidate in OPcache.'),
23
      ),
24
      'required-arguments' => 1,
25
    ),
26
    'opcache-reset' => array(
27
      'description' => dt('Reset OPcache.'),
28
      'options' => array(
29
        'all' => dt('Clear entire OPcache on server(s).'),
30
      ),
31
    ),
32
    'opcache-status' => array(
33
      'description' => dt('Get current OPcache status.'),
34
    ),
35
  );
36
37
  return $commands;
38
}
39
40
/**
41
 * Implements hook_drush_cache_clear().
42
 *
43
 * Presents the OPcache as a "clearable" cache to Drush, so it appears in the
44
 * select list of caches when one runs `drush cache-clear`. The callback is
45
 * invoked when one selects "opcache" form that list or simply runs `drush cc
46
 * opcache`.
47
 */
48
function opcache_drush_cache_clear(&$types) {
49
  $types['opcache'] = 'drush_opcache_reset';
50
}
51
52
/**
53
 * Callback to invalidate scripts in OPcache.
54
 *
55
 * @see opcache_drush_command()
56
 */
57
function drush_opcache_invalidate() {
58
  $opcache = new OPCache();
59
  $args = func_get_args();
60
  if (count($args) > 0) {
61
    foreach ($args as $script) {
62
      $opcache->drushInvalidate($script);
63
    }
64
  }
65
}
66
67
/**
68
 * Callback to reset ("clear") OPcache.
69
 *
70
 * @see opcache_drush_cache_clear()
71
 */
72
function drush_opcache_reset() {
73
  $all = drush_get_option('all');
74
  $opcache = new OPCache();
75
  $opcache->drushReset($all);
76
}
77
78
/**
79
 * Implements… actually, I'm not sure what this implements.
80
 *
81
 * In the case of a `drush cc all`, Drush will clear its own cache, then tell
82
 * Drupal to clear all its caches - in the CLI context. Our implementation of
83
 * hook_flush_caches() will not even bother if drupal_is_cli() == TRUE. However,
84
 * Drush will invoke drush_[all_modules]_cache_clear(), so we'll use an
85
 * implementation of that to see if the "all" cache was cleared, and reset the
86
 * OPcache if that's the case. This should be the least surprising behavior.
87
 */
88
function drush_opcache_cache_clear($type = NULL) {
89
  if ($type === 'all') {
90
    drush_opcache_reset();
91
  }
92
}
93
94
function drush_opcache_status() {
95
  $opcache = new OPCache();
96
  $table = array();
97
  $status = json_decode($opcache->drushStatus());
98
99
  drush_print('Summary:');
100
  $table[] = array('ENABLED', 'CACHE FULL', 'RESTART PENDING', 'RESTART IN PROGRESS');
101
  $table[] = array(
102
    ($status->opcache_enabled) ? 'YES' : 'NO',
103
    ($status->cache_full) ? 'YES' : 'NO',
104
    ($status->restart_pending) ? 'YES' : 'NO',
105
    ($status->restart_in_progress) ? 'YES' : 'NO',
106
  );
107
  drush_print_table($table, TRUE);
108
109
  $table = array();
110
  drush_print('Memory Usage:');
111
  $table[] = array('USED', 'FREE', 'WASTED', 'WASTED %');
112
  $table[] = array(
113
    drush_format_size($status->memory_usage->used_memory),
114
    drush_format_size($status->memory_usage->free_memory),
115
    drush_format_size($status->memory_usage->wasted_memory),
116
    round($status->memory_usage->current_wasted_percentage) . '%',
117
  );
118
  drush_print_table($table, TRUE);
119
120
  $table = array();
121
  drush_print('Interned Strings Usage:');
122
  $table[] = array('BUFFER SIZE', 'USED MEMORY', 'FREE MEMORY', 'NUMBER OF STRINGS');
123
  $table[] = array(
124
    drush_format_size($status->interned_strings_usage->buffer_size),
125
    drush_format_size($status->interned_strings_usage->used_memory),
126
    drush_format_size($status->interned_strings_usage->free_memory),
127
    $status->interned_strings_usage->number_of_strings,
128
  );
129
  drush_print_table($table, TRUE);
130
131
  $table = array();
132
  drush_print('Statistics:');
133
  $table[] = array('Number of Cached Scripts', $status->opcache_statistics->num_cached_scripts);
134
  $table[] = array('Number of Cached Keys', $status->opcache_statistics->num_cached_keys);
135
  $table[] = array('Maximum Cached Keys', $status->opcache_statistics->max_cached_keys);
136
  $table[] = array('Hits', $status->opcache_statistics->hits);
137
  $table[] = array('Start Time', $status->opcache_statistics->start_time);
138
  $table[] = array('Last Restart Time', $status->opcache_statistics->last_restart_time);
139
  $table[] = array('OOM Restarts', $status->opcache_statistics->oom_restarts);
140
  $table[] = array('Hash Restarts', $status->opcache_statistics->hash_restarts);
141
  $table[] = array('Manual Restarts', $status->opcache_statistics->manual_restarts);
142
  $table[] = array('Misses', $status->opcache_statistics->misses);
143
  $table[] = array('Blacklist Misses', $status->opcache_statistics->blacklist_misses);
144
  $table[] = array('Blacklist Miss Ratio', $status->opcache_statistics->blacklist_miss_ratio);
145
  $table[] = array('Hit Rate', round($status->opcache_statistics->opcache_hit_rate, 3) . '%');
146
  drush_print_table($table, TRUE);
147
148
}
149
150