| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Service Provider for Terminal Shutdown tasks. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Shutdown; |
| 11 |
|
| 12 |
use SolidWP\Performance\Config\Config; |
| 13 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 14 |
use SolidWP\Performance\Page_Cache\Purge\Batch\Batch_Purger; |
| 15 |
use SolidWP\Performance\Update\Updater; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* The Service Provider for Terminal Shutdown tasks. |
| 23 |
* |
| 24 |
* @package SolidWP\Performance |
| 25 |
*/ |
| 26 |
final class Provider extends Service_Provider { |
| 27 |
|
| 28 |
/** |
| 29 |
* @inheritDoc |
| 30 |
*/ |
| 31 |
public function register(): void { |
| 32 |
$this->container->singleton( |
| 33 |
Shutdown_Handler::class, |
| 34 |
fn() => new Shutdown_Handler( |
| 35 |
// Add any terminable tasks to the collection to run on shutdown. |
| 36 |
// Important: these will run in the order provided. |
| 37 |
$this->container->get( Config::class ), |
| 38 |
$this->container->get( Updater::class ), |
| 39 |
$this->container->get( Batch_Purger::class ), |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
// Create an action that can be called manually. |
| 44 |
add_action( 'solidwp/performance/terminate', $this->container->callback( Shutdown_Handler::class, 'handle' ) ); |
| 45 |
|
| 46 |
// Run early to get ahead of any potentially bad code that could kill execution. |
| 47 |
add_action( |
| 48 |
'shutdown', |
| 49 |
static function (): void { |
| 50 |
do_action( 'solidwp/performance/terminate' ); |
| 51 |
}, |
| 52 |
1 |
| 53 |
); |
| 54 |
|
| 55 |
// Run this again in case any other code was updated in the shutdown action that could trigger our Terminable tasks. |
| 56 |
add_action( |
| 57 |
'shutdown', |
| 58 |
static function (): void { |
| 59 |
do_action( 'solidwp/performance/terminate' ); |
| 60 |
}, |
| 61 |
9999 |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|