IPPhpDoc.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?PHP
  2. /**
  3. * The base phpdoc class searches for available classes
  4. *
  5. * @version 0.1
  6. * @author David Kingma
  7. */
  8. class IPPhpDoc{
  9. /** @var IPReflectionClass[] Array with available classes */
  10. public $classes = array();
  11. /** @var IPReflectionClass The current class */
  12. public $class = "";
  13. /**
  14. * Constructor, initiates the getClasses() method
  15. *
  16. * @return void
  17. */
  18. function __construct() {
  19. $this->getClasses();
  20. }
  21. /** Sets the current class
  22. * @param string The class name
  23. * @return void
  24. */
  25. public function setClass($class) {
  26. $this->class = new IPReflectionClass($class);
  27. }
  28. /**
  29. * Haalt alle geladen classes op die 'custom zijn
  30. *
  31. * @return IPReflectionClass[]
  32. */
  33. function getClasses() {
  34. $ar = get_declared_classes();
  35. foreach($ar as $class){
  36. $c = new reflectionClass($class);
  37. if($c->isUserDefined()){//add only when class is user-defined
  38. $this->classes[$class] = new IPReflectionClass($class);
  39. }
  40. }
  41. ksort($this->classes);
  42. return $this->classes;
  43. }
  44. /**
  45. * Generates the documentation page with all classes, methods etc.
  46. * @TODO FIXME: use the new template class
  47. * @param string Template file (optional)
  48. * @return string
  49. */
  50. public function getDocumentation($template="templates/docclass.xsl") {
  51. if(!is_file($template))
  52. throw new WSException("Could not find the template file: '$template'");
  53. $xtpl = new IPXSLTemplate($template);
  54. $documentation = Array();
  55. $documentation['menu'] = Array();
  56. //loop menu items
  57. $documentation['menu'] = $this->getClasses();
  58. if($this->class){
  59. if($this->class->isUserDefined()) {
  60. $this->class->properties = $this->class->getProperties(false, false);
  61. $this->class->methods = $this->class->getMethods(false, false);
  62. foreach((array)$this->class->methods as $method) {
  63. $method->params = $method->getParameters();
  64. }
  65. } else {
  66. $documentation['fault'] = "Native class";
  67. }
  68. $documentation['class'] = $this->class;
  69. }
  70. echo $xtpl->execute($documentation);
  71. }
  72. /**
  73. *
  74. * @param $comment String The doccomment
  75. * @param $annotationName String the annotation name
  76. * @param $annotationClass String the annotation class
  77. * @return void
  78. */
  79. public static function getAnnotation($comment, $annotationName, $annotationClass = null){
  80. if(!$annotationClass){
  81. $annotationClass = $annotationName;
  82. }
  83. $start = 0;
  84. if($start = stripos($comment, "@".$annotationName)){
  85. $obi = new $annotationClass();
  86. $start = strpos($comment, "(", $start);
  87. $end = strpos($comment, ")", $start);
  88. $propString = substr($comment, $start, ($end-$start) + 1);
  89. $eval = "return Array$propString;";
  90. $arr = @eval($eval);
  91. if($arr === false) throw new Exception("Error parsing annotation: $propString");
  92. foreach ((Array)$arr as $name => $value){
  93. $obi->$name= $value;
  94. }
  95. return $obi;
  96. }
  97. throw new Exception("Cannot find annotation @$annotationName ($start, $end): {$this->comment} ");
  98. }
  99. }
  100. ?>