basics.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. use Cake\Core\Configure;
  16. use Cake\Error\Debugger;
  17. use Cake\I18n\I18n;
  18. /**
  19. * Basic defines for timing functions.
  20. */
  21. define('SECOND', 1);
  22. define('MINUTE', 60);
  23. define('HOUR', 3600);
  24. define('DAY', 86400);
  25. define('WEEK', 604800);
  26. define('MONTH', 2592000);
  27. define('YEAR', 31536000);
  28. if (!function_exists('debug')) {
  29. /**
  30. * Prints out debug information about given variable.
  31. *
  32. * Only runs if debug level is greater than zero.
  33. *
  34. * @param mixed $var Variable to show debug information for.
  35. * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  36. * @param bool $showFrom If set to true, the method prints from where the function was called.
  37. * @return void
  38. * @link http://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
  39. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
  40. */
  41. function debug($var, $showHtml = null, $showFrom = true)
  42. {
  43. if (!Configure::read('debug')) {
  44. return;
  45. }
  46. $file = '';
  47. $line = '';
  48. $lineInfo = '';
  49. if ($showFrom) {
  50. $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
  51. $search = [ROOT];
  52. if (defined('CAKE_CORE_INCLUDE_PATH')) {
  53. array_unshift($search, CAKE_CORE_INCLUDE_PATH);
  54. }
  55. $file = str_replace($search, '', $trace[0]['file']);
  56. $line = $trace[0]['line'];
  57. }
  58. $html = <<<HTML
  59. <div class="cake-debug-output">
  60. %s
  61. <pre class="cake-debug">
  62. %s
  63. </pre>
  64. </div>
  65. HTML;
  66. $text = <<<TEXT
  67. %s
  68. ########## DEBUG ##########
  69. %s
  70. ###########################
  71. TEXT;
  72. $template = $html;
  73. if (php_sapi_name() === 'cli' || $showHtml === false) {
  74. $template = $text;
  75. if ($showFrom) {
  76. $lineInfo = sprintf('%s (line %s)', $file, $line);
  77. }
  78. }
  79. if ($showHtml === null && $template !== $text) {
  80. $showHtml = true;
  81. }
  82. $var = Debugger::exportVar($var, 25);
  83. if ($showHtml) {
  84. $template = $html;
  85. $var = h($var);
  86. if ($showFrom) {
  87. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  88. }
  89. }
  90. printf($template, $lineInfo, $var);
  91. }
  92. }
  93. if (!function_exists('stackTrace')) {
  94. /**
  95. * Outputs a stack trace based on the supplied options.
  96. *
  97. * ### Options
  98. *
  99. * - `depth` - The number of stack frames to return. Defaults to 999
  100. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  101. * will be displayed.
  102. * - `start` - The stack frame to start generating a trace from. Defaults to 1
  103. *
  104. * @param array $options Format for outputting stack trace
  105. * @return mixed Formatted stack trace
  106. * @see Debugger::trace()
  107. */
  108. function stackTrace(array $options = [])
  109. {
  110. if (!Configure::read('debug')) {
  111. return;
  112. }
  113. $options += ['start' => 0];
  114. $options['start']++;
  115. echo Debugger::trace($options);
  116. }
  117. }
  118. if (!function_exists('__')) {
  119. /**
  120. * Returns a translated string if one is found; Otherwise, the submitted message.
  121. *
  122. * @param string $singular Text to translate
  123. * @param mixed $args Array with arguments or multiple arguments in function
  124. * @return mixed translated string
  125. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  126. */
  127. function __($singular, $args = null)
  128. {
  129. if (!$singular) {
  130. return;
  131. }
  132. $arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
  133. return I18n::translator()->translate($singular, $arguments);
  134. }
  135. }
  136. if (!function_exists('__n')) {
  137. /**
  138. * Returns correct plural form of message identified by $singular and $plural for count $count.
  139. * Some languages have more than one form for plural messages dependent on the count.
  140. *
  141. * @param string $singular Singular text to translate
  142. * @param string $plural Plural text
  143. * @param int $count Count
  144. * @param mixed $args Array with arguments or multiple arguments in function
  145. * @return mixed plural form of translated string
  146. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__n
  147. */
  148. function __n($singular, $plural, $count, $args = null)
  149. {
  150. if (!$singular) {
  151. return;
  152. }
  153. $arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
  154. return I18n::translator()->translate(
  155. $plural,
  156. ['_count' => $count, '_singular' => $singular] + $arguments
  157. );
  158. }
  159. }
  160. if (!function_exists('__d')) {
  161. /**
  162. * Allows you to override the current domain for a single message lookup.
  163. *
  164. * @param string $domain Domain
  165. * @param string $msg String to translate
  166. * @param mixed $args Array with arguments or multiple arguments in function
  167. * @return string translated string
  168. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__d
  169. */
  170. function __d($domain, $msg, $args = null)
  171. {
  172. if (!$msg) {
  173. return;
  174. }
  175. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  176. return I18n::translator($domain)->translate($msg, $arguments);
  177. }
  178. }
  179. if (!function_exists('__dn')) {
  180. /**
  181. * Allows you to override the current domain for a single plural message lookup.
  182. * Returns correct plural form of message identified by $singular and $plural for count $count
  183. * from domain $domain.
  184. *
  185. * @param string $domain Domain
  186. * @param string $singular Singular string to translate
  187. * @param string $plural Plural
  188. * @param int $count Count
  189. * @param mixed $args Array with arguments or multiple arguments in function
  190. * @return string plural form of translated string
  191. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dn
  192. */
  193. function __dn($domain, $singular, $plural, $count, $args = null)
  194. {
  195. if (!$singular) {
  196. return;
  197. }
  198. $arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
  199. return I18n::translator($domain)->translate(
  200. $plural,
  201. ['_count' => $count, '_singular' => $singular] + $arguments
  202. );
  203. }
  204. }
  205. if (!function_exists('__x')) {
  206. /**
  207. * Returns a translated string if one is found; Otherwise, the submitted message.
  208. * The context is a unique identifier for the translations string that makes it unique
  209. * for in the same domain.
  210. *
  211. * @param string $context Context of the text
  212. * @param string $singular Text to translate
  213. * @param mixed $args Array with arguments or multiple arguments in function
  214. * @return mixed translated string
  215. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  216. */
  217. function __x($context, $singular, $args = null)
  218. {
  219. if (!$singular) {
  220. return;
  221. }
  222. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  223. return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
  224. }
  225. }
  226. if (!function_exists('__xn')) {
  227. /**
  228. * Returns correct plural form of message identified by $singular and $plural for count $count.
  229. * Some languages have more than one form for plural messages dependent on the count.
  230. * The context is a unique identifier for the translations string that makes it unique
  231. * for in the same domain.
  232. *
  233. * @param string $context Context of the text
  234. * @param string $singular Singular text to translate
  235. * @param string $plural Plural text
  236. * @param int $count Count
  237. * @param mixed $args Array with arguments or multiple arguments in function
  238. * @return mixed plural form of translated string
  239. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__xn
  240. */
  241. function __xn($context, $singular, $plural, $count, $args = null)
  242. {
  243. if (!$singular) {
  244. return;
  245. }
  246. $arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 2);
  247. return I18n::translator()->translate(
  248. $singular,
  249. ['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
  250. );
  251. }
  252. }
  253. if (!function_exists('__dx')) {
  254. /**
  255. * Allows you to override the current domain for a single message lookup.
  256. * The context is a unique identifier for the translations string that makes it unique
  257. * for in the same domain.
  258. *
  259. * @param string $domain Domain
  260. * @param string $context Context of the text
  261. * @param string $msg String to translate
  262. * @param mixed $args Array with arguments or multiple arguments in function
  263. * @return string translated string
  264. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dx
  265. */
  266. function __dx($domain, $context, $msg, $args = null)
  267. {
  268. if (!$msg) {
  269. return;
  270. }
  271. $arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 2);
  272. return I18n::translator($domain)->translate(
  273. $msg,
  274. ['_context' => $context] + $arguments
  275. );
  276. }
  277. }
  278. if (!function_exists('__dxn')) {
  279. /**
  280. * Returns correct plural form of message identified by $singular and $plural for count $count.
  281. * Allows you to override the current domain for a single message lookup.
  282. * The context is a unique identifier for the translations string that makes it unique
  283. * for in the same domain.
  284. *
  285. * @param string $domain Domain
  286. * @param string $context Context of the text
  287. * @param string $singular Singular text to translate
  288. * @param string $plural Plural text
  289. * @param int $count Count
  290. * @param mixed $args Array with arguments or multiple arguments in function
  291. * @return mixed plural form of translated string
  292. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dxn
  293. */
  294. function __dxn($domain, $context, $singular, $plural, $count, $args = null)
  295. {
  296. if (!$singular) {
  297. return;
  298. }
  299. $arguments = func_num_args() === 6 ? (array)$args : array_slice(func_get_args(), 2);
  300. return I18n::translator($domain)->translate(
  301. $singular,
  302. ['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
  303. );
  304. }
  305. }