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