← All changes
|
vendor/jetpack-autoloader/class-shutdown-handler.php
+92
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,92 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * This file was automatically generated by automattic/jetpack-autoloader. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-autoloader | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Autoloader\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ16_3_beta\al6_0_1; | |
| 9 | + | |
| 10 | + // phpcs:ignore | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * This class handles the shutdown of the autoloader. | |
| 14 | + */ | |
| 15 | +class Shutdown_Handler { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * The Plugins_Handler instance. | |
| 19 | + * | |
| 20 | + * @var Plugins_Handler | |
| 21 | + */ | |
| 22 | + private $plugins_handler; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * The plugins cached by this autoloader. | |
| 26 | + * | |
| 27 | + * @var string[] | |
| 28 | + */ | |
| 29 | + private $cached_plugins; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Indicates whether or not this autoloader was included by another. | |
| 33 | + * | |
| 34 | + * @var bool | |
| 35 | + */ | |
| 36 | + private $was_included_by_autoloader; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Constructor. | |
| 40 | + * | |
| 41 | + * @param Plugins_Handler $plugins_handler The Plugins_Handler instance to use. | |
| 42 | + * @param string[] $cached_plugins The plugins cached by the autoloaer. | |
| 43 | + * @param bool $was_included_by_autoloader Indicates whether or not the autoloader was included by another. | |
| 44 | + */ | |
| 45 | + public function __construct( $plugins_handler, $cached_plugins, $was_included_by_autoloader ) { | |
| 46 | + $this->plugins_handler = $plugins_handler; | |
| 47 | + $this->cached_plugins = $cached_plugins; | |
| 48 | + $this->was_included_by_autoloader = $was_included_by_autoloader; | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Handles the shutdown of the autoloader. | |
| 53 | + */ | |
| 54 | + public function __invoke() { | |
| 55 | + // Don't save a broken cache if an error happens during some plugin's initialization. | |
| 56 | + if ( ! did_action( 'plugins_loaded' ) ) { | |
| 57 | + // Ensure that the cache is emptied to prevent consecutive failures if the cache is to blame. | |
| 58 | + if ( ! empty( $this->cached_plugins ) ) { | |
| 59 | + $this->plugins_handler->cache_plugins( array() ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + return; | |
| 63 | + } | |
| 64 | + | |
| 65 | + // Load the active plugins fresh since the list we pulled earlier might not contain | |
| 66 | + // plugins that were activated but did not reset the autoloader. This happens | |
| 67 | + // when a plugin is in the cache but not "active" when the autoloader loads. | |
| 68 | + // We also want to make sure that plugins which are deactivating are not | |
| 69 | + // considered "active" so that they will be removed from the cache now. | |
| 70 | + try { | |
| 71 | + $active_plugins = $this->plugins_handler->get_active_plugins( false, ! $this->was_included_by_autoloader ); | |
| 72 | + } catch ( \Exception $ex ) { | |
| 73 | + // When the package is deleted before shutdown it will throw an exception. | |
| 74 | + // In the event this happens we should erase the cache. | |
| 75 | + if ( ! empty( $this->cached_plugins ) ) { | |
| 76 | + $this->plugins_handler->cache_plugins( array() ); | |
| 77 | + } | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // The paths should be sorted for easy comparisons with those loaded from the cache. | |
| 82 | + // Note we don't need to sort the cached entries because they're already sorted. | |
| 83 | + sort( $active_plugins ); | |
| 84 | + | |
| 85 | + // We don't want to waste time saving a cache that hasn't changed. | |
| 86 | + if ( $this->cached_plugins === $active_plugins ) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $this->plugins_handler->cache_plugins( $active_plugins ); | |
| 91 | + } | |
| 92 | +} | |