/package/app/app/api_v3/lib/KalturaDocCommentParser.php

https://github.com/richhl/kalturaCE · PHP · 243 lines · 121 code · 49 blank · 73 comment · 17 complexity · d98d7835866b49ccfa51b93ef9647648 MD5 · raw file

  1. <?php
  2. /**
  3. * @ignore
  4. */
  5. class KalturaDocCommentParser
  6. {
  7. const DOCCOMMENT_READONLY = "/\\@readonly/i";
  8. const DOCCOMMENT_INSERTONLY = "/\\@insertonly/i";
  9. const DOCCOMMENT_WRITEONLY = "/\\@writeonly/i";
  10. const DOCCOMMENT_PARAM = '/\@param (\w*) \$__NAME__ ?(.*)/';
  11. const DOCCOMMENT_REPLACENET_PARAM_NAME = '__NAME__';
  12. const DOCCOMMENT_VAR_TYPE = "/\\@var (\\w*)/";
  13. const DOCCOMMENT_LINK = "/\\@link (.*)/";
  14. const DOCCOMMENT_DESCRIPTION = "/\\* [a-zA-Z\\-\\s].*/";
  15. const DOCCOMMENT_RETURN_TYPE = "/\\@return (\\w*)/";
  16. const DOCCOMMENT_SERVICE_NAME = "/\\@service\\s?(\\w*)/";
  17. const DOCCOMMENT_ACTION = "/\\@action\\s?(\\w*)/";
  18. const DOCCOMMENT_ACTION_ERRORS = "/\\@throws (.*)::(.*)/";
  19. const DOCCOMMENT_PACKAGE = "/\\@package ([.\\w]*)/";
  20. const DOCCOMMENT_SUBPACKAGE = "/\\@subpackage ([.\\w]*)/";
  21. const DOCCOMMENT_CLIENT_GENERATOR = "/\\@clientgenerator (\\w*)/";
  22. const DOCCOMMENT_FILTER = "/\\@filter ([\\w\\,\\s]*)/";
  23. const DOCCOMMENT_ABSTRACT = "/\\@abstract/i";
  24. const DOCCOMMENT_DEPRECATED = "/\\@deprecated/i";
  25. const DOCCOMMENT_SERVER_ONLY = "/\\@serverOnly/i";
  26. const DOCCOMMENT_DYNAMIC_TYPE = "/\\@dynamicType (\\w*)/i";
  27. const DOCCOMMENT_PERMISSIONS = "/\\@requiresPermission ([\\w\\,\\s]*)/";
  28. /**
  29. * @var bool
  30. */
  31. public $readOnly;
  32. /**
  33. * @var bool
  34. */
  35. public $writeOnly;
  36. /**
  37. * @var bool
  38. */
  39. public $insertOnly;
  40. /**
  41. * @var string
  42. */
  43. public $param = "";
  44. /**
  45. * @var string
  46. */
  47. public $paramDescription = "";
  48. /**
  49. * @var string
  50. */
  51. public $varType;
  52. /**
  53. * @var string
  54. */
  55. public $returnType;
  56. /**
  57. * @var string
  58. */
  59. public $link;
  60. /**
  61. * @var string
  62. */
  63. public $description;
  64. /**
  65. * @var string
  66. */
  67. public $serviceName;
  68. /**
  69. * @var string
  70. */
  71. public $action;
  72. /**
  73. * @var string
  74. */
  75. public $package;
  76. /**
  77. * @var string
  78. */
  79. public $subpackage;
  80. /**
  81. * @var string
  82. */
  83. public $clientgenerator;
  84. /**
  85. * @var string
  86. */
  87. public $filter;
  88. /**
  89. * @var bool
  90. */
  91. public $abstract = false;
  92. /**
  93. * @var bool
  94. */
  95. public $deprecated = false;
  96. /**
  97. * @var bool
  98. */
  99. public $serverOnly = false;
  100. /**
  101. * @var array
  102. */
  103. public $errors;
  104. /**
  105. * @var string
  106. */
  107. public $dynamicType;
  108. /**
  109. * @var string
  110. */
  111. public $permissions;
  112. /**
  113. * Parse a docComment
  114. *
  115. * @param string $comment
  116. * @param array $replacements Optional associative array for replacing values in search patterns
  117. * @return array
  118. */
  119. function KalturaDocCommentParser($comment , $replacements = null)
  120. {
  121. $this->readOnly = preg_match( self::DOCCOMMENT_READONLY, $comment);
  122. $this->insertOnly = preg_match( self::DOCCOMMENT_INSERTONLY, $comment);
  123. $this->writeOnly = preg_match( self::DOCCOMMENT_WRITEONLY, $comment);
  124. $this->abstract = preg_match( self::DOCCOMMENT_ABSTRACT, $comment);
  125. $this->deprecated = preg_match( self::DOCCOMMENT_DEPRECATED, $comment);
  126. $this->serverOnly = preg_match( self::DOCCOMMENT_SERVER_ONLY, $comment);
  127. $result = null;
  128. if (is_array($replacements) && key_exists(self::DOCCOMMENT_REPLACENET_PARAM_NAME, $replacements))
  129. {
  130. $pattern = str_replace(self::DOCCOMMENT_REPLACENET_PARAM_NAME, $replacements[self::DOCCOMMENT_REPLACENET_PARAM_NAME], self::DOCCOMMENT_PARAM);
  131. if (preg_match( $pattern, $comment, $result ))
  132. {
  133. $this->param = $result[1];
  134. $this->paramDescription = $result[2];
  135. }
  136. }
  137. $result = null;
  138. if (preg_match( self::DOCCOMMENT_VAR_TYPE, $comment, $result ))
  139. $this->varType = $result[1];
  140. $result = null;
  141. if (preg_match_all( self::DOCCOMMENT_DESCRIPTION, $comment, $result ))
  142. $this->description = preg_replace("/(\\*\\s*)/", "", implode("\n", $result[0]));
  143. $result = null;
  144. if (preg_match( self::DOCCOMMENT_LINK, $comment, $result ))
  145. $this->link = $result[1];
  146. $result = null;
  147. if (preg_match(self::DOCCOMMENT_RETURN_TYPE, $comment, $result))
  148. $this->returnType = $result[1];
  149. $result = null;
  150. if (preg_match(self::DOCCOMMENT_SERVICE_NAME, $comment, $result))
  151. $this->serviceName = preg_replace("/[^a-zA-Z0-9_]/", "", $result[1]); // remove not allowed characters
  152. $result = null;
  153. if (preg_match(self::DOCCOMMENT_ACTION, $comment, $result))
  154. $this->action = preg_replace("/[^a-zA-Z0-9_]/", "", $result[1]); // remove not allowed characters
  155. $result = null;
  156. if (preg_match(self::DOCCOMMENT_PACKAGE, $comment, $result))
  157. $this->package = $result[1];
  158. $result = null;
  159. if (preg_match(self::DOCCOMMENT_SUBPACKAGE, $comment, $result))
  160. $this->subpackage = $result[1];
  161. $result = null;
  162. if (preg_match(self::DOCCOMMENT_CLIENT_GENERATOR, $comment, $result))
  163. $this->clientgenerator = $result[1];
  164. $result = null;
  165. if (preg_match(self::DOCCOMMENT_FILTER, $comment, $result))
  166. $this->filter = $result[1];
  167. $result = null;
  168. if (preg_match(self::DOCCOMMENT_DYNAMIC_TYPE, $comment, $result))
  169. $this->dynamicType = $result[1];
  170. $result = null;
  171. if (preg_match(self::DOCCOMMENT_PERMISSIONS, $comment, $result))
  172. $this->permissions = $result[1];
  173. $result = null;
  174. $error_array = array();
  175. if (preg_match_all(self::DOCCOMMENT_ACTION_ERRORS, $comment, $result))
  176. {
  177. foreach($result[1] as $index => $errorClass)
  178. {
  179. $error = trim($result[2][$index]);
  180. $apiErrorsReflected = new ReflectionClass($errorClass);
  181. $apiErrors = $apiErrorsReflected->getConstants();
  182. if(isset($apiErrors[$error]))
  183. {
  184. $error_array[] = array ( $error , $apiErrors[$error]);
  185. }
  186. else
  187. {
  188. KalturaLog::err("Constant [$error] not found in class [$errorClass]");
  189. }
  190. }
  191. }
  192. $this->errors = $error_array;
  193. }
  194. }