4ccbdba0dedf6f707e5f9e3e28374352b8f23617
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.
40 private static $constantClasses = [
41 'CRM_Core_PseudoConstant',
42 'CRM_Event_PseudoConstant',
43 'CRM_Contribute_PseudoConstant',
44 'CRM_Member_PseudoConstant',
49 * ($name => $className)
51 private static $constants = NULL;
56 * Wrapper for Pseudoconstant methods. We use this so the calling function
57 * doesn't need to know which class the Pseudoconstant is on
58 * (some are on the Contribute_Pseudoconstant Class etc
61 * @param string $constant
64 * array reference of all relevant constant
66 public static function getConstant($constant) {
67 $class = self
::findConstantClass($constant);
69 return $class::$constant();
76 * Wrapper for Pseudoconstant methods. We use this so the calling function
77 * doesn't need to know which class the Pseudoconstant is on
78 * (some are on the Contribute_Pseudoconsant Class etc
84 * array reference of all relevant constant
86 public static function flushConstant($constant) {
87 $class = self
::findConstantClass($constant);
89 $class::flush(lcfirst($constant));
90 //@todo the rule is api functions should only be called from within the api - we
91 // should move this function to a Core class
92 $name = _civicrm_api_get_entity_name_from_camel($constant);
93 CRM_Core_OptionGroup
::flush($name);
99 * Determine where a constant lives.
101 * If there's a full, preloaded map, use it. Otherwise, use search
104 * @param string $constant
107 * @return string|NULL
110 public static function findConstantClass($constant) {
111 if (self
::$constants !== NULL && isset(self
::$constants[$constant])) {
112 return self
::$constants[$constant];
114 foreach (self
::$constantClasses as $class) {
115 if (method_exists($class, lcfirst($constant))) {
123 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
124 * any static properties which have corresponding static methods.
126 * This may be inefficient and should generally be avoided.
129 * Array of string, constant names
131 public static function findConstants() {
132 if (self
::$constants === NULL) {
133 self
::$constants = [];
134 foreach (self
::$constantClasses as $class) {
135 foreach (self
::findConstantsByClass($class) as $constant) {
136 self
::$constants[$constant] = $class;
140 return array_keys(self
::$constants);
144 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
145 * any static properties which have corresponding static methods.
147 * This may be inefficient and should generally be avoided.
152 * Array of string, constant names
154 public static function findConstantsByClass($class) {
155 $clazz = new ReflectionClass($class);
156 $classConstants = array_intersect(
157 CRM_Utils_Array
::collect('name', $clazz->getProperties(ReflectionProperty
::IS_STATIC
)),
158 CRM_Utils_Array
::collect('name', $clazz->getMethods(ReflectionMethod
::IS_STATIC
))
160 return $classConstants;
164 * Flush all caches related to pseudo-constants.
166 * This may be inefficient and should generally be avoided.
168 public static function flushAll() {
169 foreach (self
::findConstants() as $constant) {
170 self
::flushConstant($constant);
172 CRM_Core_PseudoConstant
::flush();