@@ -14,4 +14,4 @@ |
||
| 14 | 14 | |
| 15 | 15 | use PinkCrab\Plugin_Lifecycle\Plugin_State_Change; |
| 16 | 16 | |
| 17 | -interface Activation extends Plugin_State_Change{} |
|
| 17 | +interface Activation extends Plugin_State_Change {} |
|
@@ -14,4 +14,4 @@ |
||
| 14 | 14 | |
| 15 | 15 | use PinkCrab\Plugin_Lifecycle\Plugin_State_Change; |
| 16 | 16 | |
| 17 | -interface Deactivation extends Plugin_State_Change{} |
|
| 17 | +interface Deactivation extends Plugin_State_Change {} |
|
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | protected $events; |
| 26 | 26 | |
| 27 | 27 | /** @param Plugin_State_Change ...$event */ |
| 28 | - public function __construct( Plugin_State_Change ...$event ) { |
|
| 28 | + public function __construct(Plugin_State_Change ...$event) { |
|
| 29 | 29 | $this->events = $event; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | * @return void |
| 36 | 36 | */ |
| 37 | 37 | public function __invoke() { |
| 38 | - foreach ( $this->events as $event ) { |
|
| 38 | + foreach ($this->events as $event) { |
|
| 39 | 39 | try { |
| 40 | 40 | $event->run(); |
| 41 | - } catch ( \Throwable $th ) { |
|
| 41 | + } catch (\Throwable $th) { |
|
| 42 | 42 | // If caught on Activation, throw Plugin_State_Exception |
| 43 | - if ( is_a( $event, Activation::class ) ) { |
|
| 44 | - throw Plugin_State_Exception::error_running_state_change_event( $event, $th ); |
|
| 43 | + if (is_a($event, Activation::class)) { |
|
| 44 | + throw Plugin_State_Exception::error_running_state_change_event($event, $th); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | continue; |
@@ -44,10 +44,10 @@ discard block |
||
| 44 | 44 | * @param class-string<Plugin_State_Change> $event |
| 45 | 45 | * @return self |
| 46 | 46 | */ |
| 47 | - public function event( string $event ): self { |
|
| 47 | + public function event(string $event): self { |
|
| 48 | 48 | // Ensure the event is a valid class. |
| 49 | - if ( ! class_exists( $event ) ) { |
|
| 50 | - throw Plugin_State_Exception::invalid_state_change_event_type( $event ); |
|
| 49 | + if ( ! class_exists($event)) { |
|
| 50 | + throw Plugin_State_Exception::invalid_state_change_event_type($event); |
|
| 51 | 51 | } |
| 52 | 52 | $this->events[] = $event; |
| 53 | 53 | return $this; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @param string $plugin_base_file |
| 60 | 60 | * @return self |
| 61 | 61 | */ |
| 62 | - public function plugin_base_file( string $plugin_base_file ): self { |
|
| 62 | + public function plugin_base_file(string $plugin_base_file): self { |
|
| 63 | 63 | $this->plugin_base_file = $plugin_base_file; |
| 64 | 64 | return $this; |
| 65 | 65 | } |
@@ -72,17 +72,17 @@ discard block |
||
| 72 | 72 | * @pram DI_Container $di_container |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 75 | + public function pre_boot(App_Config $config, Hook_Loader $loader, DI_Container $di_container): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 76 | 76 | |
| 77 | - if ( null === $this->plugin_base_file ) { |
|
| 78 | - throw Plugin_State_Exception::invalid_plugin_base_file( $this->plugin_base_file ); |
|
| 77 | + if (null === $this->plugin_base_file) { |
|
| 78 | + throw Plugin_State_Exception::invalid_plugin_base_file($this->plugin_base_file); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | // Create the instance of the state controller. |
| 82 | - $this->state_controller = new Plugin_State_Controller( $di_container, $this->plugin_base_file ); |
|
| 82 | + $this->state_controller = new Plugin_State_Controller($di_container, $this->plugin_base_file); |
|
| 83 | 83 | |
| 84 | 84 | // Finalise the state controller. |
| 85 | - add_action( Hooks::APP_INIT_PRE_BOOT, array( $this, 'finalise' ), 999, 3 ); |
|
| 85 | + add_action(Hooks::APP_INIT_PRE_BOOT, array($this, 'finalise'), 999, 3); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -93,26 +93,26 @@ discard block |
||
| 93 | 93 | * @pram DI_Container $di_container |
| 94 | 94 | * @return void |
| 95 | 95 | */ |
| 96 | - public function finalise( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed |
|
| 96 | + public function finalise(App_Config $config, Hook_Loader $loader, DI_Container $di_container): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed |
|
| 97 | 97 | |
| 98 | 98 | // Throw exception if controller not set. |
| 99 | - if ( null === $this->state_controller ) { |
|
| 99 | + if (null === $this->state_controller) { |
|
| 100 | 100 | throw Plugin_State_Exception::missing_controller(); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // Trigger the pre action. |
| 104 | - do_action( self::PRE_FINALISE, $this ); |
|
| 104 | + do_action(self::PRE_FINALISE, $this); |
|
| 105 | 105 | |
| 106 | 106 | // Add events to the controller. |
| 107 | - foreach ( $this->get_events() as $event ) { |
|
| 108 | - $this->state_controller->event( $event ); |
|
| 107 | + foreach ($this->get_events() as $event) { |
|
| 108 | + $this->state_controller->event($event); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Register the state controller. |
| 112 | 112 | $this->state_controller->finalise(); |
| 113 | 113 | |
| 114 | 114 | // Trigger the post action. |
| 115 | - do_action( self::POST_FINALISE, $this ); |
|
| 115 | + do_action(self::POST_FINALISE, $this); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | |
@@ -122,20 +122,20 @@ discard block |
||
| 122 | 122 | * @return class-string<Plugin_State_Change>[] |
| 123 | 123 | */ |
| 124 | 124 | public function get_events(): array { |
| 125 | - $events = apply_filters( self::STATE_EVENTS, $this->events ); |
|
| 126 | - $events = array_unique( $events ); |
|
| 127 | - $events = array_filter( $events, 'is_string' ); |
|
| 128 | - $events = array_filter( $events, fn( $event ) => is_subclass_of( $event, Plugin_State_Change::class ) ); |
|
| 125 | + $events = apply_filters(self::STATE_EVENTS, $this->events); |
|
| 126 | + $events = array_unique($events); |
|
| 127 | + $events = array_filter($events, 'is_string'); |
|
| 128 | + $events = array_filter($events, fn($event) => is_subclass_of($event, Plugin_State_Change::class)); |
|
| 129 | 129 | return $events; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | ## Unused methods |
| 133 | 133 | |
| 134 | 134 | /** @inheritDoc */ |
| 135 | - public function pre_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 135 | + public function pre_register(App_Config $config, Hook_Loader $loader, DI_Container $di_container): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 136 | 136 | |
| 137 | 137 | /** @inheritDoc */ |
| 138 | - public function post_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 138 | + public function post_register(App_Config $config, Hook_Loader $loader, DI_Container $di_container): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
|
| 139 | 139 | |
| 140 | 140 | /** @inheritDoc */ |
| 141 | 141 | public function get_middleware(): ?string { |
@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | * @param Throwable|null $exception |
| 25 | 25 | * @return Plugin_State_Exception |
| 26 | 26 | */ |
| 27 | - public static function failed_to_create_state_change_event( $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
| 27 | + public static function failed_to_create_state_change_event($event, ?Throwable $exception = null): Plugin_State_Exception { |
|
| 28 | 28 | $message = \sprintf( |
| 29 | 29 | 'Failed to construct %s using the DI Container. %s', |
| 30 | - is_string( $event ) ? $event : get_class( $event ), |
|
| 30 | + is_string($event) ? $event : get_class($event), |
|
| 31 | 31 | $exception ? $exception->getMessage() : '' |
| 32 | 32 | ); |
| 33 | - return new Plugin_State_Exception( $message, 101 ); |
|
| 33 | + return new Plugin_State_Exception($message, 101); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | * @param string|object $event |
| 40 | 40 | * @return Plugin_State_Exception |
| 41 | 41 | */ |
| 42 | - public static function invalid_state_change_event_type( $event ): Plugin_State_Exception { |
|
| 42 | + public static function invalid_state_change_event_type($event): Plugin_State_Exception { |
|
| 43 | 43 | $message = \sprintf( |
| 44 | 44 | '%s is not a valid Plugin State Change Event class', |
| 45 | - is_string( $event ) ? $event : get_class( $event ) |
|
| 45 | + is_string($event) ? $event : get_class($event) |
|
| 46 | 46 | ); |
| 47 | - return new Plugin_State_Exception( $message, 102 ); |
|
| 47 | + return new Plugin_State_Exception($message, 102); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return Plugin_State_Exception |
| 54 | 54 | */ |
| 55 | 55 | public static function failed_to_locate_calling_file(): Plugin_State_Exception { |
| 56 | - return new Plugin_State_Exception( 'Could not locate the file which created the Plugin State Controller. Please define this value manually using $controller->finalise( string $file )', 103 ); |
|
| 56 | + return new Plugin_State_Exception('Could not locate the file which created the Plugin State Controller. Please define this value manually using $controller->finalise( string $file )', 103); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -63,13 +63,13 @@ discard block |
||
| 63 | 63 | * @param Throwable $exception |
| 64 | 64 | * @return Plugin_State_Exception |
| 65 | 65 | */ |
| 66 | - public static function error_running_state_change_event( Plugin_State_Change $event, ?Throwable $exception = null ): Plugin_State_Exception { |
|
| 66 | + public static function error_running_state_change_event(Plugin_State_Change $event, ?Throwable $exception = null): Plugin_State_Exception { |
|
| 67 | 67 | $message = \sprintf( |
| 68 | 68 | 'Failed to run %s->run(), error thrown::%s', |
| 69 | - get_class( $event ), |
|
| 69 | + get_class($event), |
|
| 70 | 70 | $exception === null ? 'NO ERROR PASSED' : $exception->getMessage() |
| 71 | 71 | ); |
| 72 | - return new Plugin_State_Exception( $message, 104 ); |
|
| 72 | + return new Plugin_State_Exception($message, 104); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,8 +78,8 @@ discard block |
||
| 78 | 78 | * @param ?string $error |
| 79 | 79 | * @return Plugin_State_Exception |
| 80 | 80 | */ |
| 81 | - public static function invalid_plugin_base_file( $error = null ): Plugin_State_Exception { |
|
| 82 | - return new Plugin_State_Exception( is_string( $error ) ? $error : 'No plugin base file name passed.', 105 ); |
|
| 81 | + public static function invalid_plugin_base_file($error = null): Plugin_State_Exception { |
|
| 82 | + return new Plugin_State_Exception(is_string($error) ? $error : 'No plugin base file name passed.', 105); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -88,6 +88,6 @@ discard block |
||
| 88 | 88 | * @return Plugin_State_Exception |
| 89 | 89 | */ |
| 90 | 90 | public static function missing_controller(): Plugin_State_Exception { |
| 91 | - return new Plugin_State_Exception( 'No Plugin State Controller passed.', 106 ); |
|
| 91 | + return new Plugin_State_Exception('No Plugin State Controller passed.', 106); |
|
| 92 | 92 | } |
| 93 | 93 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | protected $plugin_base_file; |
| 49 | 49 | |
| 50 | - public function __construct( DI_Container $container, string $plugin_base_file ) { |
|
| 50 | + public function __construct(DI_Container $container, string $plugin_base_file) { |
|
| 51 | 51 | $this->container = $container; |
| 52 | 52 | $this->plugin_base_file = $plugin_base_file; |
| 53 | 53 | } |
@@ -59,25 +59,25 @@ discard block |
||
| 59 | 59 | * @return self |
| 60 | 60 | * @throws Plugin_State_Exception If none Plugin_State_Change (string or object) passed or fails to create instance from valid class name. |
| 61 | 61 | */ |
| 62 | - public function event( string $state_event ): self { |
|
| 62 | + public function event(string $state_event): self { |
|
| 63 | 63 | // Cache the event class name. |
| 64 | 64 | $state_event_string = $state_event; |
| 65 | 65 | |
| 66 | 66 | /* @phpstan-ignore-next-line, as this cannot be type hinted the check exists. */ |
| 67 | - if ( ! is_subclass_of( $state_event, Plugin_State_Change::class ) ) { |
|
| 68 | - throw Plugin_State_Exception::invalid_state_change_event_type( $state_event ); |
|
| 67 | + if ( ! is_subclass_of($state_event, Plugin_State_Change::class)) { |
|
| 68 | + throw Plugin_State_Exception::invalid_state_change_event_type($state_event); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | try { |
| 72 | 72 | /** @var Plugin_State_Change|null */ |
| 73 | - $state_event = $this->container->create( $state_event ); |
|
| 74 | - } catch ( \Throwable $th ) { |
|
| 75 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 73 | + $state_event = $this->container->create($state_event); |
|
| 74 | + } catch (\Throwable $th) { |
|
| 75 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // Throw exception if failed to create |
| 79 | - if ( null === $state_event || ! is_a( $state_event, Plugin_State_Change::class ) ) { |
|
| 80 | - throw Plugin_State_Exception::failed_to_create_state_change_event( $state_event_string ); |
|
| 79 | + if (null === $state_event || ! is_a($state_event, Plugin_State_Change::class)) { |
|
| 80 | + throw Plugin_State_Exception::failed_to_create_state_change_event($state_event_string); |
|
| 81 | 81 | } |
| 82 | 82 | $this->state_events[] = $state_event; |
| 83 | 83 | return $this; |
@@ -93,29 +93,29 @@ discard block |
||
| 93 | 93 | $file = $this->plugin_base_file; |
| 94 | 94 | // dump($file); |
| 95 | 95 | // Activation hooks if need adding. |
| 96 | - if ( $this->has_events_for_state( Activation::class ) ) { |
|
| 97 | - register_activation_hook( $file, $this->activation() ); |
|
| 96 | + if ($this->has_events_for_state(Activation::class)) { |
|
| 97 | + register_activation_hook($file, $this->activation()); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Deactivation hooks. |
| 101 | - if ( $this->has_events_for_state( Deactivation::class ) ) { |
|
| 102 | - register_deactivation_hook( $file, $this->deactivation() ); |
|
| 101 | + if ($this->has_events_for_state(Deactivation::class)) { |
|
| 102 | + register_deactivation_hook($file, $this->deactivation()); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // If we have an uninstall events, add then during activation. |
| 106 | - if ( $this->has_events_for_state( Uninstall::class ) ) { |
|
| 106 | + if ($this->has_events_for_state(Uninstall::class)) { |
|
| 107 | 107 | $callback = $this->uninstall(); |
| 108 | 108 | |
| 109 | 109 | // Register the callback so itsits included (but wont run due to serialization issues). |
| 110 | 110 | register_activation_hook( |
| 111 | 111 | $file, |
| 112 | - static function() use ( $file, $callback ): void { |
|
| 113 | - register_uninstall_hook( $file, $callback ); |
|
| 112 | + static function() use ($file, $callback): void { |
|
| 113 | + register_uninstall_hook($file, $callback); |
|
| 114 | 114 | } |
| 115 | 115 | ); |
| 116 | 116 | |
| 117 | 117 | // Manually re-add the uninstall hook. |
| 118 | - add_action( 'uninstall_' . plugin_basename( $file ), $callback ); |
|
| 118 | + add_action('uninstall_'.plugin_basename($file), $callback); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | return $this; |
@@ -127,12 +127,12 @@ discard block |
||
| 127 | 127 | * @param string $state |
| 128 | 128 | * @return Plugin_State_Change[] |
| 129 | 129 | */ |
| 130 | - private function get_events_for_state( string $state ): array { |
|
| 130 | + private function get_events_for_state(string $state): array { |
|
| 131 | 131 | return array_filter( |
| 132 | - apply_filters( Plugin_Life_Cycle::EVENT_LIST, $this->state_events ), |
|
| 133 | - function( $e ) use ( $state ): bool { |
|
| 132 | + apply_filters(Plugin_Life_Cycle::EVENT_LIST, $this->state_events), |
|
| 133 | + function($e) use ($state): bool { |
|
| 134 | 134 | /* @phpstan-ignore-next-line */ |
| 135 | - return is_subclass_of( $e, $state ); |
|
| 135 | + return is_subclass_of($e, $state); |
|
| 136 | 136 | } |
| 137 | 137 | ); |
| 138 | 138 | } |
@@ -143,8 +143,8 @@ discard block |
||
| 143 | 143 | * @param string $state |
| 144 | 144 | * @return bool |
| 145 | 145 | */ |
| 146 | - private function has_events_for_state( string $state ): bool { |
|
| 147 | - return count( $this->get_events_for_state( $state ) ) !== 0; |
|
| 146 | + private function has_events_for_state(string $state): bool { |
|
| 147 | + return count($this->get_events_for_state($state)) !== 0; |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | * @return State_Change_Queue |
| 154 | 154 | */ |
| 155 | 155 | public function activation(): State_Change_Queue { |
| 156 | - return new State_Change_Queue( ...$this->get_events_for_state( Activation::class ) ); |
|
| 156 | + return new State_Change_Queue(...$this->get_events_for_state(Activation::class)); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | * @return State_Change_Queue |
| 163 | 163 | */ |
| 164 | 164 | public function deactivation(): State_Change_Queue { |
| 165 | - return new State_Change_Queue( ...$this->get_events_for_state( Deactivation::class ) ); |
|
| 165 | + return new State_Change_Queue(...$this->get_events_for_state(Deactivation::class)); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * @return State_Change_Queue |
| 172 | 172 | */ |
| 173 | 173 | public function uninstall(): State_Change_Queue { |
| 174 | - return new State_Change_Queue( ...$this->get_events_for_state( Uninstall::class ) ); |
|
| 174 | + return new State_Change_Queue(...$this->get_events_for_state(Uninstall::class)); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | } |