status check for external case xml files
[civicrm-core.git] / CRM / Utils / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes.
35 */
36 class CRM_Utils_PseudoConstant {
37 /**
38 * CiviCRM pseudoconstant classes for wrapper functions.
39 * @var array
40 */
41 private static $constantClasses = [
42 'CRM_Core_PseudoConstant',
43 'CRM_Event_PseudoConstant',
44 'CRM_Contribute_PseudoConstant',
45 'CRM_Member_PseudoConstant',
46 ];
47
48 /**
49 * @var array
50 * ($name => $className)
51 */
52 private static $constants = NULL;
53
54 /**
55 * Get constant.
56 *
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
60 *
61 *
62 * @param string $constant
63 *
64 * @return array
65 * array reference of all relevant constant
66 */
67 public static function getConstant($constant) {
68 $class = self::findConstantClass($constant);
69 if ($class) {
70 return $class::$constant();
71 }
72 }
73
74 /**
75 * Flush constant.
76 *
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
80 *
81 *
82 * @param $constant
83 *
84 * @return array
85 * array reference of all relevant constant
86 */
87 public static function flushConstant($constant) {
88 $class = self::findConstantClass($constant);
89 if ($class) {
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);
95 return TRUE;
96 }
97 }
98
99 /**
100 * Determine where a constant lives.
101 *
102 * If there's a full, preloaded map, use it. Otherwise, use search
103 * class space.
104 *
105 * @param string $constant
106 * Constant-name.
107 *
108 * @return string|NULL
109 * class-name
110 */
111 public static function findConstantClass($constant) {
112 if (self::$constants !== NULL && isset(self::$constants[$constant])) {
113 return self::$constants[$constant];
114 }
115 foreach (self::$constantClasses as $class) {
116 if (method_exists($class, lcfirst($constant))) {
117 return $class;
118 }
119 }
120 return NULL;
121 }
122
123 /**
124 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
125 * any static properties which have corresponding static methods.
126 *
127 * This may be inefficient and should generally be avoided.
128 *
129 * @return array
130 * Array of string, constant names
131 */
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;
138 }
139 }
140 }
141 return array_keys(self::$constants);
142 }
143
144 /**
145 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
146 * any static properties which have corresponding static methods.
147 *
148 * This may be inefficient and should generally be avoided.
149 *
150 * @param $class
151 *
152 * @return array
153 * Array of string, constant names
154 */
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))
160 );
161 return $classConstants;
162 }
163
164 /**
165 * Flush all caches related to pseudo-constants.
166 *
167 * This may be inefficient and should generally be avoided.
168 */
169 public static function flushAll() {
170 foreach (self::findConstants() as $constant) {
171 self::flushConstant($constant);
172 }
173 CRM_Core_PseudoConstant::flush();
174 }
175
176 }