Context.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace Swagger;
  6. /**
  7. * Context
  8. *
  9. * The context in which the annotation is parsed.
  10. * It includes useful metadata which the Processors can use to augment the annotations.
  11. *
  12. * Context hierarchy:
  13. * - parseContext
  14. * |- docBlockContext
  15. * |- classContext
  16. * |- docBlockContext
  17. * |- propertyContext
  18. * |- methodContext
  19. *
  20. * @property string $comment The PHP DocComment
  21. * @property string $filename
  22. * @property int $line
  23. * @property int $character
  24. *
  25. * @property string $namespace
  26. * @property array $uses
  27. * @property string $class
  28. * @property string $extends
  29. * @property string $method
  30. * @property string $property
  31. * @property Annotations\AbstractAnnotation[] $annotations
  32. */
  33. class Context
  34. {
  35. /**
  36. * Prototypical inheritance for properties.
  37. * @var Context
  38. */
  39. private $_parent;
  40. /**
  41. * @param array $properties new properties for this context.
  42. * @param Context $parent The parent context
  43. */
  44. public function __construct($properties = [], $parent = null)
  45. {
  46. foreach ($properties as $property => $value) {
  47. $this->$property = $value;
  48. }
  49. $this->_parent = $parent;
  50. }
  51. /**
  52. * Check if a property is set directly on this context and not its parent context.
  53. *
  54. * @param string $type Example: $c->is('method') or $c->is('class')
  55. * @return bool
  56. */
  57. public function is($type)
  58. {
  59. return property_exists($this, $type);
  60. }
  61. /**
  62. * Check if a property is NOT set directly on this context and but its parent context.
  63. *
  64. * @param string $type Example: $c->not('method') or $c->not('class')
  65. * @return bool
  66. */
  67. public function not($type)
  68. {
  69. return property_exists($this, $type) === false;
  70. }
  71. /**
  72. * Return the context containing the specified property.
  73. *
  74. * @param string $property
  75. * @return boolean|\Swagger\Context
  76. */
  77. public function with($property)
  78. {
  79. if (property_exists($this, $property)) {
  80. return $this;
  81. }
  82. if ($this->_parent) {
  83. return $this->_parent->with($property);
  84. }
  85. return false;
  86. }
  87. /**
  88. * @return \Swagger\Context
  89. */
  90. public function getRootContext()
  91. {
  92. if ($this->_parent) {
  93. return $this->_parent->getRootContext();
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Export location for debugging.
  99. *
  100. * @return string Example: "file1.php on line 12"
  101. */
  102. public function getDebugLocation()
  103. {
  104. $location = '';
  105. if ($this->class && ($this->method || $this->property)) {
  106. $location .= $this->fullyQualifiedName($this->class);
  107. if ($this->method) {
  108. $location .= ($this->static ? '::' : '->') . $this->method . '()';
  109. } elseif ($this->property) {
  110. $location .= ($this->static ? '::$' : '->') . $this->property;
  111. }
  112. }
  113. if ($this->filename) {
  114. if ($location !== '') {
  115. $location .= ' in ';
  116. }
  117. $location .= $this->filename;
  118. }
  119. if ($this->line) {
  120. if ($location !== '') {
  121. $location .= ' on';
  122. }
  123. $location .= ' line ' . $this->line;
  124. if ($this->character) {
  125. $location .= ':' . $this->character;
  126. }
  127. }
  128. return $location;
  129. }
  130. /**
  131. * Traverse the context tree to get the property value.
  132. *
  133. * @param string $property
  134. * @return mixed
  135. */
  136. public function __get($property)
  137. {
  138. if ($this->_parent) {
  139. return $this->_parent->$property;
  140. }
  141. return null;
  142. }
  143. public function __toString()
  144. {
  145. return $this->getDebugLocation();
  146. }
  147. public function __debugInfo()
  148. {
  149. return ['-' => $this->getDebugLocation()];
  150. }
  151. /**
  152. * A short piece of text, usually one line, providing the basic function of the associated element.
  153. * @return string|null
  154. */
  155. public function phpdocSummary()
  156. {
  157. $content = $this->phpdocContent();
  158. if (!$content) {
  159. return null;
  160. }
  161. $lines = preg_split('/(\n|\r\n)/', $content);
  162. $summary = '';
  163. foreach ($lines as $line) {
  164. $summary .= $line."\n";
  165. if ($line === '' || substr($line, -1) === '.') {
  166. return trim($summary);
  167. }
  168. }
  169. $summary = trim($summary);
  170. if ($summary === '') {
  171. return null;
  172. }
  173. return $summary;
  174. }
  175. /**
  176. * An optional longer piece of text providing more details on the associated element’s function. This is very useful when working with a complex element.
  177. * @return string|null
  178. */
  179. public function phpdocDescription()
  180. {
  181. $summary = $this->phpdocSummary();
  182. if (!$summary) {
  183. return null;
  184. }
  185. $description = trim(substr($this->phpdocContent(), strlen($summary)));
  186. if ($description === '') {
  187. return null;
  188. }
  189. return $description;
  190. }
  191. /**
  192. * The text contents of the phpdoc comment (excl. tags)
  193. * @return string|null
  194. */
  195. public function phpdocContent()
  196. {
  197. $comment = preg_split('/(\n|\r\n)/', $this->comment);
  198. $comment[0] = preg_replace('/[ \t]*\\/\*\*/', '', $comment[0]); // strip '/**'
  199. $i = count($comment) -1;
  200. $comment[$i] = preg_replace('/\*\/[ \t]*$/', '', $comment[$i]); // strip '*/'
  201. $lines = [];
  202. $append = false;
  203. foreach ($comment as $line) {
  204. $line = ltrim($line, "\t *");
  205. if (substr($line, 0, 1) === '@') {
  206. break;
  207. }
  208. if ($append) {
  209. $i = count($lines) - 1;
  210. $lines[$i] = substr($lines[$i], 0, -1).$line;
  211. } else {
  212. $lines[] = $line;
  213. }
  214. $append = (substr($line, -1) === '\\');
  215. }
  216. $description = trim(implode("\n", $lines));
  217. if ($description === '') {
  218. return null;
  219. }
  220. return $description;
  221. }
  222. /**
  223. * Create a Context based on the debug_backtrace
  224. * @param int $index
  225. * @return \Swagger\Context
  226. */
  227. public static function detect($index = 0)
  228. {
  229. $context = new Context();
  230. $backtrace = debug_backtrace();
  231. $position = $backtrace[$index];
  232. if (isset($position['file'])) {
  233. $context->filename = $position['file'];
  234. }
  235. if (isset($position['line'])) {
  236. $context->line = $position['line'];
  237. }
  238. $caller = isset($backtrace[$index + 1]) ? $backtrace[$index + 1] : null;
  239. if (isset($caller['function'])) {
  240. $context->method = $caller['function'];
  241. if (isset($caller['type']) && $caller['type'] === '::') {
  242. $context->static = true;
  243. }
  244. }
  245. if (isset($caller['class'])) {
  246. $fqn = explode('\\', $caller['class']);
  247. $context->class = array_pop($fqn);
  248. if (count($fqn)) {
  249. $context->namespace = implode('\\', $fqn);
  250. }
  251. }
  252. // @todo extract namespaces and use statements
  253. return $context;
  254. }
  255. /**
  256. * Resolve the fully qualified name.
  257. *
  258. * @param string $class The class name
  259. * @return string
  260. */
  261. public function fullyQualifiedName($class)
  262. {
  263. if ($this->namespace) {
  264. $namespace = str_replace('\\\\', '\\', '\\' . $this->namespace . '\\');
  265. } else {
  266. $namespace = '\\'; // global namespace
  267. }
  268. if (strcasecmp($class, $this->class) === 0) {
  269. return $namespace . $this->class;
  270. }
  271. $pos = strpos($class, '\\');
  272. if ($pos !== false) {
  273. if ($pos === 0) {
  274. // Fully qualified name (\Foo\Bar)
  275. return $class;
  276. }
  277. // Qualified name (Foo\Bar)
  278. if ($this->uses) {
  279. foreach ($this->uses as $alias => $aliasedNamespace) {
  280. $alias .= '\\';
  281. if (strcasecmp(substr($class, 0, strlen($alias)), $alias) === 0) {
  282. // Aliased namespace (use \Long\Namespace as Foo)
  283. return '\\' . $aliasedNamespace . substr($class, strlen($alias) - 1);
  284. }
  285. }
  286. }
  287. } elseif ($this->uses) {
  288. // Unqualified name (Foo)
  289. foreach ($this->uses as $alias => $aliasedNamespace) {
  290. if (strcasecmp($alias, $class) === 0) {
  291. return '\\' . $aliasedNamespace;
  292. }
  293. }
  294. }
  295. return $namespace . $class;
  296. }
  297. }