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