Merge pull request #15901 from eileenmcnaughton/matt
[civicrm-core.git] / CRM / Utils / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
50bfb460
SB
12/**
13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
50bfb460
SB
15 */
16
6a488035 17/**
b8c71ffa 18 * Utilities for manipulating/inspecting CRM_*_PseudoConstant classes.
6a488035
TO
19 */
20class CRM_Utils_PseudoConstant {
d424ffde 21 /**
fe482240 22 * CiviCRM pseudoconstant classes for wrapper functions.
6714d8d2 23 * @var array
6a488035 24 */
be2fb01f 25 private static $constantClasses = [
6a488035
TO
26 'CRM_Core_PseudoConstant',
27 'CRM_Event_PseudoConstant',
28 'CRM_Contribute_PseudoConstant',
29 'CRM_Member_PseudoConstant',
be2fb01f 30 ];
6a488035
TO
31
32 /**
d631cdc8 33 * @var array
50bfb460 34 * ($name => $className)
6a488035
TO
35 */
36 private static $constants = NULL;
37
38 /**
fe482240 39 * Get constant.
6a488035
TO
40 *
41 * Wrapper for Pseudoconstant methods. We use this so the calling function
42 * doesn't need to know which class the Pseudoconstant is on
b8c71ffa 43 * (some are on the Contribute_Pseudoconstant Class etc
6a488035 44 *
6a488035 45 *
b8c71ffa 46 * @param string $constant
f4aaa82a 47 *
a6c01b45
CW
48 * @return array
49 * array reference of all relevant constant
6a488035
TO
50 */
51 public static function getConstant($constant) {
52 $class = self::findConstantClass($constant);
53 if ($class) {
54 return $class::$constant();
55 }
56 }
57
58 /**
fe482240 59 * Flush constant.
6a488035
TO
60 *
61 * Wrapper for Pseudoconstant methods. We use this so the calling function
62 * doesn't need to know which class the Pseudoconstant is on
63 * (some are on the Contribute_Pseudoconsant Class etc
64 *
6a488035 65 *
f4aaa82a
EM
66 * @param $constant
67 *
a6c01b45
CW
68 * @return array
69 * array reference of all relevant constant
6a488035
TO
70 */
71 public static function flushConstant($constant) {
72 $class = self::findConstantClass($constant);
73 if ($class) {
74 $class::flush(lcfirst($constant));
75 //@todo the rule is api functions should only be called from within the api - we
76 // should move this function to a Core class
77 $name = _civicrm_api_get_entity_name_from_camel($constant);
78 CRM_Core_OptionGroup::flush($name);
79 return TRUE;
80 }
81 }
82
83 /**
fe482240 84 * Determine where a constant lives.
6a488035
TO
85 *
86 * If there's a full, preloaded map, use it. Otherwise, use search
87 * class space.
88 *
77855840
TO
89 * @param string $constant
90 * Constant-name.
f4aaa82a 91 *
72b3a70c
CW
92 * @return string|NULL
93 * class-name
6a488035
TO
94 */
95 public static function findConstantClass($constant) {
96 if (self::$constants !== NULL && isset(self::$constants[$constant])) {
97 return self::$constants[$constant];
98 }
99 foreach (self::$constantClasses as $class) {
100 if (method_exists($class, lcfirst($constant))) {
101 return $class;
102 }
103 }
104 return NULL;
105 }
106
107 /**
108 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
109 * any static properties which have corresponding static methods.
110 *
111 * This may be inefficient and should generally be avoided.
112 *
a6c01b45 113 * @return array
16b10e64 114 * Array of string, constant names
6a488035
TO
115 */
116 public static function findConstants() {
117 if (self::$constants === NULL) {
be2fb01f 118 self::$constants = [];
6a488035
TO
119 foreach (self::$constantClasses as $class) {
120 foreach (self::findConstantsByClass($class) as $constant) {
121 self::$constants[$constant] = $class;
122 }
123 }
124 }
125 return array_keys(self::$constants);
126 }
127
128 /**
129 * Scan for a list of pseudo-constants. A pseudo-constant is recognized by listing
130 * any static properties which have corresponding static methods.
131 *
132 * This may be inefficient and should generally be avoided.
133 *
f4aaa82a
EM
134 * @param $class
135 *
a6c01b45 136 * @return array
16b10e64 137 * Array of string, constant names
6a488035
TO
138 */
139 public static function findConstantsByClass($class) {
140 $clazz = new ReflectionClass($class);
141 $classConstants = array_intersect(
142 CRM_Utils_Array::collect('name', $clazz->getProperties(ReflectionProperty::IS_STATIC)),
143 CRM_Utils_Array::collect('name', $clazz->getMethods(ReflectionMethod::IS_STATIC))
144 );
145 return $classConstants;
146 }
147
148 /**
b8c71ffa 149 * Flush all caches related to pseudo-constants.
6a488035 150 *
b8c71ffa 151 * This may be inefficient and should generally be avoided.
6a488035
TO
152 */
153 public static function flushAll() {
154 foreach (self::findConstants() as $constant) {
155 self::flushConstant($constant);
156 }
4770c356 157 CRM_Core_PseudoConstant::flush();
6a488035 158 }
96025800 159
6a488035 160}