basics.php 10 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. if (!Configure::read('debug')) {
  43. return;
  44. }
  45. $file = '';
  46. $line = '';
  47. $lineInfo = '';
  48. if ($showFrom) {
  49. $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
  50. $search = array(ROOT);
  51. if (defined('CAKE_CORE_INCLUDE_PATH')) {
  52. array_unshift($search, CAKE_CORE_INCLUDE_PATH);
  53. }
  54. $file = str_replace($search, '', $trace[0]['file']);
  55. $line = $trace[0]['line'];
  56. }
  57. $html = <<<HTML
  58. <div class="cake-debug-output">
  59. %s
  60. <pre class="cake-debug">
  61. %s
  62. </pre>
  63. </div>
  64. HTML;
  65. $text = <<<TEXT
  66. %s
  67. ########## DEBUG ##########
  68. %s
  69. ###########################
  70. TEXT;
  71. $template = $html;
  72. if (php_sapi_name() === 'cli' || $showHtml === false) {
  73. $template = $text;
  74. if ($showFrom) {
  75. $lineInfo = sprintf('%s (line %s)', $file, $line);
  76. }
  77. }
  78. if ($showHtml === null && $template !== $text) {
  79. $showHtml = true;
  80. }
  81. $var = Debugger::exportVar($var, 25);
  82. if ($showHtml) {
  83. $template = $html;
  84. $var = h($var);
  85. if ($showFrom) {
  86. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  87. }
  88. }
  89. printf($template, $lineInfo, $var);
  90. }
  91. }
  92. if (!function_exists('stackTrace')) {
  93. /**
  94. * Outputs a stack trace based on the supplied options.
  95. *
  96. * ### Options
  97. *
  98. * - `depth` - The number of stack frames to return. Defaults to 999
  99. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  100. * will be displayed.
  101. * - `start` - The stack frame to start generating a trace from. Defaults to 1
  102. *
  103. * @param array $options Format for outputting stack trace
  104. * @return mixed Formatted stack trace
  105. * @see Debugger::trace()
  106. */
  107. function stackTrace(array $options = array()) {
  108. if (!Configure::read('debug')) {
  109. return;
  110. }
  111. $options += array('start' => 0);
  112. $options['start']++;
  113. echo Debugger::trace($options);
  114. }
  115. }
  116. if (!function_exists('__')) {
  117. /**
  118. * Returns a translated string if one is found; Otherwise, the submitted message.
  119. *
  120. * @param string $singular Text to translate
  121. * @param mixed $args Array with arguments or multiple arguments in function
  122. * @return mixed translated string
  123. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  124. */
  125. function __($singular, $args = null) {
  126. if (!$singular) {
  127. return;
  128. }
  129. $arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
  130. return I18n::translator()->translate($singular, $arguments);
  131. }
  132. }
  133. if (!function_exists('__n')) {
  134. /**
  135. * Returns correct plural form of message identified by $singular and $plural for count $count.
  136. * Some languages have more than one form for plural messages dependent on the count.
  137. *
  138. * @param string $singular Singular text to translate
  139. * @param string $plural Plural text
  140. * @param int $count Count
  141. * @param mixed $args Array with arguments or multiple arguments in function
  142. * @return mixed plural form of translated string
  143. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__n
  144. */
  145. function __n($singular, $plural, $count, $args = null) {
  146. if (!$singular) {
  147. return;
  148. }
  149. $arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
  150. return I18n::translator()->translate(
  151. $plural,
  152. ['_count' => $count, '_singular' => $singular] + $arguments
  153. );
  154. }
  155. }
  156. if (!function_exists('__d')) {
  157. /**
  158. * Allows you to override the current domain for a single message lookup.
  159. *
  160. * @param string $domain Domain
  161. * @param string $msg String to translate
  162. * @param mixed $args Array with arguments or multiple arguments in function
  163. * @return string translated string
  164. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__d
  165. */
  166. function __d($domain, $msg, $args = null) {
  167. if (!$msg) {
  168. return;
  169. }
  170. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  171. return I18n::translator($domain)->translate($msg, $arguments);
  172. }
  173. }
  174. if (!function_exists('__dn')) {
  175. /**
  176. * Allows you to override the current domain for a single plural message lookup.
  177. * Returns correct plural form of message identified by $singular and $plural for count $count
  178. * from domain $domain.
  179. *
  180. * @param string $domain Domain
  181. * @param string $singular Singular string to translate
  182. * @param string $plural Plural
  183. * @param int $count Count
  184. * @param mixed $args Array with arguments or multiple arguments in function
  185. * @return string plural form of translated string
  186. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dn
  187. */
  188. function __dn($domain, $singular, $plural, $count, $args = null) {
  189. if (!$singular) {
  190. return;
  191. }
  192. $arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
  193. return I18n::translator($domain)->translate(
  194. $plural,
  195. ['_count' => $count, '_singular' => $singular] + $arguments
  196. );
  197. }
  198. }
  199. if (!function_exists('__x')) {
  200. /**
  201. * Returns a translated string if one is found; Otherwise, the submitted message.
  202. * The context is a unique identifier for the translations string that makes it unique
  203. * for in the same domain.
  204. *
  205. * @param string $context Context of the text
  206. * @param string $singular Text to translate
  207. * @param mixed $args Array with arguments or multiple arguments in function
  208. * @return mixed translated string
  209. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  210. */
  211. function __x($context, $singular, $args = null) {
  212. if (!$singular) {
  213. return;
  214. }
  215. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  216. return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
  217. }
  218. }
  219. if (!function_exists('__xn')) {
  220. /**
  221. * Returns correct plural form of message identified by $singular and $plural for count $count.
  222. * Some languages have more than one form for plural messages dependent on the count.
  223. * The context is a unique identifier for the translations string that makes it unique
  224. * for in the same domain.
  225. *
  226. * @param string $context Context of the text
  227. * @param string $singular Singular text to translate
  228. * @param string $plural Plural text
  229. * @param int $count Count
  230. * @param mixed $args Array with arguments or multiple arguments in function
  231. * @return mixed plural form of translated string
  232. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__xn
  233. */
  234. function __xn($context, $singular, $plural, $count, $args = null) {
  235. if (!$singular) {
  236. return;
  237. }
  238. $arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 2);
  239. return I18n::translator()->translate(
  240. $singular,
  241. ['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
  242. );
  243. }
  244. }
  245. if (!function_exists('__dx')) {
  246. /**
  247. * Allows you to override the current domain for a single message lookup.
  248. * The context is a unique identifier for the translations string that makes it unique
  249. * for in the same domain.
  250. *
  251. * @param string $domain Domain
  252. * @param string $context Context of the text
  253. * @param string $msg String to translate
  254. * @param mixed $args Array with arguments or multiple arguments in function
  255. * @return string translated string
  256. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dx
  257. */
  258. function __dx($domain, $context, $msg, $args = null) {
  259. if (!$msg) {
  260. return;
  261. }
  262. $arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 2);
  263. return I18n::translator($domain)->translate(
  264. $msg,
  265. ['_context' => $context] + $arguments
  266. );
  267. }
  268. }
  269. if (!function_exists('__dxn')) {
  270. /**
  271. * Returns correct plural form of message identified by $singular and $plural for count $count.
  272. * Allows you to override the current domain for a single message lookup.
  273. * The context is a unique identifier for the translations string that makes it unique
  274. * for in the same domain.
  275. *
  276. * @param string $domain Domain
  277. * @param string $context Context of the text
  278. * @param string $singular Singular text to translate
  279. * @param string $plural Plural text
  280. * @param int $count Count
  281. * @param mixed $args Array with arguments or multiple arguments in function
  282. * @return mixed plural form of translated string
  283. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dxn
  284. */
  285. function __dxn($domain, $context, $singular, $plural, $count, $args = null) {
  286. if (!$singular) {
  287. return;
  288. }
  289. $arguments = func_num_args() === 6 ? (array)$args : array_slice(func_get_args(), 2);
  290. return I18n::translator($domain)->translate(
  291. $singular,
  292. ['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
  293. );
  294. }
  295. }