functions.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * FusionPBX
  4. * Version: MPL 1.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is FusionPBX
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Mark J Crane <[email protected]>
  20. * Portions created by the Initial Developer are Copyright (C) 2008-2024
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Mark J Crane <[email protected]>
  25. * Tim Fry <[email protected]>
  26. */
  27. if (!function_exists('has_trait')) {
  28. function has_trait($object_or_class, $trait): bool {
  29. if (trait_exists($trait) && in_array($trait, class_uses($object_or_class))) {
  30. //class has the trait
  31. return true;
  32. }
  33. //class does not have the trait
  34. return false;
  35. }
  36. }
  37. if (!function_exists('has_interface')) {
  38. /**
  39. * Returns true when the class has implemented the interface
  40. * @param string|object $object_or_class
  41. * @param string|object $interface
  42. * @return bool
  43. */
  44. function has_interface ($object_or_class, $interface): bool {
  45. //convert object to string
  46. if (gettype($object_or_class) === 'object') {
  47. $class_name = get_classname($object_or_class);
  48. } else {
  49. $class_name = $object_or_class;
  50. }
  51. //check in the list of interfaces the class name implements for a match
  52. if (class_exists($class_name) && in_array($interface, class_implements($class_name))) {
  53. //class implements the interface
  54. return true;
  55. }
  56. //class does not implement the interface
  57. return false;
  58. }
  59. }
  60. if (!function_exists('get_classname')) {
  61. /**
  62. *
  63. * @param type $string_or_object
  64. * @return string|object
  65. */
  66. function get_classname($string_or_object): string {
  67. if (gettype($string_or_object) === 'object') {
  68. // if (version_compare(PHP_VERSION, "8.0.0", "<")) {
  69. $backtrace = debug_backtrace();
  70. $classname = !empty($backtrace[1]['class']) ? $backtrace[1]['class'] : 'object';
  71. // } else {
  72. // // PHP 8.0 and higher can extract the class from a dynamic name
  73. // $classname = $string_or_object::class;
  74. // }
  75. } else {
  76. $classname = $string_or_object;
  77. }
  78. return $classname;
  79. }
  80. }
  81. if (!function_exists('implementing_classes')) {
  82. /**
  83. * Returns an array of classes that implement the interface name passed to the function
  84. * @param string $interface_name Name of interface to search for in classes
  85. * @return array Array of class names that have the implemented <i>interface_name</i>
  86. */
  87. function implementing_classes(string $interface_name): array {
  88. //initialize the array
  89. $found_classes = [];
  90. //load all objects available in the project
  91. $class_files = glob(dirname(__DIR__) . '/*/*/resources/classes/*.php');
  92. foreach ($class_files as $file) {
  93. //register the class name
  94. include_once $file;
  95. }
  96. //load all declared classes in an array
  97. $declared_classes = get_declared_classes();
  98. // Iterate over each class
  99. foreach ($declared_classes as $class) {
  100. // Check if the class implements the interfaces
  101. if (in_array($interface_name, class_implements($class))) {
  102. // Add the class to the array
  103. $found_classes[] = $class;
  104. }
  105. }
  106. return $found_classes;
  107. }
  108. }
  109. if (!function_exists('implementing_classes_arr')) {
  110. /**
  111. * Returns an array of classes that implement the interface name passed to the function
  112. * @param array $interface_names Names of interfaces to search for in classes
  113. * @return array Array of class names that have the implemented <i>interface_name</i>
  114. */
  115. function implementing_classes_arr(...$interface_names): array {
  116. //initialize the array
  117. $found_classes = [];
  118. //load all objects available in the project
  119. $class_files = glob(dirname(__DIR__) . '/*/*/resources/classes/*.php');
  120. foreach ($class_files as $file) {
  121. //register the class name
  122. include_once $file;
  123. }
  124. //load all declared classes in an array
  125. $declared_classes = get_declared_classes();
  126. // Iterate over each class
  127. foreach ($interface_names as $interface_name) {
  128. foreach ($declared_classes as $class) {
  129. // Check if the class implements the interfaces
  130. if (in_array($interface_name, class_implements($class))) {
  131. // Add the class to the array
  132. $found_classes[] = $class;
  133. }
  134. }
  135. }
  136. return $found_classes;
  137. }
  138. }
  139. if (!function_exists('user_defined_classes')) {
  140. function user_defined_classes () {
  141. return array_filter(
  142. get_declared_classes(),
  143. function($className) {
  144. return !call_user_func(
  145. array(new ReflectionClass($className), 'isInternal')
  146. );
  147. }
  148. );
  149. }
  150. }
  151. if (!function_exists('trait_classes')) {
  152. function trait_classes(string $trait) {
  153. // get user defined classes
  154. $user_classes = user_defined_classes();
  155. // select only classes that use trait $trait
  156. $trait_classes = array_filter(
  157. $user_classes,
  158. function($className) use($trait) {
  159. $traits = class_uses($className);
  160. return isset($traits[$trait]);
  161. }
  162. );
  163. return $trait_classes;
  164. }
  165. }
  166. if (!function_exists('trait_classes_arr')) {
  167. function trait_classes_arr(...$traits) {
  168. // get user defined classes
  169. $user_classes = user_defined_classes();
  170. // select only classes that use trait $trait
  171. $trait_classes = array_filter(
  172. $user_classes,
  173. function($classname) use($traits) {
  174. $trait_class = class_uses($classname);
  175. return count(array_intersect($trait_class, $traits)) > 0;
  176. }
  177. );
  178. return $trait_classes;
  179. }
  180. }