Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
2 / 7 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Setup | |
28.57% |
2 / 7 |
|
33.33% |
1 / 3 |
14.11 | |
0.00% |
0 / 1 |
| add | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_instances | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_instance_of | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib; |
| 4 | |
| 5 | use Automattic\Jetpack_Boost\Contracts\Has_Setup; |
| 6 | |
| 7 | class Setup { |
| 8 | |
| 9 | protected static $instances = array(); |
| 10 | |
| 11 | /** |
| 12 | * This method takes a `Has_Setup` instance and registers the setup action. |
| 13 | * In addition, it will keep track of all the instances passed to it, |
| 14 | * |
| 15 | * This is useful if a plugin needs the instance |
| 16 | * to modify the behavior at a certain hook that |
| 17 | * Jetpack Boost is using. |
| 18 | * |
| 19 | * The use case would be something like this: |
| 20 | * ``` |
| 21 | * $instance = my_get_instance_method( Setup::get_instances() ); |
| 22 | * remove_action( 'wp_footer', array( $instance, 'foobar' ) ); |
| 23 | * ``` |
| 24 | * |
| 25 | * @param Has_Setup $instance |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public static function add( Has_Setup $instance ) { |
| 30 | $instance->setup(); |
| 31 | |
| 32 | self::$instances[] = $instance; |
| 33 | } |
| 34 | |
| 35 | public static function get_instances() { |
| 36 | return self::$instances; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @template T |
| 41 | * @param class-string<T> $class_name |
| 42 | * @return T|null |
| 43 | */ |
| 44 | public static function get_instance_of( $class_name ) { |
| 45 | foreach ( self::get_instances() as $instance ) { |
| 46 | if ( $instance instanceof $class_name ) { |
| 47 | // @phan-suppress-next-line PhanTypeMismatchReturn -- Phan isn't inferring the type correctly from the `instanceof $class_name` like it's supposed to. |
| 48 | return $instance; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return null; |
| 53 | } |
| 54 | } |