Skip to content

Commit

Permalink
Add a shutdown handler to the installation script to prevent silent f…
Browse files Browse the repository at this point in the history
…atals (#464)

* Adding a shutdown function handler to catch errors and display them

* Only load the shutdown handler during installation

* Docs
  • Loading branch information
srtfisher authored Nov 14, 2023
1 parent c692556 commit 80d3aec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,32 @@ public static function ensure_composer_loaded() {
foreach ( $paths as $path ) {
if ( ! is_dir( $path ) && file_exists( $path ) ) {
require_once $path;

return;
}
}
}

/**
* Register a shutdown function to handle errors.
*
* Used during the WordPress installation process to catch silent errors.
*/
public static function register_shutdown_function(): void {
register_shutdown_function( [ static::class, 'handle_shutdown' ] );
}

/**
* Handle a shutdown error and display it.
*/
public static function handle_shutdown(): void {
$error = error_get_last();

if ( ! $error ) {
return;
}

static::error( '🚨 Error during test run:', 'Shutdown' );
static::code( $error );
}
}
4 changes: 4 additions & 0 deletions src/mantle/testing/concerns/trait-rsync-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public function maybe_rsync( string $to = null, string $from = null ): static {
* rsync the codebase from the wp-content level to the root of the WordPress
* installation. Also will attempt to locate the wp-content directory relative
* to the current directory.
*
* This isn't a perfect function and can sometimes fail to locate the proper
* `wp-content` directory. If it does fail to work, manually call
* `maybe_rsync()` yourself with the proper paths.
*/
public function maybe_rsync_wp_content(): static {
// Attempt to locate wp-content relative to the current directory.
Expand Down
5 changes: 5 additions & 0 deletions src/mantle/testing/wordpress-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@
}
}

// Ensure that the shutdown function is registered when installing WordPress.
if ( $installing_wp ) {
Utils::register_shutdown_function();
}

if ( $multisite && ! $installing_wp ) {
Utils::info( 'Running as multisite...' );
defined( 'MULTISITE' ) or define( 'MULTISITE', true );
Expand Down

0 comments on commit 80d3aec

Please sign in to comment.