-
Notifications
You must be signed in to change notification settings - Fork 382
/
Services.php
99 lines (88 loc) · 2.02 KB
/
Services.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Interface BackgroundTask.
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP;
use AmpProject\AmpWP\BackgroundTask\MonitorCssTransientCaching;
/**
* Centralized service handler to simplify management of registration, activation and deactivation hooks.
*
* @package AmpProject\AmpWP
*/
final class Services {
/**
* List of known services.
*
* @var string[]
*/
const ALL = [
MonitorCssTransientCaching::class,
];
/**
* Array of service object instances.
*
* @var Service[]
*/
private static $instances = [];
/**
* Register the services with the system.
*
* @return void
*/
public static function register() {
foreach ( self::instances() as $service ) {
$service->register();
}
}
/**
* Run activation logic.
*
* This should be hooked up to the WordPress activation hook.
*
* @todo Iterate over blogs on multisite to activate on all subsites by default here?
*
* @param bool $network_wide Whether the activation was done network-wide.
* @return void
*/
public static function activate( $network_wide ) {
foreach ( self::instances() as $service ) {
if ( ! $service instanceof HasActivation ) {
continue;
}
$service->activate( $network_wide );
}
}
/**
* Run deactivation logic.
*
* This should be hooked up to the WordPress deactivation hook.
*
* @todo Iterate over blogs on multisite to deactivate on all subsites by default here?
*
* @param bool $network_wide Whether the deactivation was done network-wide.
* @return void
*/
public static function deactivate( $network_wide ) {
foreach ( self::instances() as $service ) {
if ( ! $service instanceof HasDeactivation ) {
continue;
}
$service->deactivate( $network_wide );
}
}
/**
* Get the services.
*
* @return Service[] Services.
*/
private static function instances() {
if ( empty( self::$instances ) ) {
foreach ( self::ALL as $service_class ) {
self::$instances[ $service_class ] = new $service_class();
}
}
return self::$instances;
}
}