|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Self-configuration for mounted sandbox runtimes. |
| 4 | + * |
| 5 | + * @package DataMachineCode\Runtime |
| 6 | + */ |
| 7 | + |
| 8 | +namespace DataMachineCode\Runtime; |
| 9 | + |
| 10 | +use DataMachineCode\Abilities\WorkspaceAbilities; |
| 11 | + |
| 12 | +defined('ABSPATH') || exit; |
| 13 | + |
| 14 | +final class MountedSandboxBootstrap { |
| 15 | + |
| 16 | + private const DEFAULT_WORKSPACE_ROOT = '/workspace'; |
| 17 | + |
| 18 | + /** @var array<string,mixed> */ |
| 19 | + private static array $context = array(); |
| 20 | + |
| 21 | + public static function register(): void { |
| 22 | + add_action('wp_codebox_sandbox_runtime_bootstrap', array( self::class, 'configure' ), 10, 1); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Configure DMC from a generic mounted-runtime context. |
| 27 | + * |
| 28 | + * @param array<string,mixed> $context Runtime context supplied by the sandbox substrate. |
| 29 | + */ |
| 30 | + public static function configure( array $context = array() ): void { |
| 31 | + self::$context = $context; |
| 32 | + self::configure_workspace_root($context); |
| 33 | + self::force_mounted_workspace_backend(); |
| 34 | + |
| 35 | + add_action('wp_abilities_api_init', array( self::class, 'adopt_workspace_mounts' ), 100); |
| 36 | + } |
| 37 | + |
| 38 | + /** @return array<string,mixed> */ |
| 39 | + public static function context(): array { |
| 40 | + return self::$context; |
| 41 | + } |
| 42 | + |
| 43 | + /** @param array<string,mixed> $context */ |
| 44 | + private static function configure_workspace_root( array $context ): void { |
| 45 | + if ( defined('DATAMACHINE_WORKSPACE_PATH') ) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $root = self::workspace_root_from_context($context); |
| 50 | + if ( '' === $root ) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if ( ! is_dir($root) ) { |
| 55 | + if ( function_exists('wp_mkdir_p') ) { |
| 56 | + wp_mkdir_p($root); |
| 57 | + } else { |
| 58 | + @mkdir($root, 0775, true); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if ( is_dir($root) ) { |
| 63 | + define('DATAMACHINE_WORKSPACE_PATH', rtrim($root, '/')); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static function force_mounted_workspace_backend(): void { |
| 68 | + add_filter('datamachine_code_remote_workspace_backend_should_handle', '__return_false', 100); |
| 69 | + } |
| 70 | + |
| 71 | + public static function adopt_workspace_mounts(): void { |
| 72 | + if ( ! defined('DATAMACHINE_WORKSPACE_PATH') || ! class_exists(WorkspaceAbilities::class) ) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $workspace_root = rtrim((string) DATAMACHINE_WORKSPACE_PATH, '/'); |
| 77 | + $mounts = self::workspace_mounts(self::$context, $workspace_root); |
| 78 | + foreach ( $mounts as $mount ) { |
| 79 | + if ( ! empty($mount['repo_backed']) ) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $path = (string) ( $mount['path'] ?? '' ); |
| 84 | + if ( '' === $path || ! is_dir($path) || ! is_dir($path . '/.git') ) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + WorkspaceAbilities::adoptRepo( |
| 89 | + array( |
| 90 | + 'path' => $path, |
| 91 | + 'name' => basename($path), |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** @param array<string,mixed> $context */ |
| 98 | + private static function workspace_root_from_context( array $context ): string { |
| 99 | + $root = isset($context['workspace_root']) ? (string) $context['workspace_root'] : ''; |
| 100 | + if ( '' === $root ) { |
| 101 | + $workspace = is_array($context['sandbox_workspace'] ?? null) ? $context['sandbox_workspace'] : array(); |
| 102 | + $root = isset($workspace['root']) ? (string) $workspace['root'] : ''; |
| 103 | + } |
| 104 | + |
| 105 | + return '' !== $root ? rtrim($root, '/') : self::DEFAULT_WORKSPACE_ROOT; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param array<string,mixed> $context |
| 110 | + * @return array<int,array{path:string,repo_backed:bool}> |
| 111 | + */ |
| 112 | + private static function workspace_mounts( array $context, string $workspace_root ): array { |
| 113 | + $workspace = is_array($context['sandbox_workspace'] ?? null) ? $context['sandbox_workspace'] : array(); |
| 114 | + $mounts = array(); |
| 115 | + if ( is_array($workspace['mounts'] ?? null) ) { |
| 116 | + foreach ( $workspace['mounts'] as $mount ) { |
| 117 | + if ( ! is_array($mount) ) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + $target = rtrim((string) ( $mount['target'] ?? '' ), '/'); |
| 121 | + if ( '' === $target || 0 !== strpos($target . '/', $workspace_root . '/') ) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + $mounts[] = array( |
| 125 | + 'path' => $target, |
| 126 | + 'repo_backed' => 'repo-backed' === (string) ( $mount['sourceMode'] ?? '' ), |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if ( ! empty($mounts) ) { |
| 132 | + return $mounts; |
| 133 | + } |
| 134 | + |
| 135 | + foreach ( glob($workspace_root . '/*', GLOB_ONLYDIR) ?: array() as $path ) { |
| 136 | + $mounts[] = array( |
| 137 | + 'path' => $path, |
| 138 | + 'repo_backed' => is_file($path . '/.git'), |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + return $mounts; |
| 143 | + } |
| 144 | +} |
0 commit comments