Skip to content

Commit 879ff49

Browse files
authored
Add post-runtime browser blueprint steps (#1747)
1 parent fa1a5cf commit 879ff49

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

packages/wordpress-plugin/src/trait-wp-codebox-abilities-browser-blueprint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ private static function browser_blueprint_with_site_artifact( array $blueprint,
4646
return $merged;
4747
}
4848

49+
/** @param array<string,mixed> $blueprint Runtime-materialized blueprint. @param array<string,mixed> $post_runtime_blueprint Caller steps that must run after runtime dependencies. @return array<string,mixed> */
50+
private static function browser_blueprint_with_post_runtime( array $blueprint, array $post_runtime_blueprint ): array {
51+
if ( empty( $post_runtime_blueprint ) ) {
52+
return $blueprint;
53+
}
54+
55+
$steps = is_array( $blueprint['steps'] ?? null ) ? $blueprint['steps'] : array();
56+
$post_steps = is_array( $post_runtime_blueprint['steps'] ?? null ) ? $post_runtime_blueprint['steps'] : array();
57+
$merged = array_merge( $blueprint, $post_runtime_blueprint );
58+
$merged['steps'] = array_values( array_merge( $steps, $post_steps ) );
59+
60+
if ( isset( $blueprint['features'] ) && isset( $post_runtime_blueprint['features'] ) && is_array( $blueprint['features'] ) && is_array( $post_runtime_blueprint['features'] ) ) {
61+
$merged['features'] = array_merge( $blueprint['features'], $post_runtime_blueprint['features'] );
62+
}
63+
64+
return $merged;
65+
}
66+
4967
/** @param array<string,mixed> $blueprint Blueprint override. @param array<string,mixed> $runtime Runtime dependency specs. @return array<string,mixed> */
5068
private static function browser_blueprint_with_runtime( array $blueprint, array $runtime, array $playground = array() ): array {
5169
$steps = is_array( $blueprint['steps'] ?? null ) ? $blueprint['steps'] : array();

packages/wordpress-plugin/src/trait-wp-codebox-abilities-execution.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public static function create_browser_playground_session( array $input ): array|
188188

189189
$base_blueprint = self::browser_blueprint_with_site_artifact( is_array( $input['blueprint'] ?? null ) ? $input['blueprint'] : array(), $site_blueprint_artifact );
190190
$blueprint = self::browser_blueprint_with_runtime( $base_blueprint, $runtime, $playground );
191+
$blueprint = self::browser_blueprint_with_post_runtime( $blueprint, is_array( $input['post_runtime_blueprint'] ?? null ) ? $input['post_runtime_blueprint'] : array() );
191192
$prepared_runtime = self::browser_prepared_runtime_with_blueprints( is_array( $runtime['prepared_runtime'] ?? null ) ? $runtime['prepared_runtime'] : array(), $blueprint, $playground );
192193
$runtime['prepared_runtime'] = $prepared_runtime;
193194
$contained_site = self::browser_contained_site_envelope( $input, $session_id, $playground, $runtime, $prepared_runtime, 'ready' );

packages/wordpress-plugin/src/trait-wp-codebox-abilities-schemas.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ private static function wordpress_workload_run_request_schema(): array {
755755
'schema' => array( 'type' => 'string', 'const' => 'wp-codebox/wordpress-workload-run/v1' ),
756756
'wordpress_version' => self::string_property_schema( 'WordPress version requested for the disposable Playground workload.' ),
757757
'blueprint' => self::object_property_schema( 'Optional WordPress Playground blueprint object.' ),
758+
'post_runtime_blueprint' => self::object_property_schema( 'Optional WordPress Playground blueprint steps to run after runtime dependencies are materialized.' ),
758759
'preview' => self::object_property_schema( 'Optional preview settings for the workload.' ),
759760
'mounts' => self::mount_schema(),
760761
'runtime_stack_mounts' => self::object_array_property_schema( 'Runtime stack mounts for the disposable workload.' ),
@@ -1041,6 +1042,7 @@ private static function browser_task_input_properties( array $task_input_schema,
10411042
'runtime_profile' => self::object_property_schema( $detailed ? 'Portable wp-codebox/runtime-profile/v1 descriptor normalized into browser runtime fields before session creation.' : '' ),
10421043
'runtime' => $detailed ? self::browser_runtime_input_schema() : self::object_property_schema(),
10431044
'blueprint' => self::object_property_schema( $detailed ? 'Optional WordPress Playground blueprint for the browser to compile and run.' : '' ),
1045+
'post_runtime_blueprint' => self::object_property_schema( $detailed ? 'Optional WordPress Playground blueprint steps to run after runtime dependencies are materialized.' : '' ),
10441046
'site_blueprint_artifact' => $detailed ? self::site_blueprint_artifact_input_schema() : self::object_property_schema(),
10451047
'artifact_files' => $detailed ? self::artifact_files_input_schema() : array( 'type' => 'array' ),
10461048
'debug' => array(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
define( 'ABSPATH', __DIR__ );
4+
5+
require_once __DIR__ . '/../packages/wordpress-plugin/src/trait-wp-codebox-abilities-browser-blueprint.php';
6+
7+
final class WP_Codebox_Post_Runtime_Blueprint_Smoke {
8+
use WP_Codebox_Abilities_Browser_Blueprint;
9+
10+
/** @param array<string,mixed> $blueprint @param array<string,mixed> $post_runtime @return array<string,mixed> */
11+
public static function merge( array $blueprint, array $post_runtime ): array {
12+
$method = new ReflectionMethod( self::class, 'browser_blueprint_with_post_runtime' );
13+
return $method->invoke( null, $blueprint, $post_runtime );
14+
}
15+
}
16+
17+
$runtime_blueprint = array(
18+
'preferredVersions' => array( 'php' => '8.3' ),
19+
'features' => array( 'networking' => false ),
20+
'steps' => array(
21+
array( 'step' => 'login' ),
22+
array( 'step' => 'installPlugin', 'pluginData' => array( 'url' => 'runtime.zip' ) ),
23+
),
24+
);
25+
$post_runtime = array(
26+
'features' => array( 'networking' => true ),
27+
'steps' => array( array( 'step' => 'runPHP', 'code' => '<?php import_site();' ) ),
28+
);
29+
30+
$merged = WP_Codebox_Post_Runtime_Blueprint_Smoke::merge( $runtime_blueprint, $post_runtime );
31+
$steps = array_column( $merged['steps'] ?? array(), 'step' );
32+
33+
if ( array( 'login', 'installPlugin', 'runPHP' ) !== $steps ) {
34+
fwrite( STDERR, 'Post-runtime blueprint steps were not appended after runtime materialization.' . PHP_EOL );
35+
exit( 1 );
36+
}
37+
if ( true !== ( $merged['features']['networking'] ?? null ) ) {
38+
fwrite( STDERR, 'Post-runtime blueprint features did not override the base feature.' . PHP_EOL );
39+
exit( 1 );
40+
}
41+
if ( $runtime_blueprint !== WP_Codebox_Post_Runtime_Blueprint_Smoke::merge( $runtime_blueprint, array() ) ) {
42+
fwrite( STDERR, 'Empty post-runtime blueprint should preserve the runtime blueprint.' . PHP_EOL );
43+
exit( 1 );
44+
}
45+
46+
fwrite( STDOUT, 'OK: post-runtime blueprint ordering smoke passed.' . PHP_EOL );

0 commit comments

Comments
 (0)