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