Resources.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. <?php
  2. namespace Luracast\Restler;
  3. use Luracast\Restler\Data\Text;
  4. use Luracast\Restler\Scope;
  5. use stdClass;
  6. /**
  7. * API Class to create Swagger Spec 1.1 compatible id and operation
  8. * listing
  9. *
  10. * @category Framework
  11. * @package Restler
  12. * @author R.Arul Kumaran <arul@luracast.com>
  13. * @copyright 2010 Luracast
  14. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15. * @link http://luracast.com/products/restler/
  16. *
  17. */
  18. class Resources implements iUseAuthentication, iProvideMultiVersionApi
  19. {
  20. /**
  21. * @var bool should protected resources be shown to unauthenticated users?
  22. */
  23. public static $hideProtected = true;
  24. /**
  25. * @var bool should we use format as extension?
  26. */
  27. public static $useFormatAsExtension = true;
  28. /**
  29. * @var bool should we include newer apis in the list? works only when
  30. * Defaults::$useUrlBasedVersioning is set to true;
  31. */
  32. public static $listHigherVersions = true;
  33. /**
  34. * @var array all http methods specified here will be excluded from
  35. * documentation
  36. */
  37. public static $excludedHttpMethods = array('OPTIONS');
  38. /**
  39. * @var array all paths beginning with any of the following will be excluded
  40. * from documentation
  41. */
  42. public static $excludedPaths = array();
  43. /**
  44. * @var bool
  45. */
  46. public static $placeFormatExtensionBeforeDynamicParts = true;
  47. /**
  48. * @var bool should we group all the operations with the same url or not
  49. */
  50. public static $groupOperations = false;
  51. /**
  52. * @var null|callable if the api methods are under access control mechanism
  53. * you can attach a function here that returns true or false to determine
  54. * visibility of a protected api method. this function will receive method
  55. * info as the only parameter.
  56. */
  57. public static $accessControlFunction = null;
  58. /**
  59. * @var array type mapping for converting data types to javascript / swagger
  60. */
  61. public static $dataTypeAlias = array(
  62. 'string' => 'string',
  63. 'int' => 'int',
  64. 'number' => 'float',
  65. 'float' => 'float',
  66. 'bool' => 'boolean',
  67. 'boolean' => 'boolean',
  68. 'NULL' => 'null',
  69. 'array' => 'Array',
  70. 'object' => 'Object',
  71. 'stdClass' => 'Object',
  72. 'mixed' => 'string',
  73. 'DateTime' => 'Date'
  74. );
  75. /**
  76. * @var array configurable symbols to differentiate public, hybrid and
  77. * protected api
  78. */
  79. public static $apiDescriptionSuffixSymbols = array(
  80. 0 => '&nbsp; <i class="icon-unlock-alt icon-large"></i>', //public api
  81. 1 => '&nbsp; <i class="icon-adjust icon-large"></i>', //hybrid api
  82. 2 => '&nbsp; <i class="icon-lock icon-large"></i>', //protected api
  83. );
  84. /**
  85. * Injected at runtime
  86. *
  87. * @var Restler instance of restler
  88. */
  89. public $restler;
  90. /**
  91. * @var string when format is not used as the extension this property is
  92. * used to set the extension manually
  93. */
  94. public $formatString = '';
  95. protected $_models;
  96. protected $_bodyParam;
  97. /**
  98. * @var bool|stdClass
  99. */
  100. protected $_fullDataRequested = false;
  101. protected $crud = array(
  102. 'POST' => 'create',
  103. 'GET' => 'retrieve',
  104. 'PUT' => 'update',
  105. 'DELETE' => 'delete',
  106. 'PATCH' => 'partial update'
  107. );
  108. protected static $prefixes = array(
  109. 'get' => 'retrieve',
  110. 'index' => 'list',
  111. 'post' => 'create',
  112. 'put' => 'update',
  113. 'patch' => 'modify',
  114. 'delete' => 'remove',
  115. );
  116. protected $_authenticated = false;
  117. protected $cacheName = '';
  118. public function __construct()
  119. {
  120. if (static::$useFormatAsExtension) {
  121. $this->formatString = '.{format}';
  122. }
  123. }
  124. /**
  125. * This method will be called first for filter classes and api classes so
  126. * that they can respond accordingly for filer method call and api method
  127. * calls
  128. *
  129. *
  130. * @param bool $isAuthenticated passes true when the authentication is
  131. * done, false otherwise
  132. *
  133. * @return mixed
  134. */
  135. public function __setAuthenticationStatus($isAuthenticated = false)
  136. {
  137. $this->_authenticated = $isAuthenticated;
  138. }
  139. /**
  140. * pre call for get($id)
  141. *
  142. * if cache is present, use cache
  143. */
  144. public function _pre_get_json($id)
  145. {
  146. $userClass = Defaults::$userIdentifierClass;
  147. $this->cacheName = $userClass::getCacheIdentifier() . '_resources_' . $id;
  148. if ($this->restler->getProductionMode()
  149. && !$this->restler->refreshCache
  150. && $this->restler->cache->isCached($this->cacheName)
  151. ) {
  152. //by pass call, compose, postCall stages and directly send response
  153. $this->restler->composeHeaders();
  154. die($this->restler->cache->get($this->cacheName));
  155. }
  156. }
  157. /**
  158. * post call for get($id)
  159. *
  160. * create cache if in production mode
  161. *
  162. * @param $responseData
  163. *
  164. * @internal param string $data composed json output
  165. *
  166. * @return string
  167. */
  168. public function _post_get_json($responseData)
  169. {
  170. if ($this->restler->getProductionMode()) {
  171. $this->restler->cache->set($this->cacheName, $responseData);
  172. }
  173. return $responseData;
  174. }
  175. /**
  176. * @access hybrid
  177. *
  178. * @param string $id
  179. *
  180. * @throws RestException
  181. * @return null|stdClass
  182. *
  183. * @url GET {id}
  184. */
  185. public function get($id = '')
  186. {
  187. $version = $this->restler->getRequestedApiVersion();
  188. if (empty($id)) {
  189. //do nothing
  190. } elseif (false !== ($pos = strpos($id, '-v'))) {
  191. //$version = intval(substr($id, $pos + 2));
  192. $id = substr($id, 0, $pos);
  193. } elseif ($id[0] == 'v' && is_numeric($v = substr($id, 1))) {
  194. $id = '';
  195. //$version = $v;
  196. } elseif ($id == 'root' || $id == 'index') {
  197. $id = '';
  198. }
  199. $this->_models = new stdClass();
  200. $r = null;
  201. $count = 0;
  202. $tSlash = !empty($id);
  203. $target = empty($id) ? '' : $id;
  204. $tLen = strlen($target);
  205. $filter = array();
  206. $routes
  207. = Util::nestedValue(Routes::toArray(), "v$version")
  208. ? : array();
  209. $prefix = Defaults::$useUrlBasedVersioning ? "/v$version" : '';
  210. foreach ($routes as $value) {
  211. foreach ($value as $httpMethod => $route) {
  212. if (in_array($httpMethod, static::$excludedHttpMethods)) {
  213. continue;
  214. }
  215. $fullPath = $route['url'];
  216. if ($fullPath !== $target && !Text::beginsWith($fullPath, $target)) {
  217. continue;
  218. }
  219. $fLen = strlen($fullPath);
  220. if ($tSlash) {
  221. if ($fLen != $tLen && !Text::beginsWith($fullPath, $target . '/'))
  222. continue;
  223. } elseif ($fLen > $tLen + 1 && $fullPath[$tLen + 1] != '{' && !Text::beginsWith($fullPath, '{')) {
  224. //when mapped to root exclude paths that have static parts
  225. //they are listed else where under that static part name
  226. continue;
  227. }
  228. if (!static::verifyAccess($route)) {
  229. continue;
  230. }
  231. foreach (static::$excludedPaths as $exclude) {
  232. if (empty($exclude)) {
  233. if ($fullPath == $exclude)
  234. continue 2;
  235. } elseif (Text::beginsWith($fullPath, $exclude)) {
  236. continue 2;
  237. }
  238. }
  239. $m = $route['metadata'];
  240. if ($id == '' && $m['resourcePath'] != '') {
  241. continue;
  242. }
  243. if (isset($filter[$httpMethod][$fullPath])) {
  244. continue;
  245. }
  246. $filter[$httpMethod][$fullPath] = true;
  247. // reset body params
  248. $this->_bodyParam = array(
  249. 'required' => false,
  250. 'description' => array()
  251. );
  252. $count++;
  253. $className = $this->_noNamespace($route['className']);
  254. if (!$r) {
  255. $resourcePath = '/'
  256. . trim($m['resourcePath'], '/');
  257. $r = $this->_operationListing($resourcePath);
  258. }
  259. $parts = explode('/', $fullPath);
  260. $pos = count($parts) - 1;
  261. if (count($parts) == 1 && $httpMethod == 'GET') {
  262. } else {
  263. for ($i = 0; $i < count($parts); $i++) {
  264. if (strlen($parts[$i]) && $parts[$i][0] == '{') {
  265. $pos = $i - 1;
  266. break;
  267. }
  268. }
  269. }
  270. $nickname = $this->_nickname($route);
  271. $index = static::$placeFormatExtensionBeforeDynamicParts && $pos > 0 ? $pos : 0;
  272. if (!empty($parts[$index]))
  273. $parts[$index] .= $this->formatString;
  274. $fullPath = implode('/', $parts);
  275. $description = isset(
  276. $m['classDescription'])
  277. ? $m['classDescription']
  278. : $className . ' API';
  279. if (empty($m['description'])) {
  280. $m['description'] = $this->restler->getProductionMode()
  281. ? ''
  282. : 'routes to <mark>'
  283. . $route['className']
  284. . '::'
  285. . $route['methodName'] . '();</mark>';
  286. }
  287. if (empty($m['longDescription'])) {
  288. $m['longDescription'] = $this->restler->getProductionMode()
  289. ? ''
  290. : 'Add PHPDoc long description to '
  291. . "<mark>$className::"
  292. . $route['methodName'] . '();</mark>'
  293. . ' (the api method) to write here';
  294. }
  295. $operation = $this->_operation(
  296. $route,
  297. $nickname,
  298. $httpMethod,
  299. $m['description'],
  300. $m['longDescription']
  301. );
  302. if (isset($m['throws'])) {
  303. foreach ($m['throws'] as $exception) {
  304. $operation->errorResponses[] = array(
  305. 'reason' => $exception['message'],
  306. 'code' => $exception['code']);
  307. }
  308. }
  309. if (isset($m['param'])) {
  310. foreach ($m['param'] as $param) {
  311. //combine body params as one
  312. $p = $this->_parameter($param);
  313. if ($p->paramType == 'body') {
  314. $this->_appendToBody($p);
  315. } else {
  316. $operation->parameters[] = $p;
  317. }
  318. }
  319. }
  320. if (
  321. count($this->_bodyParam['description']) ||
  322. (
  323. $this->_fullDataRequested &&
  324. $httpMethod != 'GET' &&
  325. $httpMethod != 'DELETE'
  326. )
  327. ) {
  328. $operation->parameters[] = $this->_getBody();
  329. }
  330. if (isset($m['return']['type'])) {
  331. $responseClass = $m['return']['type'];
  332. if (is_string($responseClass)) {
  333. if (class_exists($responseClass)) {
  334. $this->_model($responseClass);
  335. $operation->responseClass
  336. = $this->_noNamespace($responseClass);
  337. } elseif (strtolower($responseClass) == 'array') {
  338. $operation->responseClass = 'Array';
  339. $rt = $m['return'];
  340. if (isset(
  341. $rt[CommentParser::$embeddedDataName]['type'])
  342. ) {
  343. $rt = $rt[CommentParser::$embeddedDataName]
  344. ['type'];
  345. if (class_exists($rt)) {
  346. $this->_model($rt);
  347. $operation->responseClass .= '[' .
  348. $this->_noNamespace($rt) . ']';
  349. }
  350. }
  351. }
  352. }
  353. }
  354. $api = false;
  355. if (static::$groupOperations) {
  356. foreach ($r->apis as $a) {
  357. if ($a->path == "$prefix/$fullPath") {
  358. $api = $a;
  359. break;
  360. }
  361. }
  362. }
  363. if (!$api) {
  364. $api = $this->_api("$prefix/$fullPath", $description);
  365. $r->apis[] = $api;
  366. }
  367. $api->operations[] = $operation;
  368. }
  369. }
  370. if (!$count) {
  371. throw new RestException(404);
  372. }
  373. if (!is_null($r))
  374. $r->models = $this->_models;
  375. usort(
  376. $r->apis,
  377. function ($a, $b) {
  378. $order = array(
  379. 'GET' => 1,
  380. 'POST' => 2,
  381. 'PUT' => 3,
  382. 'PATCH' => 4,
  383. 'DELETE' => 5
  384. );
  385. return
  386. $a->operations[0]->httpMethod ==
  387. $b->operations[0]->httpMethod
  388. ? $a->path > $b->path
  389. : $order[$a->operations[0]->httpMethod] >
  390. $order[$b->operations[0]->httpMethod];
  391. }
  392. );
  393. return $r;
  394. }
  395. protected function _nickname(array $route)
  396. {
  397. static $hash = array();
  398. $method = $route['methodName'];
  399. if (isset(static::$prefixes[$method])) {
  400. $method = static::$prefixes[$method];
  401. } else {
  402. $method = str_replace(
  403. array_keys(static::$prefixes),
  404. array_values(static::$prefixes),
  405. $method
  406. );
  407. }
  408. while (isset($hash[$method]) && $route['url'] != $hash[$method]) {
  409. //create another one
  410. $method .= '_';
  411. }
  412. $hash[$method] = $route['url'];
  413. return $method;
  414. }
  415. protected function _noNamespace($className)
  416. {
  417. $className = explode('\\', $className);
  418. return end($className);
  419. }
  420. protected function _operationListing($resourcePath = '/')
  421. {
  422. $r = $this->_resourceListing();
  423. $r->resourcePath = $resourcePath;
  424. $r->models = new stdClass();
  425. return $r;
  426. }
  427. protected function _resourceListing()
  428. {
  429. $r = new stdClass();
  430. $r->apiVersion = (string)$this->restler->_requestedApiVersion;
  431. $r->swaggerVersion = "1.1";
  432. $r->basePath = $this->restler->getBaseUrl();
  433. $r->produces = $this->restler->getWritableMimeTypes();
  434. $r->consumes = $this->restler->getReadableMimeTypes();
  435. $r->apis = array();
  436. return $r;
  437. }
  438. protected function _api($path, $description = '')
  439. {
  440. $r = new stdClass();
  441. $r->path = $path;
  442. $r->description =
  443. empty($description) && $this->restler->getProductionMode()
  444. ? 'Use PHPDoc comment to describe here'
  445. : $description;
  446. $r->operations = array();
  447. return $r;
  448. }
  449. protected function _operation(
  450. $route,
  451. $nickname,
  452. $httpMethod = 'GET',
  453. $summary = 'description',
  454. $notes = 'long description',
  455. $responseClass = 'void'
  456. )
  457. {
  458. //reset body params
  459. $this->_bodyParam = array(
  460. 'required' => false,
  461. 'description' => array()
  462. );
  463. $r = new stdClass();
  464. $r->httpMethod = $httpMethod;
  465. $r->nickname = $nickname;
  466. $r->responseClass = $responseClass;
  467. $r->parameters = array();
  468. $r->summary = $summary . ($route['accessLevel'] > 2
  469. ? static::$apiDescriptionSuffixSymbols[2]
  470. : static::$apiDescriptionSuffixSymbols[$route['accessLevel']]
  471. );
  472. $r->notes = $notes;
  473. $r->errorResponses = array();
  474. return $r;
  475. }
  476. protected function _parameter($param)
  477. {
  478. $r = new stdClass();
  479. $r->name = $param['name'];
  480. $r->description = !empty($param['description'])
  481. ? $param['description'] . '.'
  482. : ($this->restler->getProductionMode()
  483. ? ''
  484. : 'add <mark>@param {type} $' . $r->name
  485. . ' {comment}</mark> to describe here');
  486. //paramType can be path or query or body or header
  487. $r->paramType = Util::nestedValue($param, CommentParser::$embeddedDataName, 'from') ? : 'query';
  488. $r->required = isset($param['required']) && $param['required'];
  489. if (isset($param['default'])) {
  490. $r->defaultValue = $param['default'];
  491. } elseif (isset($param[CommentParser::$embeddedDataName]['example'])) {
  492. $r->defaultValue
  493. = $param[CommentParser::$embeddedDataName]['example'];
  494. }
  495. $r->allowMultiple = false;
  496. $type = 'string';
  497. if (isset($param['type'])) {
  498. $type = $param['type'];
  499. if (is_array($type)) {
  500. $type = array_shift($type);
  501. }
  502. if ($type == 'array') {
  503. $contentType = Util::nestedValue(
  504. $param,
  505. CommentParser::$embeddedDataName,
  506. 'type'
  507. );
  508. if ($contentType) {
  509. if ($contentType == 'indexed') {
  510. $type = 'Array';
  511. } elseif ($contentType == 'associative') {
  512. $type = 'Object';
  513. } else {
  514. $type = "Array[$contentType]";
  515. }
  516. if (Util::isObjectOrArray($contentType)) {
  517. $this->_model($contentType);
  518. }
  519. } elseif (isset(static::$dataTypeAlias[$type])) {
  520. $type = static::$dataTypeAlias[$type];
  521. }
  522. } elseif (Util::isObjectOrArray($type)) {
  523. $this->_model($type);
  524. } elseif (isset(static::$dataTypeAlias[$type])) {
  525. $type = static::$dataTypeAlias[$type];
  526. }
  527. }
  528. $r->dataType = $type;
  529. if (isset($param[CommentParser::$embeddedDataName])) {
  530. $p = $param[CommentParser::$embeddedDataName];
  531. if (isset($p['min']) && isset($p['max'])) {
  532. $r->allowableValues = array(
  533. 'valueType' => 'RANGE',
  534. 'min' => $p['min'],
  535. 'max' => $p['max'],
  536. );
  537. } elseif (isset($p['choice'])) {
  538. $r->allowableValues = array(
  539. 'valueType' => 'LIST',
  540. 'values' => $p['choice']
  541. );
  542. }
  543. }
  544. return $r;
  545. }
  546. protected function _appendToBody($p)
  547. {
  548. if ($p->name === Defaults::$fullRequestDataName) {
  549. $this->_fullDataRequested = $p;
  550. unset($this->_bodyParam['names'][Defaults::$fullRequestDataName]);
  551. return;
  552. }
  553. $this->_bodyParam['description'][$p->name]
  554. = "$p->name"
  555. . ' : <tag>' . $p->dataType . '</tag> '
  556. . ($p->required ? ' <i>(required)</i> - ' : ' - ')
  557. . $p->description;
  558. $this->_bodyParam['required'] = $p->required
  559. || $this->_bodyParam['required'];
  560. $this->_bodyParam['names'][$p->name] = $p;
  561. }
  562. protected function _getBody()
  563. {
  564. $r = new stdClass();
  565. $n = isset($this->_bodyParam['names'])
  566. ? array_values($this->_bodyParam['names'])
  567. : array();
  568. if (count($n) == 1) {
  569. if (isset($this->_models->{$n[0]->dataType})) {
  570. // ============ custom class ===================
  571. $r = $n[0];
  572. $c = $this->_models->{$r->dataType};
  573. $a = $c->properties;
  574. $r->description = "Paste JSON data here";
  575. if (count($a)) {
  576. $r->description .= " with the following"
  577. . (count($a) > 1 ? ' properties.' : ' property.');
  578. foreach ($a as $k => $v) {
  579. $r->description .= "<hr/>$k : <tag>"
  580. . $v['type'] . '</tag> '
  581. . (isset($v['required']) ? '(required)' : '')
  582. . ' - ' . $v['description'];
  583. }
  584. }
  585. $r->defaultValue = "{\n \""
  586. . implode("\": \"\",\n \"",
  587. array_keys($c->properties))
  588. . "\": \"\"\n}";
  589. return $r;
  590. } elseif (false !== ($p = strpos($n[0]->dataType, '['))) {
  591. // ============ array of custom class ===============
  592. $r = $n[0];
  593. $t = substr($r->dataType, $p + 1, -1);
  594. if ($c = Util::nestedValue($this->_models, $t)) {
  595. $a = $c->properties;
  596. $r->description = "Paste JSON data here";
  597. if (count($a)) {
  598. $r->description .= " with an array of objects with the following"
  599. . (count($a) > 1 ? ' properties.' : ' property.');
  600. foreach ($a as $k => $v) {
  601. $r->description .= "<hr/>$k : <tag>"
  602. . $v['type'] . '</tag> '
  603. . (isset($v['required']) ? '(required)' : '')
  604. . ' - ' . $v['description'];
  605. }
  606. }
  607. $r->defaultValue = "[\n {\n \""
  608. . implode("\": \"\",\n \"",
  609. array_keys($c->properties))
  610. . "\": \"\"\n }\n]";
  611. return $r;
  612. } else {
  613. $r->description = "Paste JSON data here with an array of $t values.";
  614. $r->defaultValue = "[ ]";
  615. return $r;
  616. }
  617. } elseif ($n[0]->dataType == 'Array') {
  618. // ============ array ===============================
  619. $r = $n[0];
  620. $r->description = "Paste JSON array data here"
  621. . ($r->required ? ' (required) . ' : '. ')
  622. . "<br/>$r->description";
  623. $r->defaultValue = "[\n {\n \""
  624. . "property\" : \"\"\n }\n]";
  625. return $r;
  626. } elseif ($n[0]->dataType == 'Object') {
  627. // ============ object ==============================
  628. $r = $n[0];
  629. $r->description = "Paste JSON object data here"
  630. . ($r->required ? ' (required) . ' : '. ')
  631. . "<br/>$r->description";
  632. $r->defaultValue = "{\n \""
  633. . "property\" : \"\"\n}";
  634. return $r;
  635. }
  636. }
  637. $p = array_values($this->_bodyParam['description']);
  638. $r->name = 'REQUEST_BODY';
  639. $r->description = "Paste JSON data here";
  640. if (count($p) == 0 && $this->_fullDataRequested) {
  641. $r->required = $this->_fullDataRequested->required;
  642. $r->defaultValue = "{\n \"property\" : \"\"\n}";
  643. } else {
  644. $r->description .= " with the following"
  645. . (count($p) > 1 ? ' properties.' : ' property.')
  646. . '<hr/>'
  647. . implode("<hr/>", $p);
  648. $r->required = $this->_bodyParam['required'];
  649. // Create default object that includes parameters to be submitted
  650. $defaultObject = new \StdClass();
  651. foreach ($this->_bodyParam['names'] as $name => $values) {
  652. if (!$values->required)
  653. continue;
  654. if (class_exists($values->dataType)) {
  655. $myClassName = $values->dataType;
  656. $defaultObject->$name = new $myClassName();
  657. } else {
  658. $defaultObject->$name = '';
  659. }
  660. }
  661. $r->defaultValue = Scope::get('JsonFormat')->encode($defaultObject, true);
  662. }
  663. $r->paramType = 'body';
  664. $r->allowMultiple = false;
  665. $r->dataType = 'Object';
  666. return $r;
  667. }
  668. protected function _model($className, $instance = null)
  669. {
  670. $id = $this->_noNamespace($className);
  671. if (isset($this->_models->{$id})) {
  672. return;
  673. }
  674. $properties = array();
  675. if (!$instance) {
  676. if (!class_exists($className))
  677. return;
  678. $instance = new $className();
  679. }
  680. $data = get_object_vars($instance);
  681. $reflectionClass = new \ReflectionClass($className);
  682. foreach ($data as $key => $value) {
  683. $propertyMetaData = null;
  684. try {
  685. $property = $reflectionClass->getProperty($key);
  686. if ($c = $property->getDocComment()) {
  687. $propertyMetaData = Util::nestedValue(
  688. CommentParser::parse($c),
  689. 'var'
  690. );
  691. }
  692. } catch (\ReflectionException $e) {
  693. }
  694. if (is_null($propertyMetaData)) {
  695. $type = $this->getType($value, true);
  696. $description = '';
  697. } else {
  698. $type = Util::nestedValue(
  699. $propertyMetaData,
  700. 'type'
  701. ) ? : $this->getType($value, true);
  702. $description = Util::nestedValue(
  703. $propertyMetaData,
  704. 'description'
  705. ) ? : '';
  706. if (class_exists($type)) {
  707. $this->_model($type);
  708. $type = $this->_noNamespace($type);
  709. }
  710. }
  711. if (isset(static::$dataTypeAlias[$type])) {
  712. $type = static::$dataTypeAlias[$type];
  713. }
  714. $properties[$key] = array(
  715. 'type' => $type,
  716. 'description' => $description
  717. );
  718. if (Util::nestedValue(
  719. $propertyMetaData,
  720. CommentParser::$embeddedDataName,
  721. 'required'
  722. )
  723. ) {
  724. $properties[$key]['required'] = true;
  725. }
  726. if ($type == 'Array') {
  727. $itemType = Util::nestedValue(
  728. $propertyMetaData,
  729. CommentParser::$embeddedDataName,
  730. 'type'
  731. ) ? :
  732. (count($value)
  733. ? $this->getType(end($value), true)
  734. : 'string');
  735. if (class_exists($itemType)) {
  736. $this->_model($itemType);
  737. $itemType = $this->_noNamespace($itemType);
  738. }
  739. $properties[$key]['items'] = array(
  740. 'type' => $itemType,
  741. /*'description' => '' */ //TODO: add description
  742. );
  743. } else if (preg_match('/^Array\[(.+)\]$/', $type, $matches)) {
  744. $itemType = $matches[1];
  745. $properties[$key]['type'] = 'Array';
  746. $properties[$key]['items']['type'] = $this->_noNamespace($itemType);
  747. if (class_exists($itemType)) {
  748. $this->_model($itemType);
  749. }
  750. }
  751. }
  752. if (!empty($properties)) {
  753. $model = new stdClass();
  754. $model->id = $id;
  755. $model->properties = $properties;
  756. $this->_models->{$id} = $model;
  757. }
  758. }
  759. /**
  760. * Find the data type of the given value.
  761. *
  762. *
  763. * @param mixed $o given value for finding type
  764. *
  765. * @param bool $appendToModels if an object is found should we append to
  766. * our models list?
  767. *
  768. * @return string
  769. *
  770. * @access private
  771. */
  772. public function getType($o, $appendToModels = false)
  773. {
  774. if (is_object($o)) {
  775. $oc = get_class($o);
  776. if ($appendToModels) {
  777. $this->_model($oc, $o);
  778. }
  779. return $this->_noNamespace($oc);
  780. }
  781. if (is_array($o)) {
  782. if (count($o)) {
  783. $child = end($o);
  784. if (Util::isObjectOrArray($child)) {
  785. $childType = $this->getType($child, $appendToModels);
  786. return "Array[$childType]";
  787. }
  788. }
  789. return 'array';
  790. }
  791. if (is_bool($o)) return 'boolean';
  792. if (is_numeric($o)) return is_float($o) ? 'float' : 'int';
  793. return 'string';
  794. }
  795. /**
  796. * pre call for index()
  797. *
  798. * if cache is present, use cache
  799. */
  800. public function _pre_index_json()
  801. {
  802. $userClass = Defaults::$userIdentifierClass;
  803. $this->cacheName = $userClass::getCacheIdentifier()
  804. . '_resources-v'
  805. . $this->restler->_requestedApiVersion;
  806. if ($this->restler->getProductionMode()
  807. && !$this->restler->refreshCache
  808. && $this->restler->cache->isCached($this->cacheName)
  809. ) {
  810. //by pass call, compose, postCall stages and directly send response
  811. $this->restler->composeHeaders();
  812. die($this->restler->cache->get($this->cacheName));
  813. }
  814. }
  815. /**
  816. * post call for index()
  817. *
  818. * create cache if in production mode
  819. *
  820. * @param $responseData
  821. *
  822. * @internal param string $data composed json output
  823. *
  824. * @return string
  825. */
  826. public function _post_index_json($responseData)
  827. {
  828. if ($this->restler->getProductionMode()) {
  829. $this->restler->cache->set($this->cacheName, $responseData);
  830. }
  831. return $responseData;
  832. }
  833. /**
  834. * @access hybrid
  835. * @return \stdClass
  836. */
  837. public function index()
  838. {
  839. if (!static::$accessControlFunction && Defaults::$accessControlFunction)
  840. static::$accessControlFunction = Defaults::$accessControlFunction;
  841. $version = $this->restler->getRequestedApiVersion();
  842. $allRoutes = Util::nestedValue(Routes::toArray(), "v$version");
  843. $r = $this->_resourceListing();
  844. $map = array();
  845. if (isset($allRoutes['*'])) {
  846. $this->_mapResources($allRoutes['*'], $map, $version);
  847. unset($allRoutes['*']);
  848. }
  849. $this->_mapResources($allRoutes, $map, $version);
  850. foreach ($map as $path => $description) {
  851. if (!Text::contains($path, '{')) {
  852. //add id
  853. $r->apis[] = array(
  854. 'path' => $path . $this->formatString,
  855. 'description' => $description
  856. );
  857. }
  858. }
  859. if (Defaults::$useUrlBasedVersioning && static::$listHigherVersions) {
  860. $nextVersion = $version + 1;
  861. if ($nextVersion <= $this->restler->getApiVersion()) {
  862. list($status, $data) = $this->_loadResource("/v$nextVersion/resources.json");
  863. if ($status == 200) {
  864. $r->apis = array_merge($r->apis, $data->apis);
  865. $r->apiVersion = $data->apiVersion;
  866. }
  867. }
  868. }
  869. return $r;
  870. }
  871. protected function _loadResource($url)
  872. {
  873. $ch = curl_init($this->restler->getBaseUrl() . $url
  874. . (empty($_GET) ? '' : '?' . http_build_query($_GET)));
  875. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  876. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  877. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  878. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  879. 'Accept:application/json',
  880. ));
  881. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  882. $result = json_decode(curl_exec($ch));
  883. $http_status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
  884. return array($http_status, $result);
  885. }
  886. protected function _mapResources(array $allRoutes, array &$map, $version = 1)
  887. {
  888. foreach ($allRoutes as $fullPath => $routes) {
  889. $path = explode('/', $fullPath);
  890. $resource = isset($path[0]) ? $path[0] : '';
  891. if ($resource == 'resources' || Text::endsWith($resource, 'index'))
  892. continue;
  893. foreach ($routes as $httpMethod => $route) {
  894. if (in_array($httpMethod, static::$excludedHttpMethods)) {
  895. continue;
  896. }
  897. if (!static::verifyAccess($route)) {
  898. continue;
  899. }
  900. foreach (static::$excludedPaths as $exclude) {
  901. if (empty($exclude)) {
  902. if ($fullPath == $exclude)
  903. continue 2;
  904. } elseif (Text::beginsWith($fullPath, $exclude)) {
  905. continue 2;
  906. }
  907. }
  908. $res = $resource
  909. ? ($version == 1 ? "/resources/$resource" : "/v$version/resources/$resource-v$version")
  910. : ($version == 1 ? "/resources/root" : "/v$version/resources/root-v$version");
  911. if (empty($map[$res])) {
  912. $map[$res] = isset(
  913. $route['metadata']['classDescription'])
  914. ? $route['metadata']['classDescription'] : '';
  915. }
  916. }
  917. }
  918. }
  919. /**
  920. * Maximum api version supported by the api class
  921. * @return int
  922. */
  923. public static function __getMaximumSupportedVersion()
  924. {
  925. return Scope::get('Restler')->getApiVersion();
  926. }
  927. /**
  928. * Verifies that the requesting user is allowed to view the docs for this API
  929. *
  930. * @param $route
  931. *
  932. * @return boolean True if the user should be able to view this API's docs
  933. */
  934. protected function verifyAccess($route)
  935. {
  936. if ($route['accessLevel'] < 2) {
  937. return true;
  938. }
  939. if (
  940. static::$hideProtected
  941. && !$this->_authenticated
  942. && $route['accessLevel'] > 1
  943. ) {
  944. return false;
  945. }
  946. if ($this->_authenticated
  947. && static::$accessControlFunction
  948. && (!call_user_func(
  949. static::$accessControlFunction, $route['metadata']))
  950. ) {
  951. return false;
  952. }
  953. return true;
  954. }
  955. }