-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMountedSandboxBootstrap.php
More file actions
183 lines (152 loc) · 5.19 KB
/
Copy pathMountedSandboxBootstrap.php
File metadata and controls
183 lines (152 loc) · 5.19 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
<?php
/**
* Self-configuration for mounted sandbox runtimes.
*
* @package DataMachineCode\Runtime
*/
namespace DataMachineCode\Runtime;
use DataMachineCode\Abilities\WorkspaceAbilities;
defined('ABSPATH') || exit;
final class MountedSandboxBootstrap {
private const DEFAULT_WORKSPACE_ROOT = '/workspace';
/** @var array<string,mixed> */
private static array $context = array();
public static function register(): void {
add_action('mounted_runtime_bootstrap', array( self::class, 'configure' ), 10, 1);
add_action('wordpress_runtime_bootstrap', array( self::class, 'configure' ), 10, 1);
$context = self::discover_context();
if ( self::should_configure($context) ) {
self::configure($context);
}
}
/**
* Configure DMC from a generic mounted-runtime context.
*
* @param array<string,mixed> $context Runtime context supplied by the sandbox substrate.
*/
public static function configure( array $context = array() ): void {
self::$context = $context;
self::configure_workspace_root($context);
self::force_mounted_workspace_backend();
add_action('wp_abilities_api_init', array( self::class, 'adopt_workspace_mounts' ), 100);
}
/** @return array<string,mixed> */
public static function context(): array {
return self::$context;
}
/** @return array<string,mixed> */
private static function discover_context(): array {
foreach ( array( 'mounted_runtime_context', 'wordpress_runtime_context' ) as $global_key ) {
$context = $GLOBALS[ $global_key ] ?? null;
if ( is_array($context) ) {
return $context;
}
}
$encoded = getenv('MOUNTED_RUNTIME_CONTEXT');
if ( is_string($encoded) && '' !== $encoded ) {
$decoded = json_decode($encoded, true);
if ( is_array($decoded) ) {
return $decoded;
}
}
$workspace_root = getenv('MOUNTED_RUNTIME_WORKSPACE_ROOT');
if ( is_string($workspace_root) && '' !== $workspace_root ) {
return array( 'workspace_root' => $workspace_root );
}
return is_dir(self::DEFAULT_WORKSPACE_ROOT)
? array( 'workspace_root' => self::DEFAULT_WORKSPACE_ROOT )
: array();
}
/** @param array<string,mixed> $context */
private static function should_configure( array $context ): bool {
return '' !== self::workspace_root_from_context($context);
}
/** @param array<string,mixed> $context */
private static function configure_workspace_root( array $context ): void {
if ( defined('DATAMACHINE_WORKSPACE_PATH') ) {
return;
}
$root = self::workspace_root_from_context($context);
if ( '' === $root ) {
return;
}
if ( ! is_dir($root) ) {
if ( function_exists('wp_mkdir_p') ) {
wp_mkdir_p($root);
} else {
@mkdir($root, 0775, true); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
}
}
if ( is_dir($root) ) {
define('DATAMACHINE_WORKSPACE_PATH', rtrim($root, '/'));
}
}
private static function force_mounted_workspace_backend(): void {
add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100);
}
public static function adopt_workspace_mounts(): void {
if ( ! defined('DATAMACHINE_WORKSPACE_PATH') || ! class_exists(WorkspaceAbilities::class) ) {
return;
}
$workspace_root = rtrim( (string) DATAMACHINE_WORKSPACE_PATH, '/');
$mounts = self::workspace_mounts(self::$context, $workspace_root);
foreach ( $mounts as $mount ) {
if ( ! empty($mount['repo_backed']) ) {
continue;
}
$path = (string) $mount['path'];
if ( '' === $path || ! is_dir($path) || ! is_dir($path . '/.git') ) {
continue;
}
WorkspaceAbilities::adoptRepo(
array(
'path' => $path,
'name' => basename($path),
)
);
}
}
/** @param array<string,mixed> $context */
private static function workspace_root_from_context( array $context ): string {
$root = isset($context['workspace_root']) ? (string) $context['workspace_root'] : '';
if ( '' === $root ) {
$workspace = is_array($context['sandbox_workspace'] ?? null) ? $context['sandbox_workspace'] : array();
$root = isset($workspace['root']) ? (string) $workspace['root'] : '';
}
return '' !== $root ? rtrim($root, '/') : '';
}
/**
* @param array<string,mixed> $context
* @return array<int,array{path:string,repo_backed:bool}>
*/
private static function workspace_mounts( array $context, string $workspace_root ): array {
$workspace = is_array($context['sandbox_workspace'] ?? null) ? $context['sandbox_workspace'] : array();
$mounts = array();
if ( is_array($workspace['mounts'] ?? null) ) {
foreach ( $workspace['mounts'] as $mount ) {
if ( ! is_array($mount) ) {
continue;
}
$target = rtrim( (string) ( $mount['target'] ?? '' ), '/');
if ( '' === $target || 0 !== strpos($target . '/', $workspace_root . '/') ) {
continue;
}
$mounts[] = array(
'path' => $target,
'repo_backed' => 'repo-backed' === (string) ( $mount['sourceMode'] ?? '' ),
);
}
}
if ( ! empty($mounts) ) {
return $mounts;
}
$paths = glob($workspace_root . '/*', GLOB_ONLYDIR);
foreach ( false !== $paths ? $paths : array() as $path ) {
$mounts[] = array(
'path' => $path,
'repo_backed' => is_file($path . '/.git'),
);
}
return $mounts;
}
}