Defaults.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace Luracast\Restler;
  3. use Luracast\Restler\Data\ValidationInfo;
  4. use Luracast\Restler\Data\Validator;
  5. /**
  6. * Static class to hold all restler defaults, change the values to suit your
  7. * needs in the gateway file (index.php), you may also allow the api users to
  8. * change them per request by adding the properties to Defaults::$overridables
  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 Defaults
  19. {
  20. // ==================================================================
  21. //
  22. // Class Mappings
  23. //
  24. // ------------------------------------------------------------------
  25. /**
  26. * @var string of name of the class that implements
  27. * \Luracast\Restler\iCache the cache class to be used
  28. */
  29. public static $cacheClass = 'Luracast\\Restler\\HumanReadableCache';
  30. /**
  31. * @var string full path of the directory where all the generated files will
  32. * be kept. When set to null (default) it will use the cache folder that is
  33. * in the same folder as index.php (gateway)
  34. */
  35. public static $cacheDirectory;
  36. /**
  37. * @var string of name of the class that implements
  38. * \Luracast\Restler\Data\iValidate the validator class to be used
  39. */
  40. public static $validatorClass = 'Luracast\\Restler\\Data\\Validator';
  41. /**
  42. * @var string name of the class that implements \Luracast\Restler\iCompose
  43. * the class to be used to compose the response
  44. */
  45. public static $composeClass = 'Luracast\\Restler\\Compose';
  46. // ==================================================================
  47. //
  48. // Routing
  49. //
  50. // ------------------------------------------------------------------
  51. /**
  52. * @var bool should auto routing for public and protected api methods
  53. * should be enabled by default or not. Set this to false to get
  54. * Restler 1.0 style behavior
  55. */
  56. public static $autoRoutingEnabled = true;
  57. /**
  58. * @var boolean avoids creating multiple routes that can increase the
  59. * ambiguity when set to true. when a method parameter is optional it is
  60. * not mapped to the url and should only be used in request body or as
  61. * query string `/resource?id=value`. When a parameter is required and is
  62. * scalar, it will be mapped as part of the url `/resource/{id}`
  63. */
  64. public static $smartAutoRouting = true;
  65. /**
  66. * @var boolean enables more ways of finding the parameter data in the request.
  67. * If you need backward compatibility with Restler 2 or below turn this off
  68. */
  69. public static $smartParameterParsing = true;
  70. // ==================================================================
  71. //
  72. // API Version Management
  73. //
  74. // ------------------------------------------------------------------
  75. /**
  76. * @var null|string name that is used for vendor specific media type and
  77. * api version using the Accept Header for example
  78. * application/vnd.{vendor}-v1+json
  79. *
  80. * Keep this null if you do not want to use vendor MIME for specifying api version
  81. */
  82. public static $apiVendor = null;
  83. /**
  84. * @var bool set it to true to force vendor specific MIME for versioning.
  85. * It will be automatically set to true when Defaults::$vendor is not
  86. * null and client is requesting for the custom MIME type
  87. */
  88. public static $useVendorMIMEVersioning = false;
  89. /**
  90. * @var bool set it to true to use enableUrl based versioning
  91. */
  92. public static $useUrlBasedVersioning = false;
  93. // ==================================================================
  94. //
  95. // Request
  96. //
  97. // ------------------------------------------------------------------
  98. /**
  99. * @var string name to be used for the method parameter to capture the
  100. * entire request data
  101. */
  102. public static $fullRequestDataName = 'request_data';
  103. /**
  104. * @var string name of the property that can sent through $_GET or $_POST to
  105. * override the http method of the request. Set it to null or
  106. * blank string to disable http method override through request
  107. * parameters.
  108. */
  109. public static $httpMethodOverrideProperty = 'http_method';
  110. /**
  111. * @var bool should auto validating api parameters should be enabled by
  112. * default or not. Set this to false to avoid validation.
  113. */
  114. public static $autoValidationEnabled = true;
  115. /**
  116. * @var string name of the class that implements iUser interface to identify
  117. * the user for caching purposes
  118. */
  119. public static $userIdentifierClass = 'Luracast\\Restler\\User';
  120. // ==================================================================
  121. //
  122. // Response
  123. //
  124. // ------------------------------------------------------------------
  125. /**
  126. * @var bool HTTP status codes are set on all responses by default.
  127. * Some clients (like flash, mobile) have trouble dealing with non-200
  128. * status codes on error responses.
  129. *
  130. * You can set it to true to force a HTTP 200 status code on all responses,
  131. * even when errors occur. If you suppress status codes, look for an error
  132. * response to determine if an error occurred.
  133. */
  134. public static $suppressResponseCode = false;
  135. public static $supportedCharsets = array('utf-8', 'iso-8859-1');
  136. public static $supportedLanguages = array('en', 'en-US');
  137. public static $charset = 'utf-8';
  138. public static $language = 'en';
  139. /**
  140. * @var bool when set to true, it will exclude the response body
  141. */
  142. public static $emptyBodyForNullResponse = true;
  143. /**
  144. * @var bool when set to true, the response will not be outputted directly into the buffer.
  145. * If set, Restler::handle() will return the response as a string.
  146. */
  147. public static $returnResponse = false;
  148. /**
  149. * @var bool enables CORS support
  150. */
  151. public static $crossOriginResourceSharing = false;
  152. public static $accessControlAllowOrigin = '*';
  153. public static $accessControlAllowMethods =
  154. 'GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD';
  155. // ==================================================================
  156. //
  157. // Header
  158. //
  159. // ------------------------------------------------------------------
  160. /**
  161. * @var array default Cache-Control template that used to set the
  162. * Cache-Control header and has two values, first one is used when
  163. * Defaults::$headerExpires is 0 and second one when it has some time
  164. * value specified. When only one value is specified it will be used for
  165. * both cases
  166. */
  167. public static $headerCacheControl = array(
  168. 'no-cache, must-revalidate',
  169. /* "public, " or "private, " will be prepended based on api method
  170. * called (public or protected)
  171. */
  172. 'max-age={expires}, must-revalidate',
  173. );
  174. /**
  175. * @var int sets the content to expire immediately when set to zero
  176. * alternatively you can specify the number of seconds the content will
  177. * expire. This setting can be altered at api level using php doc comment
  178. * with @expires numOfSeconds
  179. */
  180. public static $headerExpires = 0;
  181. // ==================================================================
  182. //
  183. // Access Control
  184. //
  185. // ------------------------------------------------------------------
  186. /**
  187. * @var null|callable if the api methods are under access control mechanism
  188. * you can attach a function here that returns true or false to determine
  189. * visibility of a protected api method. this function will receive method
  190. * info as the only parameter.
  191. */
  192. public static $accessControlFunction = null;
  193. /**
  194. * @var int set the default api access mode
  195. * value of 0 = public api
  196. * value of 1 = hybrid api using `@access hybrid` comment
  197. * value of 2 = protected api using `@access protected` comment
  198. * value of 3 = protected api using `protected function` method
  199. */
  200. public static $apiAccessLevel = 0;
  201. /**
  202. * @var string authentication method to be called in iAuthenticate
  203. * Interface
  204. */
  205. public static $authenticationMethod = '__isAllowed';
  206. /**
  207. * @var int time in milliseconds for bandwidth throttling,
  208. * which is the minimum response time for each api request. You can
  209. * change it per api method by setting `@throttle 3000` in php doc
  210. * comment either at the method level or class level
  211. */
  212. public static $throttle = 0;
  213. // ==================================================================
  214. //
  215. // Overrides for API User
  216. //
  217. // ------------------------------------------------------------------
  218. /**
  219. * @var array use 'alternativeName'=> 'actualName' to set alternative
  220. * names that can be used to represent the api method parameters and/or
  221. * static properties of Defaults
  222. */
  223. public static $aliases = array(
  224. /**
  225. * suppress_response_codes=true as an URL parameter to force
  226. * a HTTP 200 status code on all responses
  227. */
  228. 'suppress_response_codes' => 'suppressResponseCode',
  229. );
  230. /**
  231. * @var array determines the defaults that can be overridden by the api
  232. * user by passing them as URL parameters
  233. */
  234. public static $overridables = array(
  235. 'suppressResponseCode',
  236. );
  237. /**
  238. * @var array contains validation details for defaults to be used when
  239. * set through URL parameters
  240. */
  241. public static $validation = array(
  242. 'suppressResponseCode' => array('type' => 'bool'),
  243. 'headerExpires' => array('type' => 'int', 'min' => 0),
  244. );
  245. // ==================================================================
  246. //
  247. // Overrides API Developer
  248. //
  249. // ------------------------------------------------------------------
  250. /**
  251. * @var array determines what are the phpdoc comment tags that will
  252. * override the Defaults here with their values
  253. */
  254. public static $fromComments = array(
  255. /**
  256. * use PHPDoc comments such as the following
  257. * `
  258. *
  259. * @cache no-cache, must-revalidate` to set the Cache-Control header
  260. * for a specific api method
  261. */
  262. 'cache' => 'headerCacheControl',
  263. /**
  264. * use PHPDoc comments such as the following
  265. * `
  266. *
  267. * @expires 50` to set the Expires header
  268. * for a specific api method
  269. */
  270. 'expires' => 'headerExpires',
  271. /**
  272. * use PHPDoc comments such as the following
  273. * `
  274. *
  275. * @throttle 300`
  276. * to set the bandwidth throttling for 300 milliseconds
  277. * for a specific api method
  278. */
  279. 'throttle' => 'throttle',
  280. /**
  281. * enable or disable smart auto routing from method comments
  282. * this one is hardwired so cant be turned off
  283. * it is placed here just for documentation purpose
  284. */
  285. 'smart-auto-routing' => 'smartAutoRouting',
  286. );
  287. // ==================================================================
  288. //
  289. // Util
  290. //
  291. // ------------------------------------------------------------------
  292. /**
  293. * Use this method to set value to a static properly of Defaults when
  294. * you want to make sure only proper values are taken in with the help of
  295. * validation
  296. *
  297. * @static
  298. *
  299. * @param string $name name of the static property
  300. * @param mixed $value value to set the property to
  301. *
  302. * @return bool
  303. */
  304. public static function setProperty($name, $value)
  305. {
  306. if (!property_exists(__CLASS__, $name)) return false;
  307. if (@is_array(Defaults::$validation[$name])) {
  308. $info = new ValidationInfo(Defaults::$validation[$name]);
  309. $value = Validator::validate($value, $info);
  310. }
  311. Defaults::$$name = $value;
  312. return true;
  313. }
  314. }