Completed
Pull Request — master (#76)
by
unknown
02:19
created

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