Commit | Line | Data |
---|---|---|
711aa5d7 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class Civi | |
5 | * | |
6 | * The "Civi" class provides a facade for accessing major subsystems, | |
7 | * such as the service-container and settings manager. It serves as a | |
8 | * bridge which allows procedural code to access important objects. | |
9 | * | |
10 | * General principles: | |
11 | * - Each function provides access to a major subsystem. | |
12 | * - Each function performs a simple lookup. | |
13 | * - Each function returns an interface. | |
14 | * - Whenever possible, interfaces should be well-known (e.g. based | |
15 | * on a standard or well-regarded provider). | |
16 | */ | |
17 | class Civi { | |
18 | ||
19 | /** | |
20 | * A central location for static variable storage. | |
21 | * | |
22 | * @code | |
23 | * `Civi::$statics[__CLASS__]['foo'] = 'bar'; | |
24 | * @endcode | |
25 | */ | |
26 | public static $statics = array(); | |
27 | ||
7b5937fe TO |
28 | /** |
29 | * EXPERIMENTAL. Retrieve a named cache instance. | |
30 | * | |
31 | * This interface is flagged as experimental due to political | |
32 | * ambiguity in PHP community -- PHP-FIG has an open but | |
33 | * somewhat controversial draft standard for caching. Based on | |
34 | * the current draft, it's expected that this function could | |
35 | * simultaneously support both CRM_Utils_Cache_Interface and | |
36 | * PSR-6, but that depends on whether PSR-6 changes any more. | |
37 | * | |
38 | * @param string $name | |
39 | * The name of the cache. The 'default' cache is biased toward | |
40 | * high-performance caches (eg memcache/redis/apc) when | |
41 | * available and falls back to single-request (static) caching. | |
42 | * @return CRM_Utils_Cache_Interface | |
43 | */ | |
44 | public static function cache($name = 'default') { | |
45 | return \Civi\Core\Container::singleton()->get('cache.' . $name); | |
46 | } | |
47 | ||
711aa5d7 TO |
48 | /** |
49 | * Get the service container. | |
50 | * | |
51 | * @return \Symfony\Component\DependencyInjection\ContainerInterface | |
52 | */ | |
53 | public static function container() { | |
54 | return Civi\Core\Container::singleton(); | |
55 | } | |
56 | ||
9897fb1e TO |
57 | /** |
58 | * Get the event dispatcher. | |
59 | * | |
60 | * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface | |
61 | */ | |
62 | public static function dispatcher() { | |
63 | return Civi\Core\Container::singleton()->get('dispatcher'); | |
64 | } | |
65 | ||
83617886 TO |
66 | /** |
67 | * @return \Civi\Core\Lock\LockManager | |
68 | */ | |
69 | public static function lockManager() { | |
70 | return \Civi\Core\Container::getBootService('lockManager'); | |
71 | } | |
72 | ||
6e5ad5ee TO |
73 | /** |
74 | * @return \Psr\Log\LoggerInterface | |
75 | */ | |
76 | public static function log() { | |
77 | return Civi\Core\Container::singleton()->get('psr_log'); | |
78 | } | |
79 | ||
e3d28c74 TO |
80 | /** |
81 | * Obtain the core file/path mapper. | |
82 | * | |
83 | * @return \Civi\Core\Paths | |
84 | */ | |
85 | public static function paths() { | |
d4330c62 | 86 | return \Civi\Core\Container::getBootService('paths'); |
e3d28c74 TO |
87 | } |
88 | ||
711aa5d7 TO |
89 | /** |
90 | * Fetch a service from the container. | |
91 | * | |
92 | * @param string $id | |
93 | * The service ID. | |
94 | * @return mixed | |
95 | */ | |
96 | public static function service($id) { | |
97 | return \Civi\Core\Container::singleton()->get($id); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Reset all ephemeral system state, e.g. statics, | |
102 | * singletons, containers. | |
103 | */ | |
104 | public static function reset() { | |
711aa5d7 | 105 | self::$statics = array(); |
7f835399 | 106 | Civi\Core\Container::singleton(); |
711aa5d7 TO |
107 | } |
108 | ||
e3d28c74 TO |
109 | /** |
110 | * @return CRM_Core_Resources | |
111 | */ | |
112 | public static function resources() { | |
113 | return CRM_Core_Resources::singleton(); | |
114 | } | |
115 | ||
3a84c0ab TO |
116 | /** |
117 | * Obtain the domain settings. | |
118 | * | |
119 | * @param int|null $domainID | |
120 | * For the default domain, leave $domainID as NULL. | |
121 | * @return \Civi\Core\SettingsBag | |
122 | */ | |
123 | public static function settings($domainID = NULL) { | |
83617886 | 124 | return \Civi\Core\Container::getBootService('settings_manager')->getBagByDomain($domainID); |
3a84c0ab TO |
125 | } |
126 | ||
711aa5d7 | 127 | } |