3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes.
20 class CRM_Utils_PseudoConstant
{
22 * CiviCRM pseudoconstant classes for wrapper functions.
25 private static $constantClasses = [
26 'CRM_Core_PseudoConstant',
27 'CRM_Event_PseudoConstant',
28 'CRM_Contribute_PseudoConstant',
29 'CRM_Member_PseudoConstant',
34 * ($name => $className)
36 private static $constants = NULL;
41 * Wrapper for Pseudoconstant methods. We use this so the calling function
42 * doesn't need to know which class the Pseudoconstant is on
43 * (some are on the Contribute_Pseudoconstant Class etc
46 * @param string $constant
49 * array reference of all relevant constant
51 public static function getConstant($constant) {
52 $class = self
::findConstantClass($constant);
54 return $class::$constant();
61 * Wrapper for Pseudoconstant methods. We use this so the calling function
62 * doesn't need to know which class the Pseudoconstant is on
63 * (some are on the Contribute_Pseudoconsant Class etc
69 * array reference of all relevant constant
71 public static function flushConstant($constant) {
72 $class = self
::findConstantClass($constant);
74 $class::flush(lcfirst($constant));
75 //@todo the rule is api functions should only be called from within the api - we
76 // should move this function to a Core class
77 $name = _civicrm_api_get_entity_name_from_camel($constant);
78 CRM_Core_OptionGroup
::flush($name);
84 * Determine where a constant lives.
86 * If there's a full, preloaded map, use it. Otherwise, use search
89 * @param string $constant
95 public static function findConstantClass($constant) {
96 if (self
::$constants !== NULL && isset(self
::$constants[$constant])) {
97 return self
::$constants[$constant];
99 foreach (self
::$constantClasses as $class) {
100 if (method_exists($class, lcfirst($constant))) {
108 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
109 * any static properties which have corresponding static methods.
111 * This may be inefficient and should generally be avoided.
114 * Array of string, constant names
116 public static function findConstants() {
117 if (self
::$constants === NULL) {
118 self
::$constants = [];
119 foreach (self
::$constantClasses as $class) {
120 foreach (self
::findConstantsByClass($class) as $constant) {
121 self
::$constants[$constant] = $class;
125 return array_keys(self
::$constants);
129 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
130 * any static properties which have corresponding static methods.
132 * This may be inefficient and should generally be avoided.
137 * Array of string, constant names
139 public static function findConstantsByClass($class) {
140 $clazz = new ReflectionClass($class);
141 $classConstants = array_intersect(
142 CRM_Utils_Array
::collect('name', $clazz->getProperties(ReflectionProperty
::IS_STATIC
)),
143 CRM_Utils_Array
::collect('name', $clazz->getMethods(ReflectionMethod
::IS_STATIC
))
145 return $classConstants;
149 * Flush all caches related to pseudo-constants.
151 * This may be inefficient and should generally be avoided.
153 public static function flushAll() {
154 foreach (self
::findConstants() as $constant) {
155 self
::flushConstant($constant);
157 CRM_Core_PseudoConstant
::flush();