Skip to content

Commit c3f4540

Browse files
authored
fix: register bundle artifact hooks early (#483)
1 parent 10bbb92 commit c3f4540

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

data-machine-code.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
// PSR-4 Autoloading.
2626
require_once __DIR__ . '/vendor/autoload.php';
2727

28+
// Bundle artifact types must be registered as soon as the plugin is loaded so
29+
// Data Machine can validate DMC-owned artifacts during early import paths.
30+
( new \DataMachineCode\Bundle\WorkspacePreloadArtifact() )->register();
31+
2832
/**
2933
* Install DMC-owned database tables.
3034
*/
@@ -99,7 +103,6 @@ function datamachine_code_bootstrap() {
99103
new \DataMachineCode\Abilities\CodeTaskAbilities();
100104
new \DataMachineCode\Abilities\WordPressRuntimeAbilities();
101105
\DataMachineCode\SourceInventory\WorkspaceSourceInventory::register();
102-
( new \DataMachineCode\Bundle\WorkspacePreloadArtifact() )->register();
103106

104107
// Project active workspace identity into Data Machine's engine_data
105108
// snapshot at job init. Requires DM's datamachine_engine_snapshot
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Smoke test that DMC bundle artifact hooks register at plugin load time.
4+
*
5+
* Run: php tests/smoke-bundle-artifact-early-registration.php
6+
*
7+
* @package DataMachineCode\Tests
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
if ( ! defined( 'WPINC' ) ) {
13+
define( 'WPINC', 'wp-includes' );
14+
}
15+
if ( ! defined( 'ABSPATH' ) ) {
16+
define( 'ABSPATH', __DIR__ . '/' );
17+
}
18+
19+
$filters = array();
20+
$actions = array();
21+
22+
function plugin_dir_path( string $file ): string {
23+
return dirname( $file ) . '/';
24+
}
25+
26+
function plugin_dir_url( string $file ): string {
27+
return 'https://example.test/wp-content/plugins/' . basename( dirname( $file ) ) . '/';
28+
}
29+
30+
function register_activation_hook( string $file, callable|string $callback ): void {
31+
unset( $file, $callback );
32+
}
33+
34+
function add_action( string $hook, callable|string|array $callback, int $priority = 10, int $accepted_args = 1 ): void {
35+
global $actions;
36+
$actions[ $hook ][ $priority ][] = array( $callback, $accepted_args );
37+
}
38+
39+
function add_filter( string $hook, callable|string|array $callback, int $priority = 10, int $accepted_args = 1 ): void {
40+
global $filters;
41+
$filters[ $hook ][ $priority ][] = array( $callback, $accepted_args );
42+
}
43+
44+
function apply_filters( string $hook, mixed $value, mixed ...$args ): mixed {
45+
global $filters;
46+
if ( empty( $filters[ $hook ] ) ) {
47+
return $value;
48+
}
49+
50+
ksort( $filters[ $hook ] );
51+
foreach ( $filters[ $hook ] as $callbacks ) {
52+
foreach ( $callbacks as $callback ) {
53+
$value = call_user_func_array( $callback[0], array_slice( array_merge( array( $value ), $args ), 0, (int) $callback[1] ) );
54+
}
55+
}
56+
57+
return $value;
58+
}
59+
60+
require __DIR__ . '/../data-machine-code.php';
61+
62+
$types = apply_filters( 'datamachine_agent_bundle_artifact_types', array( 'agent' ) );
63+
$ok = in_array( 'datamachine-code/workspace_preload', $types, true );
64+
65+
echo 'Bundle artifact early registration - smoke' . PHP_EOL;
66+
echo ( $ok ? ' ok registers workspace preload artifact before bootstrap' : ' fail registers workspace preload artifact before bootstrap' ) . PHP_EOL;
67+
echo PHP_EOL . 'Result: ' . ( $ok ? '1/1 passed' : '0/1 passed' ) . PHP_EOL;
68+
69+
exit( $ok ? 0 : 1 );

0 commit comments

Comments
 (0)