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 = []; | |
9cea3619 | 66 | $param = NULL; |
19b53e5b C |
67 | foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) { |
68 | if (!$num || strpos($line, '*/') !== FALSE) { | |
69 | continue; | |
70 | } | |
9cea3619 | 71 | $line = preg_replace('/[ ]+/', ' ', ltrim(trim($line), '* ')); |
19b53e5b C |
72 | if (strpos($line, '@') === 0) { |
73 | $words = explode(' ', $line); | |
9cea3619 CW |
74 | $key = substr(array_shift($words), 1); |
75 | $param = NULL; | |
19b53e5b | 76 | if ($key == 'var') { |
9cea3619 | 77 | $info['type'] = explode('|', $words[0]); |
19b53e5b | 78 | } |
0493ec47 CW |
79 | elseif ($key == 'return') { |
80 | $info['return'] = explode('|', $words[0]); | |
81 | } | |
19b53e5b | 82 | elseif ($key == 'options') { |
9cea3619 | 83 | $val = str_replace(', ', ',', implode(' ', $words)); |
19b53e5b C |
84 | $info['options'] = explode(',', $val); |
85 | } | |
0493ec47 | 86 | elseif ($key == 'throws' || $key == 'see') { |
9cea3619 CW |
87 | $info[$key][] = implode(' ', $words); |
88 | } | |
89 | elseif ($key == 'param' && $words) { | |
90 | $type = $words[0][0] !== '$' ? explode('|', array_shift($words)) : NULL; | |
91 | $param = rtrim(array_shift($words), '-:()/'); | |
92 | $info['params'][$param] = [ | |
93 | 'type' => $type, | |
94 | 'description' => $words ? ltrim(implode(' ', $words), '-: ') : '', | |
95 | 'comment' => '', | |
96 | ]; | |
97 | } | |
19b53e5b C |
98 | else { |
99 | // Unrecognized annotation, but we'll duly add it to the info array | |
9cea3619 | 100 | $val = implode(' ', $words); |
19b53e5b C |
101 | $info[$key] = strlen($val) ? $val : TRUE; |
102 | } | |
103 | } | |
9cea3619 CW |
104 | elseif ($param) { |
105 | $info['params'][$param]['comment'] .= $line . "\n"; | |
106 | } | |
19b53e5b C |
107 | elseif ($num == 1) { |
108 | $info['description'] = $line; | |
109 | } | |
110 | elseif (!$line) { | |
111 | if (isset($info['comment'])) { | |
112 | $info['comment'] .= "\n"; | |
113 | } | |
114 | } | |
115 | else { | |
116 | $info['comment'] = isset($info['comment']) ? "{$info['comment']}\n$line" : $line; | |
117 | } | |
118 | } | |
119 | if (isset($info['comment'])) { | |
120 | $info['comment'] = trim($info['comment']); | |
121 | } | |
122 | return $info; | |
123 | } | |
124 | ||
125 | /** | |
126 | * List all traits used by a class and its parents. | |
127 | * | |
128 | * @param object|string $class | |
129 | * @return array | |
130 | */ | |
131 | public static function getTraits($class) { | |
132 | $traits = []; | |
133 | // Get traits of this class + parent classes | |
134 | do { | |
135 | $traits = array_merge(class_uses($class), $traits); | |
136 | } while ($class = get_parent_class($class)); | |
137 | // Get traits of traits | |
138 | foreach ($traits as $trait => $same) { | |
139 | $traits = array_merge(class_uses($trait), $traits); | |
140 | } | |
141 | return $traits; | |
142 | } | |
143 | ||
144 | } |