Completed
Pull Request — master (#85)
by
unknown
02:41
created

DrupalDriver::userAlter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Drupal\Driver;
4
5
use Drupal\Driver\Exception\BootstrapException;
6
7
use Behat\Behat\Tester\Exception\PendingException;
8
9
/**
10
 * Fully bootstraps Drupal and uses native API calls.
11
 */
12
class DrupalDriver implements DriverInterface, SubDriverFinderInterface {
13
14
  /**
15
   * Track whether Drupal has been bootstrapped.
16
   *
17
   * @var bool
18
   */
19
  private $bootstrapped = FALSE;
20
21
  /**
22
   * Drupal core object.
23
   *
24
   * @var \Drupal\Driver\Cores\CoreInterface
25
   */
26
  public $core;
27
28
  /**
29
   * System path to the Drupal installation.
30
   *
31
   * @var string
32
   */
33
  private $drupalRoot;
34
35
  /**
36
   * URI for the Drupal installation.
37
   *
38
   * @var string
39
   */
40
  private $uri;
41
42
  /**
43
   * Drupal core version.
44
   *
45
   * @var integer
46
   */
47
  public $version;
48
49
  /**
50
   * Set Drupal root and URI.
51
   *
52
   * @param string $drupal_root
53
   *   The Drupal root path.
54
   * @param string $uri
55
   *   The URI for the Drupal installation.
56
   *
57
   * @throws BootstrapException
58
   *   Thrown when the Drupal installation is not found in the given root path.
59
   */
60
  public function __construct($drupal_root, $uri) {
61
    $this->drupalRoot = realpath($drupal_root);
62
    if (!$this->drupalRoot) {
63
      throw new BootstrapException(sprintf('No Drupal installation found at %s', $drupal_root));
64
    }
65
    $this->uri = $uri;
66
    $this->version = $this->getDrupalVersion();
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public function getRandom() {
73
    return $this->getCore()->getRandom();
74
  }
75
76
  /**
77
   * {@inheritdoc}
78
   */
79
  public function bootstrap() {
80
    $this->getCore()->bootstrap();
81
    $this->bootstrapped = TRUE;
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function isBootstrapped() {
88
    // Assume the blackbox is always bootstrapped.
89
    return $this->bootstrapped;
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  public function userCreate(\stdClass $user) {
96
    $this->getCore()->userCreate($user);
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function userAlter($user, $values) {
103
    return $this->getCore()->userAlter($user, $values);
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function userDelete(\stdClass $user) {
110
    $this->getCore()->userDelete($user);
111
  }
112
113
  /**
114
   * {@inheritdoc}
115
   */
116
  public function processBatch() {
117
    $this->getCore()->processBatch();
118
  }
119
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function userAddRole(\stdClass $user, $role_name) {
124
    $this->getCore()->userAddRole($user, $role_name);
125
  }
126
127
  /**
128
   * {@inheritdoc}
129
   */
130
  public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) {
131
    throw new PendingException(sprintf('Currently no ability to access watchdog entries in %s', $this));
132
  }
133
134
  /**
135
   * {@inheritdoc}
136
   */
137
  public function clearCache($type = NULL) {
138
    $this->getCore()->clearCache();
139
  }
140
141
  /**
142
   * {@inheritdoc}
143
   */
144
  public function getSubDriverPaths() {
145
    // Ensure system is bootstrapped.
146
    if (!$this->isBootstrapped()) {
147
      $this->bootstrap();
148
    }
149
150
    return $this->getCore()->getExtensionPathList();
151
  }
152
153
  /**
154
   * Determine major Drupal version.
155
   *
156
   * @return int
157
   *   The major Drupal version.
158
   *
159
   * @throws \Drupal\Driver\Exception\BootstrapException
160
   *   Thrown when the Drupal version could not be determined.
161
   *
162
   * @see drush_drupal_version()
163
   */
164
  public function getDrupalVersion() {
165
    if (!isset($this->version)) {
166
      // Support 6, 7 and 8.
167
      $version_constant_paths = array(
168
        // Drupal 6.
169
        '/modules/system/system.module',
170
        // Drupal 7.
171
        '/includes/bootstrap.inc',
172
        // Drupal 8.
173
        '/autoload.php',
174
        '/core/includes/bootstrap.inc',
175
      );
176
177
      if ($this->drupalRoot === FALSE) {
178
        throw new BootstrapException('`drupal_root` parameter must be defined.');
179
      }
180
181
      foreach ($version_constant_paths as $path) {
182
        if (file_exists($this->drupalRoot . $path)) {
183
          require_once $this->drupalRoot . $path;
184
        }
185
      }
186
      if (defined('VERSION')) {
187
        $version = VERSION;
188
      }
189
      elseif (defined('\Drupal::VERSION')) {
190
        $version = \Drupal::VERSION;
191
      }
192
      else {
193
        throw new BootstrapException('Unable to determine Drupal core version. Supported versions are 6, 7, and 8.');
194
      }
195
196
      // Extract the major version from VERSION.
197
      $version_parts = explode('.', $version);
198
      if (is_numeric($version_parts[0])) {
199
        $this->version = (integer) $version_parts[0];
200
      }
201
      else {
202
        throw new BootstrapException(sprintf('Unable to extract major Drupal core version from version string %s.', $version));
203
      }
204
    }
205
    return $this->version;
206
  }
207
208
  /**
209
   * Instantiate and set Drupal core class.
210
   *
211
   * @param array $available_cores
212
   *   A major-version-keyed array of available core controllers.
213
   */
214
  public function setCore(array $available_cores) {
215
    if (!isset($available_cores[$this->version])) {
216
      throw new BootstrapException(sprintf('There is no available Drupal core controller for Drupal version %s.', $this->version));
217
    }
218
    $this->core = $available_cores[$this->version];
219
  }
220
221
  /**
222
   * Automatically set the core from the current version.
223
   */
224
  public function setCoreFromVersion() {
225
    $core = '\Drupal\Driver\Cores\Drupal' . $this->getDrupalVersion();
226
    $this->core = new $core($this->drupalRoot, $this->uri);
227
  }
228
229
  /**
230
   * Return current core.
231
   */
232
  public function getCore() {
233
    return $this->core;
234
  }
235
236
  /**
237
   * {@inheritdoc}
238
   */
239
  public function createNode($node) {
240
    return $this->getCore()->nodeCreate($node);
241
  }
242
243
  /**
244
   * {@inheritdoc}
245
   */
246
  public function nodeAlter($node, $values) {
247
    return $this->getCore()->nodeAlter($node, $values);
248
  }
249
250
  /**
251
   * {@inheritdoc}
252
   */
253
  public function nodeDelete($node) {
254
    return $this->getCore()->nodeDelete($node);
255
  }
256
257
  /**
258
   * {@inheritdoc}
259
   */
260
  public function runCron() {
261
    if (!$this->getCore()->runCron()) {
262
      throw new \Exception('Failed to run cron.');
263
    }
264
  }
265
266
  /**
267
   * {@inheritdoc}
268
   */
269
  public function createTerm(\stdClass $term) {
270
    return $this->getCore()->termCreate($term);
271
  }
272
273
  /**
274
   * {@inheritdoc}
275
   */
276
  public function termDelete(\stdClass $term) {
277
    return $this->getCore()->termDelete($term);
278
  }
279
280
  /**
281
   * {@inheritdoc}
282
   */
283
  public function roleCreate(array $permissions) {
284
    return $this->getCore()->roleCreate($permissions);
285
  }
286
287
  /**
288
   * {@inheritdoc}
289
   */
290
  public function roleDelete($rid) {
291
    $this->getCore()->roleDelete($rid);
292
  }
293
294
  /**
295
   * {@inheritdoc}
296
   */
297
  public function isField($entity_type, $field_name) {
298
    return $this->getCore()->isField($entity_type, $field_name);
299
  }
300
301
  /**
302
   * {@inheritdoc}
303
   */
304
  public function languageCreate($language) {
305
    return $this->getCore()->languageCreate($language);
306
  }
307
308
  /**
309
   * {@inheritdoc}
310
   */
311
  public function languageDelete($language) {
312
    $this->getCore()->languageDelete($language);
313
  }
314
315
  /**
316
   * {@inheritdoc}
317
   */
318
  public function configGet($name, $key) {
319
    return $this->getCore()->configGet($name, $key);
320
  }
321
322
  /**
323
   * {@inheritdoc}
324
   */
325
  public function configSet($name, $key, $value) {
326
    $this->getCore()->configSet($name, $key, $value);
327
  }
328
329
  /**
330
   * {@inheritdoc}
331
   */
332
  public function clearStaticCaches() {
333
    $this->getCore()->clearStaticCaches();
334
  }
335
336
  /**
337
   * {@inheritdoc}
338
   */
339
  public function nodeDeleteMultiple(array $nids) {
340
    $this->getCore()->nodeDeleteMultiple($nids);
341
  }
342
343
  /**
344
   * {@inheritdoc}
345
   */
346
  public function userDeleteMultiple(array $uids) {
347
    $this->getCore()->userDeleteMultiple($uids);
348
  }
349
350
}
351