| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @package CRM |
| 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes. |
| 19 | */ |
| 20 | class CRM_Utils_PseudoConstant { |
| 21 | /** |
| 22 | * CiviCRM pseudoconstant classes for wrapper functions. |
| 23 | * @var array |
| 24 | */ |
| 25 | private static $constantClasses = [ |
| 26 | 'CRM_Core_PseudoConstant', |
| 27 | 'CRM_Event_PseudoConstant', |
| 28 | 'CRM_Contribute_PseudoConstant', |
| 29 | 'CRM_Member_PseudoConstant', |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * @var array |
| 34 | * ($name => $className) |
| 35 | */ |
| 36 | private static $constants = NULL; |
| 37 | |
| 38 | /** |
| 39 | * Get constant. |
| 40 | * |
| 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 |
| 44 | * |
| 45 | * |
| 46 | * @param string $constant |
| 47 | * |
| 48 | * @return array |
| 49 | * array reference of all relevant constant |
| 50 | */ |
| 51 | public static function getConstant($constant) { |
| 52 | $class = self::findConstantClass($constant); |
| 53 | if ($class) { |
| 54 | return $class::$constant(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Flush constant. |
| 60 | * |
| 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 |
| 64 | * |
| 65 | * |
| 66 | * @param $constant |
| 67 | * |
| 68 | * @return array |
| 69 | * array reference of all relevant constant |
| 70 | */ |
| 71 | public static function flushConstant($constant) { |
| 72 | $class = self::findConstantClass($constant); |
| 73 | if ($class) { |
| 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); |
| 79 | return TRUE; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Determine where a constant lives. |
| 85 | * |
| 86 | * If there's a full, preloaded map, use it. Otherwise, use search |
| 87 | * class space. |
| 88 | * |
| 89 | * @param string $constant |
| 90 | * Constant-name. |
| 91 | * |
| 92 | * @return string|NULL |
| 93 | * class-name |
| 94 | */ |
| 95 | public static function findConstantClass($constant) { |
| 96 | if (self::$constants !== NULL && isset(self::$constants[$constant])) { |
| 97 | return self::$constants[$constant]; |
| 98 | } |
| 99 | foreach (self::$constantClasses as $class) { |
| 100 | if (method_exists($class, lcfirst($constant))) { |
| 101 | return $class; |
| 102 | } |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing |
| 109 | * any static properties which have corresponding static methods. |
| 110 | * |
| 111 | * This may be inefficient and should generally be avoided. |
| 112 | * |
| 113 | * @return array |
| 114 | * Array of string, constant names |
| 115 | */ |
| 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; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return array_keys(self::$constants); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing |
| 130 | * any static properties which have corresponding static methods. |
| 131 | * |
| 132 | * This may be inefficient and should generally be avoided. |
| 133 | * |
| 134 | * @param $class |
| 135 | * |
| 136 | * @return array |
| 137 | * Array of string, constant names |
| 138 | */ |
| 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)) |
| 144 | ); |
| 145 | return $classConstants; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Flush all caches related to pseudo-constants. |
| 150 | * |
| 151 | * This may be inefficient and should generally be avoided. |
| 152 | */ |
| 153 | public static function flushAll() { |
| 154 | foreach (self::findConstants() as $constant) { |
| 155 | self::flushConstant($constant); |
| 156 | } |
| 157 | CRM_Core_PseudoConstant::flush(); |
| 158 | } |
| 159 | |
| 160 | } |