Class Inspector


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * @name ClassInspector.php
  4.  * @author Jonnie Spratley
  5.  * @version 1.9
  6.  */
  7. class ClassInspector {
  8. public function __construct() {
  9. }
  10.  
  11. /**
  12.   * I get all of the methods and comments about a class.
  13.   *
  14.   * @param [string] $classfile - The file path of the file.
  15.   * @return [array]
  16.   */
  17. static public function inspectClass($classfile) {
  18. $classPath = pathinfo($classfile);
  19.  
  20. $fileDir = $classPath['dirname'];
  21. $fileBase = $classPath['basename'];
  22. $fileClass = $classPath['filename'];
  23. $fileExt = $classPath['extension'];
  24.  
  25. $fileLocation = $fileDir.DIRECTORY_SEPARATOR.$fileBase;
  26.  
  27. //Need to include the file or it wont be found
  28. include_once ($fileLocation);
  29.  
  30. $classArray = array();
  31. $classMethod = array();
  32. $classComments = '';
  33.  
  34. //Create a new ReflectionClass of that class
  35. $reflection = new ReflectionClass($fileClass);
  36.  
  37. //Get all the methods in the class
  38. $methods = $reflection->getMethods();
  39. //Get the Class doc comments
  40. $classComments = $reflection->getDocComment();
  41.  
  42. //This creates an instance of:
  43. //ReflectionFunction class, see documentation for all available methods
  44. foreach ($methods as $reflectionMethod) {
  45.  
  46. //Set up the values
  47. $methodParams = array();
  48. $methodComments = '';
  49.  
  50. //Make sure its public
  51. if ($reflectionMethod->isPublic())
  52. {
  53.  
  54. //Make sure that we get the constructor comments
  55. if ($reflectionMethod->isConstructor()) {
  56.  
  57. //Get the class Comments
  58. $classComments .= $reflectionMethod->getDocComment();
  59. }
  60. else {
  61.  
  62. //Grab the method comments
  63. $methodCommentArray = self::cleanComment( $reflectionMethod->getDocComment() );
  64.  
  65. //Do some method checking
  66. $isStatic = $reflectionMethod->isStatic() ? 'true' : 'false';
  67. $isFinal = $reflectionMethod->isFinal() ? 'true' : 'false';
  68. $isAbstract = $reflectionMethod->isAbstract() ? 'true' : 'false';
  69. $methodNumParams = $reflectionMethod->getNumberOfParameters();
  70. $methodNumRequiredParams = $reflectionMethod->getNumberOfRequiredParameters();
  71.  
  72. //Set the method name
  73. $methodName = $reflectionMethod->getName();
  74.  
  75. //Set the class name to be sure
  76. $className = $reflectionMethod->getDeclaringClass()->getName();
  77.  
  78. //Array of parameters
  79. $reflectionParameterArray = $reflectionMethod->getParameters();
  80.  
  81. //Loop the parameters for the method
  82.  
  83. //This creates an instance of:
  84. //ReflectionParameter class, see documentation for all available methods
  85. foreach ($reflectionParameterArray as $parameter) {
  86.  
  87. //Set the name of the parameter
  88. $paramName = $parameter->getName();
  89.  
  90. //See if there is a default value for the parameter
  91. $paramDefault = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : '';
  92. $paramType = '';
  93. $paramType = $parameter->isArray() ? 'array' : 'object';
  94.  
  95. //Build up the parameter array, with the name of the parameter, and there default values
  96. $methodParams[] = array(
  97. 'paramName'=> $paramName,
  98. 'paramDefault'=>$paramDefault,
  99. 'paramType' => $paramType,
  100. 'paramOptional'=>$parameter->isOptional() ? 'true' : 'false', 'paramComment'=>'');
  101.  
  102. } //ends param loop
  103.  
  104.  
  105. //Build up the method array, with the name of the class, method, comments, etc.
  106. $classMethod[] = array(
  107. 'methodClass'=>$className,
  108. 'methodName'=>$methodName,
  109. 'methodParamCount'=>$methodNumParams,
  110. 'methodRequiredParamCount'=>$methodNumRequiredParams,
  111. 'methodComments'=>$methodCommentArray,
  112. 'isFinal'=>$isFinal,
  113. 'isStatic'=>$isStatic,
  114. 'isAbstract'=>$isAbstract,
  115. 'methodParams'=>$methodParams );
  116.  
  117. } //ends isConstructor()
  118. } //ends isPublic
  119. } //ends method loop
  120.  
  121.  
  122. //Finish it off with an array containing all of the information about the class.
  123. $classArray = array('className'=>$className, 'classFilename'=>$fileBase, 'classLocation'=>$fileLocation, 'classMethods'=>$classMethod, 'classComments'=>self::cleanComment($classComments));
  124.  
  125. return $classArray;
  126. }
  127.  
  128. /**
  129.   * I call a user defined function in the provided class
  130.   *
  131.   * @param [string] $classFile - The full path including the file in which to call upon
  132.   * @param [string] $method - The name of the method to invoke
  133.   * @param [*] $arguments - The arguments that are required for the method
  134.   * @return [array] - The result from the method call
  135.   */
  136. static public function call($classFile, $method, $arguments = null) {
  137. if ($classFile) {
  138.  
  139. //Include the file, which is the class we are going to be using
  140. include_once ($classFile);
  141.  
  142. //Here we are getting the information about the file, this returns an array of containing the path, extension, filename, basename.
  143. $classInfo = pathinfo($classFile);
  144.  
  145. //Set the class to the filename of the file from the pathinfo function, which returns the name of the file no extension or path, just ClassName,
  146. //And php defines classes by the same name as the file, so we can use this later
  147. $class = $classInfo['filename'];
  148.  
  149. //Create a new instance of the $class, which is going to be ClassName, because we got only the nameof the file
  150. $classObj = new $class();
  151. $argArray = array();
  152. if ($arguments) {
  153. $argArray = explode(',', $arguments);
  154. }
  155. if (method_exists($classObj, $method)) {
  156. return call_user_func_array(array($classObj, $method), $argArray);
  157. }
  158.  
  159. }
  160. }
  161.  
  162. /**
  163.   * Cleans the comment string by removing all comment start and end characters.
  164.   *
  165.   * @static
  166.   * @private
  167.   * @param $comment(String) The method's comment.
  168.   */
  169. static public function cleanComment($comment) {
  170.  
  171. $comment = str_replace("/**", "", $comment);
  172. $comment = str_replace("*/", "", $comment);
  173. $comment = str_replace("*", "", $comment);
  174. $comment = str_replace("\r", "", trim($comment));
  175. $comment = str_replace("", "", trim($comment));
  176. $comment = eregi_replace("\n", "", trim($comment));
  177. $comment = str_replace("\n", "", trim($comment));
  178. $comment = eregi_replace("[\t]+", " ", trim($comment));
  179. $comment = str_replace("\"", "\\\"", $comment);
  180.  
  181. #$comments = explode( "@", $comment );
  182.  
  183. return $comment;
  184. }
  185.  
  186. }
  187.  
  188.  
  189. /*=====================================================Class testing ====================================
  190.  *
  191.  
  192.  
  193. $classfile = 'Utilities.php';
  194. $func = '';
  195. $args = array();
  196. $request[] = ClassInspector::inspectClass($classfile);
  197.  
  198. echo json_encode($request);
  199. //print_r($request);
  200.  
  201.  
  202. /*=====================================================Class testing ====================================*/
  203. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.