5.16.0 release notes: add boilerplate
[civicrm-core.git] / CRM / Utils / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
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.
6714d8d2 39 * @var array
6a488035 40 */
be2fb01f 41 private static $constantClasses = [
6a488035
TO
42 'CRM_Core_PseudoConstant',
43 'CRM_Event_PseudoConstant',
44 'CRM_Contribute_PseudoConstant',
45 'CRM_Member_PseudoConstant',
be2fb01f 46 ];
6a488035
TO
47
48 /**
d631cdc8 49 * @var array
50bfb460 50 * ($name => $className)
6a488035
TO
51 */
52 private static $constants = NULL;
53
54 /**
fe482240 55 * Get constant.
6a488035
TO
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
b8c71ffa 59 * (some are on the Contribute_Pseudoconstant Class etc
6a488035 60 *
6a488035 61 *
b8c71ffa 62 * @param string $constant
f4aaa82a 63 *
a6c01b45
CW
64 * @return array
65 * array reference of all relevant constant
6a488035
TO
66 */
67 public static function getConstant($constant) {
68 $class = self::findConstantClass($constant);
69 if ($class) {
70 return $class::$constant();
71 }
72 }
73
74 /**
fe482240 75 * Flush constant.
6a488035
TO
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 *
6a488035 81 *
f4aaa82a
EM
82 * @param $constant
83 *
a6c01b45
CW
84 * @return array
85 * array reference of all relevant constant
6a488035
TO
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 /**
fe482240 100 * Determine where a constant lives.
6a488035
TO
101 *
102 * If there's a full, preloaded map, use it. Otherwise, use search
103 * class space.
104 *
77855840
TO
105 * @param string $constant
106 * Constant-name.
f4aaa82a 107 *
72b3a70c
CW
108 * @return string|NULL
109 * class-name
6a488035
TO
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 *
a6c01b45 129 * @return array
16b10e64 130 * Array of string, constant names
6a488035
TO
131 */
132 public static function findConstants() {
133 if (self::$constants === NULL) {
be2fb01f 134 self::$constants = [];
6a488035
TO
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 *
f4aaa82a
EM
150 * @param $class
151 *
a6c01b45 152 * @return array
16b10e64 153 * Array of string, constant names
6a488035
TO
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 /**
b8c71ffa 165 * Flush all caches related to pseudo-constants.
6a488035 166 *
b8c71ffa 167 * This may be inefficient and should generally be avoided.
6a488035
TO
168 */
169 public static function flushAll() {
170 foreach (self::findConstants() as $constant) {
171 self::flushConstant($constant);
172 }
4770c356 173 CRM_Core_PseudoConstant::flush();
6a488035 174 }
96025800 175
6a488035 176}