Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
b8c71ffa | 29 | * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes. |
6a488035 TO |
30 | */ |
31 | class CRM_Utils_PseudoConstant { | |
d424ffde | 32 | /** |
fe482240 | 33 | * CiviCRM pseudoconstant classes for wrapper functions. |
6a488035 TO |
34 | */ |
35 | private static $constantClasses = array( | |
36 | 'CRM_Core_PseudoConstant', | |
37 | 'CRM_Event_PseudoConstant', | |
38 | 'CRM_Contribute_PseudoConstant', | |
39 | 'CRM_Member_PseudoConstant', | |
6a488035 TO |
40 | ); |
41 | ||
42 | /** | |
43 | * @var array ($name => $className) | |
44 | */ | |
45 | private static $constants = NULL; | |
46 | ||
47 | /** | |
fe482240 | 48 | * Get constant. |
6a488035 TO |
49 | * |
50 | * Wrapper for Pseudoconstant methods. We use this so the calling function | |
51 | * doesn't need to know which class the Pseudoconstant is on | |
b8c71ffa | 52 | * (some are on the Contribute_Pseudoconstant Class etc |
6a488035 | 53 | * |
6a488035 | 54 | * |
b8c71ffa | 55 | * @param string $constant |
f4aaa82a | 56 | * |
a6c01b45 CW |
57 | * @return array |
58 | * array reference of all relevant constant | |
6a488035 TO |
59 | */ |
60 | public static function getConstant($constant) { | |
61 | $class = self::findConstantClass($constant); | |
62 | if ($class) { | |
63 | return $class::$constant(); | |
64 | } | |
65 | } | |
66 | ||
67 | /** | |
fe482240 | 68 | * Flush constant. |
6a488035 TO |
69 | * |
70 | * Wrapper for Pseudoconstant methods. We use this so the calling function | |
71 | * doesn't need to know which class the Pseudoconstant is on | |
72 | * (some are on the Contribute_Pseudoconsant Class etc | |
73 | * | |
6a488035 | 74 | * |
f4aaa82a EM |
75 | * @param $constant |
76 | * | |
a6c01b45 CW |
77 | * @return array |
78 | * array reference of all relevant constant | |
6a488035 TO |
79 | */ |
80 | public static function flushConstant($constant) { | |
81 | $class = self::findConstantClass($constant); | |
82 | if ($class) { | |
83 | $class::flush(lcfirst($constant)); | |
84 | //@todo the rule is api functions should only be called from within the api - we | |
85 | // should move this function to a Core class | |
86 | $name = _civicrm_api_get_entity_name_from_camel($constant); | |
87 | CRM_Core_OptionGroup::flush($name); | |
88 | return TRUE; | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Determine where a constant lives. |
6a488035 TO |
94 | * |
95 | * If there's a full, preloaded map, use it. Otherwise, use search | |
96 | * class space. | |
97 | * | |
77855840 TO |
98 | * @param string $constant |
99 | * Constant-name. | |
f4aaa82a | 100 | * |
72b3a70c CW |
101 | * @return string|NULL |
102 | * class-name | |
6a488035 TO |
103 | */ |
104 | public static function findConstantClass($constant) { | |
105 | if (self::$constants !== NULL && isset(self::$constants[$constant])) { | |
106 | return self::$constants[$constant]; | |
107 | } | |
108 | foreach (self::$constantClasses as $class) { | |
109 | if (method_exists($class, lcfirst($constant))) { | |
110 | return $class; | |
111 | } | |
112 | } | |
113 | return NULL; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing | |
118 | * any static properties which have corresponding static methods. | |
119 | * | |
120 | * This may be inefficient and should generally be avoided. | |
121 | * | |
a6c01b45 | 122 | * @return array |
16b10e64 | 123 | * Array of string, constant names |
6a488035 TO |
124 | */ |
125 | public static function findConstants() { | |
126 | if (self::$constants === NULL) { | |
127 | self::$constants = array(); | |
128 | foreach (self::$constantClasses as $class) { | |
129 | foreach (self::findConstantsByClass($class) as $constant) { | |
130 | self::$constants[$constant] = $class; | |
131 | } | |
132 | } | |
133 | } | |
134 | return array_keys(self::$constants); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing | |
139 | * any static properties which have corresponding static methods. | |
140 | * | |
141 | * This may be inefficient and should generally be avoided. | |
142 | * | |
f4aaa82a EM |
143 | * @param $class |
144 | * | |
a6c01b45 | 145 | * @return array |
16b10e64 | 146 | * Array of string, constant names |
6a488035 TO |
147 | */ |
148 | public static function findConstantsByClass($class) { | |
149 | $clazz = new ReflectionClass($class); | |
150 | $classConstants = array_intersect( | |
151 | CRM_Utils_Array::collect('name', $clazz->getProperties(ReflectionProperty::IS_STATIC)), | |
152 | CRM_Utils_Array::collect('name', $clazz->getMethods(ReflectionMethod::IS_STATIC)) | |
153 | ); | |
154 | return $classConstants; | |
155 | } | |
156 | ||
157 | /** | |
b8c71ffa | 158 | * Flush all caches related to pseudo-constants. |
6a488035 | 159 | * |
b8c71ffa | 160 | * This may be inefficient and should generally be avoided. |
6a488035 TO |
161 | */ |
162 | public static function flushAll() { | |
163 | foreach (self::findConstants() as $constant) { | |
164 | self::flushConstant($constant); | |
165 | } | |
4770c356 | 166 | CRM_Core_PseudoConstant::flush(); |
6a488035 | 167 | } |
96025800 | 168 | |
6a488035 | 169 | } |