1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Cores; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DrupalKernel; |
6
|
|
|
use Drupal\Driver\Exception\BootstrapException; |
7
|
|
|
use Drupal\field\Entity\FieldStorageConfig; |
8
|
|
|
use Drupal\language\Entity\ConfigurableLanguage; |
9
|
|
|
use Drupal\node\Entity\Node; |
10
|
|
|
use Drupal\node\NodeInterface; |
11
|
|
|
use Drupal\taxonomy\Entity\Term; |
12
|
|
|
use Drupal\taxonomy\TermInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Drupal 8 core. |
17
|
|
|
*/ |
18
|
|
|
class Drupal8 extends AbstractCore { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function bootstrap() { |
24
|
|
|
// Validate, and prepare environment for Drupal bootstrap. |
25
|
|
|
if (!defined('DRUPAL_ROOT')) { |
26
|
|
|
define('DRUPAL_ROOT', $this->drupalRoot); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Bootstrap Drupal. |
30
|
|
|
chdir(DRUPAL_ROOT); |
31
|
|
|
$autoloader = require DRUPAL_ROOT . '/autoload.php'; |
32
|
|
|
require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc'; |
33
|
|
|
$this->validateDrupalSite(); |
34
|
|
|
|
35
|
|
|
$request = Request::createFromGlobals(); |
36
|
|
|
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); |
37
|
|
|
$kernel->boot(); |
38
|
|
|
$kernel->prepareLegacyRequest($request); |
39
|
|
|
|
40
|
|
|
// Initialise an anonymous session. required for the bootstrap. |
41
|
|
|
\Drupal::service('session_manager')->start(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function clearCache() { |
48
|
|
|
// Need to change into the Drupal root directory or the registry explodes. |
49
|
|
|
drupal_flush_all_caches(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function nodeCreate($node) { |
56
|
|
|
// Throw an exception if the node type is missing or does not exist. |
57
|
|
|
if (!isset($node->type) || !$node->type) { |
58
|
|
|
throw new \Exception("Cannot create content because it is missing the required property 'type'."); |
59
|
|
|
} |
60
|
|
|
$bundles = \Drupal::entityManager()->getBundleInfo('node'); |
61
|
|
|
if (!in_array($node->type, array_keys($bundles))) { |
62
|
|
|
throw new \Exception("Cannot create content because provided content type '$node->type' does not exist."); |
63
|
|
|
} |
64
|
|
|
// Default status to 1 if not set. |
65
|
|
|
if (!isset($node->status)) { |
66
|
|
|
$node->status = 1; |
67
|
|
|
} |
68
|
|
|
// If 'author' is set, remap it to 'uid'. |
69
|
|
|
if (isset($node->author)) { |
70
|
|
|
$user = user_load_by_name($node->author); |
71
|
|
|
if ($user) { |
72
|
|
|
$node->uid = $user->id(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
$this->expandEntityFields('node', $node); |
76
|
|
|
$entity = entity_create('node', (array) $node); |
77
|
|
|
$entity->save(); |
78
|
|
|
|
79
|
|
|
$node->nid = $entity->id(); |
80
|
|
|
|
81
|
|
|
return $node; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function nodeDelete($node) { |
88
|
|
|
$node = $node instanceof NodeInterface ? $node : Node::load($node->nid); |
|
|
|
|
89
|
|
|
if ($node instanceof NodeInterface) { |
|
|
|
|
90
|
|
|
$node->delete(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function nodeDeleteMultiple(array $nids) { |
98
|
|
|
foreach ($nids as $nid) { |
99
|
|
|
$node = Node::load($node->nid); |
|
|
|
|
100
|
|
|
$node->delete(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function runCron() { |
108
|
|
|
return \Drupal::service('cron')->run(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function userCreate(\stdClass $user) { |
115
|
|
|
$this->validateDrupalSite(); |
116
|
|
|
|
117
|
|
|
// Default status to TRUE if not explicitly creating a blocked user. |
118
|
|
|
if (!isset($user->status)) { |
119
|
|
|
$user->status = 1; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Clone user object, otherwise user_save() changes the password to the |
123
|
|
|
// hashed password. |
124
|
|
|
$this->expandEntityFields('user', $user); |
125
|
|
|
$account = entity_create('user', (array) $user); |
126
|
|
|
$account->save(); |
127
|
|
|
|
128
|
|
|
// Store UID. |
129
|
|
|
$user->uid = $account->id(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function roleCreate(array $permissions) { |
136
|
|
|
// Generate a random, lowercase machine name. |
137
|
|
|
$rid = strtolower($this->random->name(8, TRUE)); |
138
|
|
|
|
139
|
|
|
// Generate a random label. |
140
|
|
|
$name = trim($this->random->name(8, TRUE)); |
141
|
|
|
|
142
|
|
|
// Convert labels to machine names. |
143
|
|
|
$this->convertPermissions($permissions); |
144
|
|
|
|
145
|
|
|
// Check the all the permissions strings are valid. |
146
|
|
|
$this->checkPermissions($permissions); |
147
|
|
|
|
148
|
|
|
// Create new role. |
149
|
|
|
$role = entity_create('user_role', array( |
150
|
|
|
'id' => $rid, |
151
|
|
|
'label' => $name, |
152
|
|
|
)); |
153
|
|
|
$result = $role->save(); |
154
|
|
|
|
155
|
|
|
if ($result === SAVED_NEW) { |
156
|
|
|
// Grant the specified permissions to the role, if any. |
157
|
|
|
if (!empty($permissions)) { |
158
|
|
|
user_role_grant_permissions($role->id(), $permissions); |
159
|
|
|
} |
160
|
|
|
return $role->id(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
throw new \RuntimeException(sprintf('Failed to create a role with "%s" permission(s).', implode(', ', $permissions))); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function roleDelete($role_name) { |
170
|
|
|
$role = user_role_load($role_name); |
171
|
|
|
|
172
|
|
|
if (!$role) { |
173
|
|
|
throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$role->delete(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function processBatch() { |
183
|
|
|
$this->validateDrupalSite(); |
184
|
|
|
$batch =& batch_get(); |
185
|
|
|
$batch['progressive'] = FALSE; |
186
|
|
|
batch_process(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Retrieve all permissions. |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
* Array of all defined permissions. |
194
|
|
|
*/ |
195
|
|
|
protected function getAllPermissions() { |
196
|
|
|
$permissions = &drupal_static(__FUNCTION__); |
197
|
|
|
|
198
|
|
|
if (!isset($permissions)) { |
199
|
|
|
$permissions = \Drupal::service('user.permissions')->getPermissions(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $permissions; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Convert any permission labels to machine name. |
207
|
|
|
* |
208
|
|
|
* @param array &$permissions |
209
|
|
|
* Array of permission names. |
210
|
|
|
*/ |
211
|
|
|
protected function convertPermissions(array &$permissions) { |
212
|
|
|
$all_permissions = $this->getAllPermissions(); |
213
|
|
|
|
214
|
|
|
foreach ($all_permissions as $name => $definition) { |
215
|
|
|
$key = array_search($definition['title'], $permissions); |
216
|
|
|
if (FALSE !== $key) { |
217
|
|
|
$permissions[$key] = $name; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Check to make sure that the array of permissions are valid. |
224
|
|
|
* |
225
|
|
|
* @param array $permissions |
226
|
|
|
* Permissions to check. |
227
|
|
|
*/ |
228
|
|
|
protected function checkPermissions(array &$permissions) { |
229
|
|
|
$available = array_keys($this->getAllPermissions()); |
230
|
|
|
|
231
|
|
|
foreach ($permissions as $permission) { |
232
|
|
|
if (!in_array($permission, $available)) { |
233
|
|
|
throw new \RuntimeException(sprintf('Invalid permission "%s".', $permission)); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
|
|
public function userDelete(\stdClass $user) { |
242
|
|
|
user_cancel(array(), $user->uid, 'user_cancel_delete'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function userDeleteMultiple(array $uids) { |
249
|
|
|
foreach ($uids as $uid) { |
250
|
|
|
$user = new \stdClass(); |
251
|
|
|
$user->uid = $uid; |
252
|
|
|
$this->userDelete($user); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function userAddRole(\stdClass $user, $role_name) { |
260
|
|
|
// Allow both machine and human role names. |
261
|
|
|
$roles = user_role_names(); |
262
|
|
|
$id = array_search($role_name, $roles); |
263
|
|
|
if (FALSE !== $id) { |
264
|
|
|
$role_name = $id; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if (!$role = user_role_load($role_name)) { |
268
|
|
|
throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$account = \user_load($user->uid); |
272
|
|
|
$account->addRole($role->id()); |
273
|
|
|
$account->save(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritdoc} |
278
|
|
|
*/ |
279
|
|
|
public function validateDrupalSite() { |
280
|
|
|
if ('default' !== $this->uri) { |
281
|
|
|
// Fake the necessary HTTP headers that Drupal needs: |
282
|
|
|
$drupal_base_url = parse_url($this->uri); |
283
|
|
|
// If there's no url scheme set, add http:// and re-parse the url |
284
|
|
|
// so the host and path values are set accurately. |
285
|
|
|
if (!array_key_exists('scheme', $drupal_base_url)) { |
286
|
|
|
$drupal_base_url = parse_url($this->uri); |
287
|
|
|
} |
288
|
|
|
// Fill in defaults. |
289
|
|
|
$drupal_base_url += array( |
290
|
|
|
'path' => NULL, |
291
|
|
|
'host' => NULL, |
292
|
|
|
'port' => NULL, |
293
|
|
|
); |
294
|
|
|
$_SERVER['HTTP_HOST'] = $drupal_base_url['host']; |
295
|
|
|
|
296
|
|
|
if ($drupal_base_url['port']) { |
297
|
|
|
$_SERVER['HTTP_HOST'] .= ':' . $drupal_base_url['port']; |
298
|
|
|
} |
299
|
|
|
$_SERVER['SERVER_PORT'] = $drupal_base_url['port']; |
300
|
|
|
|
301
|
|
|
if (array_key_exists('path', $drupal_base_url)) { |
302
|
|
|
$_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php'; |
303
|
|
|
} |
304
|
|
|
else { |
305
|
|
|
$_SERVER['PHP_SELF'] = '/index.php'; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
else { |
309
|
|
|
$_SERVER['HTTP_HOST'] = 'default'; |
310
|
|
|
$_SERVER['PHP_SELF'] = '/index.php'; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; |
314
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
315
|
|
|
$_SERVER['REQUEST_METHOD'] = NULL; |
316
|
|
|
|
317
|
|
|
$_SERVER['SERVER_SOFTWARE'] = NULL; |
318
|
|
|
$_SERVER['HTTP_USER_AGENT'] = NULL; |
319
|
|
|
|
320
|
|
|
$conf_path = DrupalKernel::findSitePath(Request::createFromGlobals()); |
321
|
|
|
$conf_file = $this->drupalRoot . "/$conf_path/settings.php"; |
322
|
|
|
if (!file_exists($conf_file)) { |
323
|
|
|
throw new BootstrapException(sprintf('Could not find a Drupal settings.php file at "%s"', $conf_file)); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* {@inheritdoc} |
329
|
|
|
*/ |
330
|
|
|
public function termCreate(\stdClass $term) { |
331
|
|
|
$term->vid = $term->vocabulary_machine_name; |
332
|
|
|
$this->expandEntityFields('taxonomy_term', $term); |
333
|
|
|
$entity = Term::create((array) $term); |
334
|
|
|
$entity->save(); |
335
|
|
|
|
336
|
|
|
$term->tid = $entity->id(); |
337
|
|
|
return $term; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* {@inheritdoc} |
342
|
|
|
*/ |
343
|
|
|
public function termDelete(\stdClass $term) { |
344
|
|
|
$term = $term instanceof TermInterface ? $term : Term::load($term->tid); |
|
|
|
|
345
|
|
|
if ($term instanceof TermInterface) { |
|
|
|
|
346
|
|
|
$term->delete(); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* {@inheritdoc} |
352
|
|
|
*/ |
353
|
|
|
public function getModuleList() { |
354
|
|
|
return array_keys(\Drupal::moduleHandler()->getModuleList()); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritdoc} |
359
|
|
|
*/ |
360
|
|
|
public function getExtensionPathList() { |
361
|
|
|
$paths = array(); |
362
|
|
|
|
363
|
|
|
// Get enabled modules. |
364
|
|
|
foreach (\Drupal::moduleHandler()->getModuleList() as $module) { |
365
|
|
|
$paths[] = $this->drupalRoot . DIRECTORY_SEPARATOR . $module->getPath(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $paths; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* {@inheritdoc} |
373
|
|
|
*/ |
374
|
|
|
public function getEntityFieldTypes($entity_type) { |
375
|
|
|
$return = array(); |
376
|
|
|
$fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type); |
377
|
|
|
foreach ($fields as $field_name => $field) { |
378
|
|
|
if ($this->isField($entity_type, $field_name)) { |
379
|
|
|
$return[$field_name] = $field->getType(); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
return $return; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* {@inheritdoc} |
387
|
|
|
*/ |
388
|
|
|
public function isField($entity_type, $field_name) { |
389
|
|
|
$fields = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type); |
390
|
|
|
return (isset($fields[$field_name]) && $fields[$field_name] instanceof FieldStorageConfig); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* {@inheritdoc} |
395
|
|
|
*/ |
396
|
|
|
public function languageCreate(\stdClass $language) { |
397
|
|
|
$langcode = $language->langcode; |
398
|
|
|
|
399
|
|
|
// Enable a language only if it has not been enabled already. |
400
|
|
|
if (!ConfigurableLanguage::load($langcode)) { |
401
|
|
|
$created_language = ConfigurableLanguage::createFromLangcode($language->langcode); |
402
|
|
|
if (!$created_language) { |
403
|
|
|
throw new InvalidArgumentException("There is no predefined language with langcode '{$langcode}'."); |
404
|
|
|
} |
405
|
|
|
$created_language->save(); |
406
|
|
|
return $language; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return FALSE; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* {@inheritdoc} |
414
|
|
|
*/ |
415
|
|
|
public function languageDelete(\stdClass $language) { |
416
|
|
|
$configurable_language = ConfigurableLanguage::load($language->langcode); |
417
|
|
|
$configurable_language->delete(); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* {@inheritdoc} |
422
|
|
|
*/ |
423
|
|
|
public function clearStaticCaches() { |
424
|
|
|
drupal_static_reset(); |
425
|
|
|
\Drupal::service('cache_tags.invalidator')->resetChecksums(); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* {@inheritdoc} |
430
|
|
|
*/ |
431
|
|
|
public function configGet($name, $key = '') { |
432
|
|
|
return \Drupal::config($name)->get($key); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* {@inheritdoc} |
437
|
|
|
*/ |
438
|
|
|
public function configSet($name, $key, $value) { |
439
|
|
|
\Drupal::configFactory()->getEditable($name) |
440
|
|
|
->set($key, $value) |
441
|
|
|
->save(); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.