3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes.
36 class CRM_Utils_PseudoConstant
{
38 * CiviCRM pseudoconstant classes for wrapper functions.
41 private static $constantClasses = [
42 'CRM_Core_PseudoConstant',
43 'CRM_Event_PseudoConstant',
44 'CRM_Contribute_PseudoConstant',
45 'CRM_Member_PseudoConstant',
50 * ($name => $className)
52 private static $constants = NULL;
57 * Wrapper for Pseudoconstant methods. We use this so the calling function
58 * doesn't need to know which class the Pseudoconstant is on
59 * (some are on the Contribute_Pseudoconstant Class etc
62 * @param string $constant
65 * array reference of all relevant constant
67 public static function getConstant($constant) {
68 $class = self
::findConstantClass($constant);
70 return $class::$constant();
77 * Wrapper for Pseudoconstant methods. We use this so the calling function
78 * doesn't need to know which class the Pseudoconstant is on
79 * (some are on the Contribute_Pseudoconsant Class etc
85 * array reference of all relevant constant
87 public static function flushConstant($constant) {
88 $class = self
::findConstantClass($constant);
90 $class::flush(lcfirst($constant));
91 //@todo the rule is api functions should only be called from within the api - we
92 // should move this function to a Core class
93 $name = _civicrm_api_get_entity_name_from_camel($constant);
94 CRM_Core_OptionGroup
::flush($name);
100 * Determine where a constant lives.
102 * If there's a full, preloaded map, use it. Otherwise, use search
105 * @param string $constant
108 * @return string|NULL
111 public static function findConstantClass($constant) {
112 if (self
::$constants !== NULL && isset(self
::$constants[$constant])) {
113 return self
::$constants[$constant];
115 foreach (self
::$constantClasses as $class) {
116 if (method_exists($class, lcfirst($constant))) {
124 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
125 * any static properties which have corresponding static methods.
127 * This may be inefficient and should generally be avoided.
130 * Array of string, constant names
132 public static function findConstants() {
133 if (self
::$constants === NULL) {
134 self
::$constants = [];
135 foreach (self
::$constantClasses as $class) {
136 foreach (self
::findConstantsByClass($class) as $constant) {
137 self
::$constants[$constant] = $class;
141 return array_keys(self
::$constants);
145 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
146 * any static properties which have corresponding static methods.
148 * This may be inefficient and should generally be avoided.
153 * Array of string, constant names
155 public static function findConstantsByClass($class) {
156 $clazz = new ReflectionClass($class);
157 $classConstants = array_intersect(
158 CRM_Utils_Array
::collect('name', $clazz->getProperties(ReflectionProperty
::IS_STATIC
)),
159 CRM_Utils_Array
::collect('name', $clazz->getMethods(ReflectionMethod
::IS_STATIC
))
161 return $classConstants;
165 * Flush all caches related to pseudo-constants.
167 * This may be inefficient and should generally be avoided.
169 public static function flushAll() {
170 foreach (self
::findConstants() as $constant) {
171 self
::flushConstant($constant);
173 CRM_Core_PseudoConstant
::flush();