-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclass-v1tov2transpiler.php
More file actions
679 lines (648 loc) · 24.9 KB
/
class-v1tov2transpiler.php
File metadata and controls
679 lines (648 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<?php
namespace WordPress\Blueprints\Versions\Version1;
use VendorPrefix\Psr\Log\LoggerInterface;
use WordPress\Blueprints\Exception\BlueprintExecutionException;
use WordPress\Blueprints\Validator\HumanFriendlySchemaValidator;
use WordPress\Blueprints\Validator\ValidationError;
use function WordPress\Filesystem\wp_join_unix_paths;
/**
* @TODO: rewrite https://github.com urls to raw.githubusercontent.com urls like
* Blueprint v1 do. Maybe even do it in v2 runner in general?
*/
class V1ToV2Transpiler {
/**
* @var LoggerInterface
*/
private $logger;
public static function validate_v1_blueprint( array $v1 ): ?ValidationError {
$v = new HumanFriendlySchemaValidator(
json_decode( file_get_contents( __DIR__ . '/schema-v1.json' ), true )
);
// For every steps[] entry with step === "installPlugin" and "pluginZipFile", remove that key and rewrite it as "pluginData".
if ( isset( $v1['steps'] ) && is_array( $v1['steps'] ) ) {
foreach ( $v1['steps'] as &$step ) {
if ( ! is_array( $step ) || ! isset( $step['step'] ) ) {
continue;
}
if ( 'installPlugin' === $step['step'] && array_key_exists( 'pluginZipFile', $step ) ) {
// If pluginData is not already set, move pluginZipFile to pluginData.
if ( ! array_key_exists( 'pluginData', $step ) ) {
$step['pluginData'] = $step['pluginZipFile'];
}
unset( $step['pluginZipFile'] );
} elseif ( 'installTheme' === $step['step'] && array_key_exists( 'themeZipFile', $step ) ) {
// If themeData is not already set, move themeZipFile to themeData.
if ( ! array_key_exists( 'themeData', $step ) ) {
$step['themeData'] = $step['themeZipFile'];
}
unset( $step['themeZipFile'] );
} elseif ( 'importFile' === $step['step'] ) {
$step['step'] = 'importWxr';
}
}
unset( $step ); // break reference.
}
return $v->validate( $v1 );
}
public function __construct( LoggerInterface $logger ) {
$this->logger = $logger;
}
/**
* Upgrade a v1 Blueprint array to a v2 Blueprint array.
*
* @param array $validated_v1_blueprint
*
* @return array
* @throws BlueprintExecutionException When the v1 blueprint cannot be upgraded.
*/
public function upgrade( array $validated_v1_blueprint ): array {
$v1 = $validated_v1_blueprint;
$v2 = array(
'version' => 2,
);
$v2steps = array();
// Map $schema if present.
if ( isset( $v1['$schema'] ) ) {
$v2['$schema'] = $v1['$schema'];
}
// Map meta fields.
if ( isset( $v1['meta'] ) ) {
$v2['blueprintMeta'] = array();
if ( isset( $v1['meta']['title'] ) ) {
$v2['blueprintMeta']['name'] = $v1['meta']['title'];
}
if ( isset( $v1['meta']['description'] ) ) {
$v2['blueprintMeta']['description'] = $v1['meta']['description'];
}
if ( isset( $v1['meta']['categories'] ) ) {
$v2['blueprintMeta']['tags'] = $v1['meta']['categories'];
}
if ( isset( $v1['meta']['author'] ) ) {
$v2['blueprintMeta']['authors'] = array( $v1['meta']['author'] );
}
}
// Map preferredVersions.
if ( isset( $v1['preferredVersions'] ) ) {
$versions = $v1['preferredVersions'];
if ( isset( $versions['wp'] ) && 'latest' !== $versions['wp'] ) {
$v2['wordpressVersion'] = $versions['wp'];
}
if ( isset( $versions['php'] ) && 'latest' !== $versions['php'] ) {
$v2['phpVersion'] = $versions['php'];
}
}
// Unsupported fields.
// @TODO: Actually transpile a few of them:
// * features -> runtimeOptions.playground.features.
// -> or consider moving this to runtime configuration – as in
// permissions to access the network, disk, etc.
// * landingPage -> runtimeOptions.landingPage.
// * login -> runtimeOptions.login.
$unsupported_fields = array(
'features',
'landingPage',
'login',
'phpExtensionBundles',
);
$present_unsupported_fields = array();
foreach ( $unsupported_fields as $field ) {
if ( isset( $v1[ $field ] ) ) {
$present_unsupported_fields[] = $field;
}
}
if ( ! empty( $present_unsupported_fields ) ) {
$this->logger->warning(
sprintf(
'The following fields are not yet supported by the v1->v2 Blueprint transpiler and will be ignored: %s.',
implode( ', ', $present_unsupported_fields )
)
);
}
// SHORTHANDS:.
// Plugins.
if ( isset( $v1['plugins'] ) ) {
foreach ( $v1['plugins'] as $plugin ) {
$v2steps[] = array(
'step' => 'installPlugin',
'source' => self::convert_v1_resource_to_v2_reference( $plugin ),
);
}
}
// Constants.
if ( isset( $v1['constants'] ) ) {
$v2['constants'] = $v1['constants'];
}
// Site options.
if ( isset( $v1['siteOptions'] ) ) {
$v2['siteOptions'] = $v1['siteOptions'];
}
// STEPS:.
if ( isset( $v1['steps'] ) && is_array( $v1['steps'] ) ) {
foreach ( $v1['steps'] as $v1step ) {
switch ( $v1step['step'] ) {
case 'activatePlugin':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option is not supported on activatePlugin step and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2step = array(
'step' => 'activatePlugin',
'pluginPath' => $v1step['pluginPath'],
);
if ( isset( $v1step['humanReadableName'] ) ) {
$v2step['humanReadableName'] = $v1step['humanReadableName'];
}
$v2steps[] = $v2step;
break;
case 'activateTheme':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option is not supported on activateTheme step and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2step = array(
'step' => 'activateTheme',
'themeDirectoryName' => $v1step['themeFolderName'],
);
if ( isset( $v1step['humanReadableName'] ) ) {
$v2step['humanReadableName'] = $v1step['humanReadableName'];
}
$v2steps[] = $v2step;
break;
case 'cp':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option is not supported on cp step and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'cp',
'fromPath' => self::translate_path( $v1step['fromPath'] ),
'toPath' => self::translate_path( $v1step['toPath'] ),
);
break;
case 'defineWpConfigConsts':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option is not supported on defineWpConfigConsts step and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
if ( isset( $v1step['method'] ) ) {
$this->logger->warning( 'The `method` option is not supported on defineWpConfigConsts step and will be ignored: %s. Use the runtime configuration to set the method instead.' );
}
if ( isset( $v1step['virtualize'] ) ) {
$this->logger->warning( 'The `virtualize` option is not supported on defineWpConfigConsts step and will be ignored: %s. This option is deprecated and will be removed in a future version.' );
}
$v2steps[] = array(
'step' => 'defineConstants',
'constants' => $v1step['consts'],
);
break;
case 'defineSiteUrl':
$this->logger->warning( 'The `defineSiteUrl` step is not supported by the Blueprint v2 schema. Use the runner configuration to set the site URL instead.' );
break;
case 'enableMultisite':
$v2steps[] = array(
'step' => 'enableMultisite',
);
break;
case 'importWordPressFiles':
$this->logger->warning( 'The `importWordPressFiles` step is not supported by the Blueprint v2 schema. The entire step will be ignored.' );
break;
case 'runWpInstallationWizard':
$this->logger->warning( 'The `runWpInstallationWizard` step is not supported by the Blueprint v2 schema. Provide your WordPress export URL in the top-level "wordpressVersion" key and the runner will handle the installation automatically.' );
break;
case 'importWxr':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option is not supported on importWxr step and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'importContent',
'content' => array(
array(
'type' => 'wxr',
'source' => self::convert_v1_resource_to_v2_reference( $v1step['file'] ),
),
),
);
break;
case 'importThemeStarterContent':
// @TODO: We really need to support this in the v2 runner!
// Let's add an importContent step.
$this->logger->warning( 'The `importThemeStarterContent` step is not supported by the Blueprint v2 schema. Use the runner configuration to set the import behavior instead.' );
break;
case 'installPlugin':
if ( isset( $v1step['ifAlreadyInstalled'] ) ) {
$this->logger->warning(
sprintf(
'The `ifAlreadyInstalled` option is not yet supported by the v1->v2 Blueprint transpiler and will be ignored: %s. Use the runtime configuration to set the behavior instead.',
$v1step['ifAlreadyInstalled']
)
);
}
if ( isset( $v1step['progress']['weight'] ) ) {
$this->logger->warning( 'The `progress.weight` option is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2step = array(
'step' => 'installPlugin',
'source' => self::convert_v1_resource_to_v2_reference(
$v1step['pluginZipFile'] ??
$v1step['pluginData']
),
);
if ( isset( $v1step['progress']['caption'] ) ) {
// This isn't an exact tranlation but it will do.
// v1 caption would be "Installing Jetpack".
// v2 humanReadableName would be just "Jetpack".
$v2step['humanReadableName'] = $v1step['progress']['caption'];
}
// "activate" defaults to true in both v1 and v2.
if ( isset( $v1step['options']['activate'] ) ) {
$v2step['active'] = $v1step['options']['activate'];
}
if ( isset( $v1step['options']['targetFolderName'] ) ) {
$v2step['targetDirectoryName'] = $v1step['options']['targetFolderName'];
}
$v2steps[] = $v2step;
break;
case 'installTheme':
if ( isset( $v1step['ifAlreadyInstalled'] ) ) {
$this->logger->warning(
sprintf(
'The `ifAlreadyInstalled` option is not yet supported by the v1->v2 Blueprint transpiler and will be ignored: %s. Use the runtime configuration to set the behavior instead.',
$v1step['ifAlreadyInstalled']
)
);
}
if ( isset( $v1step['progress']['weight'] ) ) {
$this->logger->warning( 'The `progress.weight` option is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2step = array(
'step' => 'installTheme',
'source' => self::convert_v1_resource_to_v2_reference(
$v1step['themeData'] ??
$v1step['themeZipFile']
),
);
if ( isset( $v1step['progress']['caption'] ) ) {
$v2step['humanReadableName'] = $v1step['progress']['caption'];
}
if ( isset( $v1step['options']['importStarterContent'] ) ) {
$v2step['importStarterContent'] = $v1step['options']['importStarterContent'];
}
// "activate" defaults to true in both v1 and v2.
if ( isset( $v1step['options']['activate'] ) ) {
$v2step['active'] = $v1step['options']['activate'];
}
if ( isset( $v1step['options']['targetFolderName'] ) ) {
$v2step['targetDirectoryName'] = $v1step['options']['targetFolderName'];
}
$v2steps[] = $v2step;
break;
case 'login':
$this->logger->warning( 'The `login` step is not yet supported by the v1->v2 Blueprint transpiler and will be ignored.' );
break;
case 'mkdir':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on mkDir step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'mkdir',
'path' => self::translate_path( $v1step['path'] ),
);
break;
case 'mv':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on mv step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'mv',
'fromPath' => self::translate_path( $v1step['fromPath'] ),
'toPath' => self::translate_path( $v1step['toPath'] ),
);
break;
case 'request':
$this->logger->warning( 'The `request` step was deprecated in Blueprints v1 and is not supported anymore by Blueprints v2. Replace it with a wp-cli step or a runPHP step.' );
break;
case 'resetData':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on resetData step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'runPHP',
'code' => array(
'filename' => 'script.php',
'content' => <<<'PHP'
<?php
require getenv('DOCROOT') . '/wp-load.php';
$GLOBALS['@pdo']->query('DELETE FROM wp_posts WHERE id > 0');
$GLOBALS['@pdo']->query("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='wp_posts'");
$GLOBALS['@pdo']->query('DELETE FROM wp_postmeta WHERE post_id > 1');
$GLOBALS['@pdo']->query("UPDATE SQLITE_SEQUENCE SET SEQ=20 WHERE NAME='wp_postmeta'");
$GLOBALS['@pdo']->query('DELETE FROM wp_comments');
$GLOBALS['@pdo']->query("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='wp_comments'");
$GLOBALS['@pdo']->query('DELETE FROM wp_commentmeta');
$GLOBALS['@pdo']->query("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='wp_commentmeta'");
PHP
,
),
);
break;
case 'rm':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on rm step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'rm',
'path' => self::translate_path( $v1step['path'] ),
);
break;
case 'rmDir':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on rmDir step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'rmdir',
'path' => self::translate_path( $v1step['path'] ),
);
break;
case 'runPHP':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on runPHP step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'runPHP',
'code' => array(
'filename' => 'script.php',
'content' => self::convert_php_code( $v1step['code'] ),
),
);
break;
case 'runSQL':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on runSQL step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'runSQL',
'source' => array(
'filename' => 'script.sql',
'content' => $v1step['sql'],
),
);
break;
case 'setSiteLanguage':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on setSiteLanguage step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'setSiteLanguage',
'language' => $v1step['language'],
);
break;
case 'setSiteOptions':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on setSiteOptions step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'setSiteOptions',
'options' => $v1step['options'],
);
break;
case 'unzip':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on unzip step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'unzip',
'zipFile' => self::convert_v1_resource_to_v2_reference(
$v1step['zipPath'] ??
$v1step['zipFile']
),
'extractToPath' => self::translate_path( $v1step['extractToPath'] ),
);
break;
case 'updateUserMeta':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on updateUserMeta step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2steps[] = array(
'step' => 'runPHP',
'code' => array(
'filename' => 'script.php',
'content' => <<<'PHP'
<?php
include getenv("DOCROOT") . '/wp-load.php';
$meta = json_decode(getenv("META"), true);
foreach($meta as $name => $value) {
update_user_meta(getenv("USER_ID"), $name, $value);
}
?>
PHP
,
),
'env' => array(
'USER_ID' => $v1step['userId'] . '',
'META' => json_encode( $v1step['meta'] ),
),
);
break;
case 'writeFile':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on writeFile step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$path = self::translate_path( $v1step['path'] );
$v2steps[] = array(
'step' => 'writeFiles',
'files' => array(
$path => is_string( $v1step['data'] )
? array(
'filename' => basename( $path ),
'content' => str_ends_with( $path, '.php' )
? self::convert_php_code( $v1step['data'] )
: $v1step['data'],
)
: self::convert_v1_resource_to_v2_reference(
$v1step['data']
),
),
);
break;
case 'writeFiles':
if ( isset( $v1step['progress'] ) ) {
$this->logger->warning( 'The `progress` option on writeFiles step is not supported Blueprint v2 schema and will be ignored: %s. Use the runtime configuration to set the progress bar instead.' );
}
$v2step = array(
'step' => 'writeFiles',
'files' => array(),
);
// Prefix paths with "writeToPath".
// The rest of the data format is compliant with v2.
$base_path = self::translate_path( $v1step['writeToPath'] );
if ( isset( $v1step['filesTree']['resource'] ) ) {
$v2step['files']['/'] = self::convert_v1_resource_to_v2_reference( $v1step['filesTree'], $base_path );
} else {
foreach ( $v1step['filesTree'] as $path => $data ) {
$joined_path = wp_join_unix_paths( $base_path, $path );
$v2step['files'][ $joined_path ] = is_string( $data )
? array(
'filename' => basename( $path ),
'content' => str_ends_with( $path, '.php' )
? self::convert_php_code( $data )
: $data,
)
: self::convert_v1_resource_to_v2_reference(
$data
);
}
}
$v2steps[] = $v2step;
break;
case 'wp-cli':
// @TODO: Don't naively replace on the entire command. Actually parse it and only replace at the beginning
// of each argument value.
$cmd = str_replace( '/wordpress/', '', $v1step['command'] );
$cmd = str_replace( 'wordpress/', '', $cmd );
$v2steps[] = array(
'step' => 'wp-cli',
'command' => $cmd,
);
break;
default:
$this->logger->warning(
sprintf(
'The `%s` step is not yet supported by the v1->v2 Blueprint transpiler and will be ignored.',
$v1step['step']
)
);
break;
}
}
}
$v2['additionalStepsAfterExecution'] = $v2steps;
return $v2;
}
protected static function convert_v1_resource_to_v2_reference( $resource ) {
if ( is_string( $resource ) ) {
// Plugin or theme slug, preserve as-is.
return $resource;
} elseif ( is_array( $resource ) ) {
if ( ! isset( $resource['resource'] ) ) {
throw new BlueprintExecutionException( 'Missing resource type in ' . json_encode( $resource ) );
}
switch ( $resource['resource'] ) {
case 'literal':
// InlineFile.
return array(
'filename' => $resource['name'],
'content' => $resource['contents'],
);
case 'wordpress.org/themes':
// WordPressOrgThemeReference.
return $resource['slug'];
case 'wordpress.org/plugins':
// WordPressOrgPluginReference.
return $resource['slug'];
case 'vfs':
// TargetSitePath.
return 'site:' . $resource['path'];
case 'url':
// URLReference.
$url = $resource['url'];
// If it's a github.com URL, convert to raw.githubusercontent.com like WordPress Playground does.
if ( preg_match( '#^https://github\.com/([^/]+)/([^/]+)/(?:blob|raw)/(.+)$#', $url, $matches ) ) {
// e.g. https://github.com/user/repo/blob/branch/path/to/file
// => https://raw.githubusercontent.com/user/repo/branch/path/to/file.
$user = $matches[1];
$repo = $matches[2];
$rest = $matches[3];
// The first segment of $rest is the branch/ref.
$parts = explode( '/', $rest, 2 );
$ref = $parts[0];
$path = isset( $parts[1] ) ? $parts[1] : '';
$url = "https://raw.githubusercontent.com/$user/$repo/$ref/$path";
}
return $url;
case 'bundled':
// BundledReference – must start with
// ./ or /.
$path = $resource['path'];
if ( 0 !== strncmp( $path, './', strlen( './' ) ) && 0 !== strncmp( $path, '/', strlen( '/' ) ) ) {
$path = './' . $path;
}
return $path;
case 'literal:directory':
// InlineDirectory.
$files = array();
foreach ( $resource['files'] as $name => $file ) {
if ( is_string( $file ) ) {
$files[ $name ] = $file;
} else {
$files[ $name ] = self::convert_v1_resource_to_v2_reference( $file );
}
}
return array(
'directoryName' => $resource['name'],
'files' => $files,
);
case 'git:directory':
// GitDirectoryReference.
return array(
'gitRepository' => $resource['url'],
'pathInRepository' => $resource['path'],
'ref' => $resource['ref'],
);
default:
throw new BlueprintExecutionException( 'Unknown resource type: ' . $resource['resource'] );
}
}
}
protected static function translate_path( $path ) {
// V1 Blueprint paths are absolute.
if ( 0 === strncmp( $path, '/wordpress/', strlen( '/wordpress/' ) ) ) {
return substr( $path, strlen( '/wordpress/' ) );
}
if ( 0 === strncmp( $path, 'wordpress/', strlen( 'wordpress/' ) ) ) {
return substr( $path, strlen( 'wordpress/' ) );
}
return $path;
}
protected static function convert_php_code( $code ) {
$had_php_tag = '<?php' === substr( $code, 0, 5 );
if ( ! $had_php_tag ) {
$code = '<?php ' . $code;
}
$tokens = token_get_all( $code );
$converted_code = '';
foreach ( $tokens as $token ) {
if ( ! is_array( $token ) ) {
$converted_code .= $token;
}
[ $id, $text ] = $token;
switch ( $id ) {
case T_CONSTANT_ENCAPSED_STRING:
// Support both single and double quoted strings.
$quote = $text[0];
$unquoted = substr( $text, 1, -1 );
if (
(
( "'" === $quote || '"' === $quote )
&& 0 === strncmp( $unquoted, '/wordpress/', strlen( '/wordpress/' ) )
)
) {
$converted_code .= 'getenv(\'DOCROOT\') . ' . var_export( substr( $unquoted, strlen( '/wordpress' ) ), true );
} elseif (
(
( "'" === $quote || '"' === $quote )
&& 0 === strncmp( $unquoted, 'wordpress/', strlen( 'wordpress/' ) )
)
) {
$converted_code .= 'getenv(\'DOCROOT\') . ' . var_export( substr( $unquoted, strlen( 'WordPress' ) ), true );
} else {
$converted_code .= $text;
}
break;
default:
$converted_code .= $text;
break;
}
}
$converted_code = trim( $converted_code );
if ( ! $had_php_tag && '<?php' === substr( $converted_code, 0, 5 ) ) {
$converted_code = substr( $converted_code, 5 ); // Remove the initial '<?php' added for tokenization.
}
return $converted_code;
}
}