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. | |
683bf891 | 21 | * @var array |
711aa5d7 TO |
22 | * @code |
23 | * `Civi::$statics[__CLASS__]['foo'] = 'bar'; | |
24 | * @endcode | |
25 | */ | |
26 | public static $statics = array(); | |
27 | ||
7b5937fe | 28 | /** |
ebf0eb53 | 29 | * Retrieve a named cache instance. |
7b5937fe TO |
30 | * |
31 | * @param string $name | |
32 | * The name of the cache. The 'default' cache is biased toward | |
33 | * high-performance caches (eg memcache/redis/apc) when | |
34 | * available and falls back to single-request (static) caching. | |
90cdaa0e TO |
35 | * Ex: 'short' or 'default' is useful for high-speed, short-lived cache data. |
36 | * This is appropriate if you believe that latency (millisecond-level | |
37 | * read time) is the main factor. For example: caching data from | |
38 | * a couple SQL queries. | |
39 | * Ex: 'long' can be useful for longer-lived cache data. It's appropriate if | |
40 | * you believe that longevity (e.g. surviving for several hours or a day) | |
41 | * is more important than millisecond-level access time. For example: | |
42 | * caching the result of a simple metadata-query. | |
43 | * | |
7b5937fe | 44 | * @return CRM_Utils_Cache_Interface |
ebf0eb53 TO |
45 | * NOTE: Beginning in CiviCRM v5.4, the cache instance complies with |
46 | * PSR-16 (\Psr\SimpleCache\CacheInterface). | |
7b5937fe TO |
47 | */ |
48 | public static function cache($name = 'default') { | |
49 | return \Civi\Core\Container::singleton()->get('cache.' . $name); | |
50 | } | |
51 | ||
711aa5d7 TO |
52 | /** |
53 | * Get the service container. | |
54 | * | |
55 | * @return \Symfony\Component\DependencyInjection\ContainerInterface | |
56 | */ | |
57 | public static function container() { | |
58 | return Civi\Core\Container::singleton(); | |
59 | } | |
60 | ||
9897fb1e TO |
61 | /** |
62 | * Get the event dispatcher. | |
63 | * | |
64 | * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface | |
65 | */ | |
66 | public static function dispatcher() { | |
67 | return Civi\Core\Container::singleton()->get('dispatcher'); | |
68 | } | |
69 | ||
83617886 TO |
70 | /** |
71 | * @return \Civi\Core\Lock\LockManager | |
72 | */ | |
73 | public static function lockManager() { | |
74 | return \Civi\Core\Container::getBootService('lockManager'); | |
75 | } | |
76 | ||
6e5ad5ee TO |
77 | /** |
78 | * @return \Psr\Log\LoggerInterface | |
79 | */ | |
80 | public static function log() { | |
81 | return Civi\Core\Container::singleton()->get('psr_log'); | |
82 | } | |
83 | ||
e3d28c74 TO |
84 | /** |
85 | * Obtain the core file/path mapper. | |
86 | * | |
87 | * @return \Civi\Core\Paths | |
88 | */ | |
89 | public static function paths() { | |
d4330c62 | 90 | return \Civi\Core\Container::getBootService('paths'); |
e3d28c74 TO |
91 | } |
92 | ||
711aa5d7 TO |
93 | /** |
94 | * Fetch a service from the container. | |
95 | * | |
96 | * @param string $id | |
97 | * The service ID. | |
98 | * @return mixed | |
99 | */ | |
100 | public static function service($id) { | |
101 | return \Civi\Core\Container::singleton()->get($id); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Reset all ephemeral system state, e.g. statics, | |
106 | * singletons, containers. | |
107 | */ | |
108 | public static function reset() { | |
711aa5d7 | 109 | self::$statics = array(); |
7f835399 | 110 | Civi\Core\Container::singleton(); |
711aa5d7 TO |
111 | } |
112 | ||
e3d28c74 TO |
113 | /** |
114 | * @return CRM_Core_Resources | |
115 | */ | |
116 | public static function resources() { | |
117 | return CRM_Core_Resources::singleton(); | |
118 | } | |
119 | ||
01d70c56 TO |
120 | /** |
121 | * Obtain the contact's personal settings. | |
122 | * | |
123 | * @param NULL|int $contactID | |
124 | * For the default/active user's contact, leave $domainID as NULL. | |
125 | * @param NULL|int $domainID | |
126 | * For the default domain, leave $domainID as NULL. | |
127 | * @return \Civi\Core\SettingsBag | |
128 | * @throws CRM_Core_Exception | |
129 | * If there is no contact, then there's no SettingsBag, and we'll throw | |
130 | * an exception. | |
131 | */ | |
132 | public static function contactSettings($contactID = NULL, $domainID = NULL) { | |
133 | return \Civi\Core\Container::getBootService('settings_manager')->getBagByContact($domainID, $contactID); | |
134 | } | |
135 | ||
3a84c0ab TO |
136 | /** |
137 | * Obtain the domain settings. | |
138 | * | |
139 | * @param int|null $domainID | |
140 | * For the default domain, leave $domainID as NULL. | |
141 | * @return \Civi\Core\SettingsBag | |
142 | */ | |
143 | public static function settings($domainID = NULL) { | |
83617886 | 144 | return \Civi\Core\Container::getBootService('settings_manager')->getBagByDomain($domainID); |
3a84c0ab TO |
145 | } |
146 | ||
711aa5d7 | 147 | } |