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