Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
19b53e5b | 5 | | | |
41498ac5 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 | | |
19b53e5b C |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | namespace Civi\Api4\Utils; | |
13 | ||
14 | /** | |
15 | * Just another place to put static functions... | |
16 | */ | |
17 | class ReflectionUtils { | |
18 | ||
19 | /** | |
20 | * @param \Reflector|\ReflectionClass $reflection | |
21 | * @param string $type | |
22 | * If we are not reflecting the class itself, specify "Method", "Property", etc. | |
23 | * | |
24 | * @return array | |
25 | */ | |
26 | public static function getCodeDocs($reflection, $type = NULL) { | |
27 | $docs = self::parseDocBlock($reflection->getDocComment()); | |
28 | ||
29 | // Recurse into parent functions | |
30 | if (isset($docs['inheritDoc']) || isset($docs['inheritdoc'])) { | |
31 | unset($docs['inheritDoc'], $docs['inheritdoc']); | |
32 | $newReflection = NULL; | |
33 | try { | |
34 | if ($type) { | |
35 | $name = $reflection->getName(); | |
36 | $reflectionClass = $reflection->getDeclaringClass()->getParentClass(); | |
37 | if ($reflectionClass) { | |
38 | $getItem = "get$type"; | |
39 | $newReflection = $reflectionClass->$getItem($name); | |
40 | } | |
41 | } | |
42 | else { | |
43 | $newReflection = $reflection->getParentClass(); | |
44 | } | |
45 | } | |
46 | catch (\ReflectionException $e) { | |
47 | } | |
48 | if ($newReflection) { | |
49 | // Mix in | |
50 | $additionalDocs = self::getCodeDocs($newReflection, $type); | |
51 | if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) { | |
52 | $docs['comment'] .= "\n\n" . $additionalDocs['comment']; | |
53 | } | |
54 | $docs += $additionalDocs; | |
55 | } | |
56 | } | |
57 | return $docs; | |
58 | } | |
59 | ||
60 | /** | |
61 | * @param string $comment | |
62 | * @return array | |
63 | */ | |
64 | public static function parseDocBlock($comment) { | |
65 | $info = []; | |
66 | foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) { | |
67 | if (!$num || strpos($line, '*/') !== FALSE) { | |
68 | continue; | |
69 | } | |
70 | $line = ltrim(trim($line), '* '); | |
71 | if (strpos($line, '@') === 0) { | |
72 | $words = explode(' ', $line); | |
73 | $key = substr($words[0], 1); | |
74 | if ($key == 'var') { | |
75 | $info['type'] = explode('|', $words[1]); | |
76 | } | |
77 | elseif ($key == 'options') { | |
78 | $val = str_replace(', ', ',', implode(' ', array_slice($words, 1))); | |
79 | $info['options'] = explode(',', $val); | |
80 | } | |
81 | else { | |
82 | // Unrecognized annotation, but we'll duly add it to the info array | |
83 | $val = implode(' ', array_slice($words, 1)); | |
84 | $info[$key] = strlen($val) ? $val : TRUE; | |
85 | } | |
86 | } | |
87 | elseif ($num == 1) { | |
88 | $info['description'] = $line; | |
89 | } | |
90 | elseif (!$line) { | |
91 | if (isset($info['comment'])) { | |
92 | $info['comment'] .= "\n"; | |
93 | } | |
94 | } | |
95 | else { | |
96 | $info['comment'] = isset($info['comment']) ? "{$info['comment']}\n$line" : $line; | |
97 | } | |
98 | } | |
99 | if (isset($info['comment'])) { | |
100 | $info['comment'] = trim($info['comment']); | |
101 | } | |
102 | return $info; | |
103 | } | |
104 | ||
105 | /** | |
106 | * List all traits used by a class and its parents. | |
107 | * | |
108 | * @param object|string $class | |
109 | * @return array | |
110 | */ | |
111 | public static function getTraits($class) { | |
112 | $traits = []; | |
113 | // Get traits of this class + parent classes | |
114 | do { | |
115 | $traits = array_merge(class_uses($class), $traits); | |
116 | } while ($class = get_parent_class($class)); | |
117 | // Get traits of traits | |
118 | foreach ($traits as $trait => $same) { | |
119 | $traits = array_merge(class_uses($trait), $traits); | |
120 | } | |
121 | return $traits; | |
122 | } | |
123 | ||
124 | } |