basics.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Collection\Collection;
  16. use Cake\Core\Configure;
  17. use Cake\Error\Debugger;
  18. use Cake\I18n\I18n;
  19. /**
  20. * Basic defines for timing functions.
  21. */
  22. define('SECOND', 1);
  23. define('MINUTE', 60);
  24. define('HOUR', 3600);
  25. define('DAY', 86400);
  26. define('WEEK', 604800);
  27. define('MONTH', 2592000);
  28. define('YEAR', 31536000);
  29. if (!function_exists('debug')) {
  30. /**
  31. * Prints out debug information about given variable.
  32. *
  33. * Only runs if debug level is greater than zero.
  34. *
  35. * @param mixed $var Variable to show debug information for.
  36. * @param bool $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  37. * @param bool $showFrom If set to true, the method prints from where the function was called.
  38. * @return void
  39. * @link http://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
  40. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
  41. */
  42. function debug($var, $showHtml = null, $showFrom = true) {
  43. if (!Configure::read('debug')) {
  44. return;
  45. }
  46. $file = '';
  47. $line = '';
  48. $lineInfo = '';
  49. if ($showFrom) {
  50. $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
  51. $file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
  52. $line = $trace[0]['line'];
  53. }
  54. $html = <<<HTML
  55. <div class="cake-debug-output">
  56. %s
  57. <pre class="cake-debug">
  58. %s
  59. </pre>
  60. </div>
  61. HTML;
  62. $text = <<<TEXT
  63. %s
  64. ########## DEBUG ##########
  65. %s
  66. ###########################
  67. TEXT;
  68. $template = $html;
  69. if (php_sapi_name() === 'cli' || $showHtml === false) {
  70. $template = $text;
  71. if ($showFrom) {
  72. $lineInfo = sprintf('%s (line %s)', $file, $line);
  73. }
  74. }
  75. if ($showHtml === null && $template !== $text) {
  76. $showHtml = true;
  77. }
  78. $var = Debugger::exportVar($var, 25);
  79. if ($showHtml) {
  80. $template = $html;
  81. $var = h($var);
  82. if ($showFrom) {
  83. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  84. }
  85. }
  86. printf($template, $lineInfo, $var);
  87. }
  88. }
  89. if (!function_exists('stackTrace')) {
  90. /**
  91. * Outputs a stack trace based on the supplied options.
  92. *
  93. * ### Options
  94. *
  95. * - `depth` - The number of stack frames to return. Defaults to 999
  96. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  97. * will be displayed.
  98. * - `start` - The stack frame to start generating a trace from. Defaults to 1
  99. *
  100. * @param array $options Format for outputting stack trace
  101. * @return mixed Formatted stack trace
  102. * @see Debugger::trace()
  103. */
  104. function stackTrace(array $options = array()) {
  105. if (!Configure::read('debug')) {
  106. return;
  107. }
  108. $options += array('start' => 0);
  109. $options['start']++;
  110. echo Debugger::trace($options);
  111. }
  112. }
  113. if (!function_exists('__')) {
  114. /**
  115. * Returns a translated string if one is found; Otherwise, the submitted message.
  116. *
  117. * @param string $singular Text to translate
  118. * @param mixed $args Array with arguments or multiple arguments in function
  119. * @return mixed translated string
  120. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  121. */
  122. function __($singular, $args = null) {
  123. if (!$singular) {
  124. return;
  125. }
  126. $arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
  127. return I18n::translator()->translate($singular, $arguments);
  128. }
  129. }
  130. if (!function_exists('__n')) {
  131. /**
  132. * Returns correct plural form of message identified by $singular and $plural for count $count.
  133. * Some languages have more than one form for plural messages dependent on the count.
  134. *
  135. * @param string $singular Singular text to translate
  136. * @param string $plural Plural text
  137. * @param int $count Count
  138. * @param mixed $args Array with arguments or multiple arguments in function
  139. * @return mixed plural form of translated string
  140. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__n
  141. */
  142. function __n($singular, $plural, $count, $args = null) {
  143. if (!$singular) {
  144. return;
  145. }
  146. $arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
  147. return I18n::translator()->translate(
  148. $plural,
  149. ['_count' => $count, '_singular' => $singular] + $arguments
  150. );
  151. }
  152. }
  153. if (!function_exists('__d')) {
  154. /**
  155. * Allows you to override the current domain for a single message lookup.
  156. *
  157. * @param string $domain Domain
  158. * @param string $msg String to translate
  159. * @param mixed $args Array with arguments or multiple arguments in function
  160. * @return string translated string
  161. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__d
  162. */
  163. function __d($domain, $msg, $args = null) {
  164. if (!$msg) {
  165. return;
  166. }
  167. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  168. return I18n::translator($domain)->translate($msg, $arguments);
  169. }
  170. }
  171. if (!function_exists('__dn')) {
  172. /**
  173. * Allows you to override the current domain for a single plural message lookup.
  174. * Returns correct plural form of message identified by $singular and $plural for count $count
  175. * from domain $domain.
  176. *
  177. * @param string $domain Domain
  178. * @param string $singular Singular string to translate
  179. * @param string $plural Plural
  180. * @param int $count Count
  181. * @param mixed $args Array with arguments or multiple arguments in function
  182. * @return string plural form of translated string
  183. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dn
  184. */
  185. function __dn($domain, $singular, $plural, $count, $args = null) {
  186. if (!$singular) {
  187. return;
  188. }
  189. $arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
  190. return I18n::translator($domain)->translate(
  191. $plural,
  192. ['_count' => $count, '_singular' => $singular] + $arguments
  193. );
  194. }
  195. }
  196. if (!function_exists('__x')) {
  197. /**
  198. * Returns a translated string if one is found; Otherwise, the submitted message.
  199. *
  200. * @param string $context Context of the text
  201. * @param string $singular Text to translate
  202. * @param mixed $args Array with arguments or multiple arguments in function
  203. * @return mixed translated string
  204. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
  205. */
  206. function __x($context, $singular, $args = null) {
  207. if (!$singular) {
  208. return;
  209. }
  210. $arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
  211. return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
  212. }
  213. }
  214. if (!function_exists('collection')) {
  215. /**
  216. * Returns a new Cake\Collection\Collection object wrapping the passed argument
  217. *
  218. * @param \Traversable|array $items The items from which the collection will be built
  219. * @return \Cake\Collection\Collection
  220. */
  221. function collection($items) {
  222. return new Collection($items);
  223. }
  224. }