Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added an api inspect method to Debug

  • Loading branch information...
commit 86ae2722b58b0ddd3a72f080a1af3482d04b9d27 1 parent 85d7b0e
Alexander Morland authored September 07, 2011
9  al13_debug/config/bootstrap.php
@@ -50,6 +50,15 @@ function dd() {
50 50
 }
51 51
 
52 52
 /**
  53
+ * Inspect an object for it's properties and methods
  54
+ *
  55
+ * @param object $classOrObject
  56
+ */
  57
+function dapi($classOrObject) {
  58
+    $debug = Debug::get_instance();
  59
+    dd($debug->api($classOrObject));
  60
+}
  61
+/**
53 62
  * Convenient way of adding / setting a Debug setting
54 63
  * 
55 64
  * @param string $setting Name/Key of Debug config/setting to set
98  al13_debug/util/Debug.php
@@ -220,4 +220,102 @@ public function trace() {
220 220
         array_shift($arr);
221 221
         d($arr);
222 222
     }
  223
+
  224
+    public function api($var) {
  225
+        if (is_object($var)) {
  226
+            $class = get_class($var);
  227
+            $obj = $var;
  228
+        } else {
  229
+            if (!class_exists($var)) {
  230
+                throw new \Exception('Class ['.$var.'] doesn\'t exist');
  231
+            }
  232
+            $class = $var;
  233
+            try {
  234
+                $obj = new $class();
  235
+            } catch (\Exception $e) {
  236
+                throw new \Exception('Debug::api could not instantiate ['.$var.'], send it an object.');
  237
+            }
  238
+        }
  239
+        $reflection = new \ReflectionObject($obj);
  240
+        $properties = array();
  241
+        foreach (array(
  242
+            'public' => \ReflectionProperty::IS_PUBLIC,
  243
+            'protected' => \ReflectionProperty::IS_PROTECTED,
  244
+            'private' => \ReflectionProperty::IS_PRIVATE
  245
+            ) as $access => $rule) {
  246
+                $vars = $reflection->getProperties($rule);
  247
+                foreach ($vars as $refProp) {
  248
+                    $property = $refProp->getName();
  249
+                    $refProp->setAccessible(true);
  250
+                    $value = $refProp->getValue($obj);
  251
+                    $type = gettype($value);
  252
+                    if (is_object($value)) {
  253
+                        $value = get_class($value);
  254
+                    } elseif (is_array($value)) {
  255
+                        $value = 'array['.count($value).']';
  256
+                    }
  257
+
  258
+                    $properties[$access][$property] = compact('value', 'type');
  259
+                }
  260
+        }
  261
+        $constants = $reflection->getConstants();
  262
+
  263
+        $methods = array();
  264
+
  265
+        foreach (array(
  266
+            'public' => \ReflectionMethod::IS_PUBLIC,
  267
+            'protected' => \ReflectionMethod::IS_PROTECTED,
  268
+            'private' => \ReflectionMethod::IS_PRIVATE
  269
+            ) as $access => $rule) {
  270
+                $refMethods = $reflection->getMethods($rule);
  271
+                foreach ($refMethods as $refMethod) {
  272
+                    $refParams = $refMethod->getParameters();
  273
+                    $params = array();
  274
+                    foreach ($refParams as $refParam) {
  275
+                        $params[] = $refParam->getName();
  276
+                    }/*
  277
+                    $required = $refMethod->getNumberOfRequiredParameters();
  278
+                    $requiredParams = array();
  279
+                    for ($i=0;$i<$required;$i++) {
  280
+                        $requiredParams[] = array_shift($params);
  281
+                    }*/
  282
+                    $method_name = $refMethod->getName();
  283
+
  284
+                    $string = $access .' function '.$method_name.'(';
  285
+                    $paramString = '';
  286
+                    foreach ($params as $p) {
  287
+                        $paramString .= '$'.$p.', ';
  288
+                    }
  289
+                    $paramString  = substr($paramString,0,-2);
  290
+                    $string .= $paramString;
  291
+                    $string .= ')';
  292
+
  293
+                    $comment = $refMethod->getDocComment();
  294
+                    $comment = trim(preg_replace('/^(\s*\/\*\*|\s*\*{1,2}\/|\s*\* ?)/m', '', $comment));
  295
+                    $comment = str_replace("\r\n", "\n", $comment);
  296
+                    $commentParts = explode('@', $comment);
  297
+                    $description = array_shift($commentParts);
  298
+                    $tags = array();
  299
+                    foreach ($commentParts as $part) {
  300
+                        $tagArr = explode(' ', $part, 2);
  301
+                        if ($tagArr[0] == 'param') {
  302
+                            $paramArr = preg_split("/[\s\t]+/", $tagArr[1]);
  303
+                            $type = array_shift($paramArr);
  304
+                            if (empty($type)) $type = array_shift($paramArr);
  305
+                            $name = array_shift($paramArr);
  306
+                            $info = implode(' ', $paramArr);
  307
+                            $tags['param'][$name] = compact('type', 'info');
  308
+                        } else {
  309
+                            $tags[$tagArr[0]] = isset($tagArr[1])?$tagArr[1]:'';
  310
+                        }
  311
+                    }
  312
+
  313
+
  314
+                    $methods[$access][$string] = compact('description', 'tags');
  315
+                }
  316
+        }
  317
+
  318
+        return compact('properties', 'constants' ,'methods');
  319
+    }
  320
+
223 321
 }

0 notes on commit 86ae272

Please sign in to comment.
Something went wrong with that request. Please try again.