PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Core/functions.php

https://github.com/cakephp/cakephp
PHP | 336 lines | 232 code | 29 blank | 75 comment | 33 complexity | 3a779bbb3573d9feaccc81c45c1f5ef2 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. use Cake\Core\Configure;
  17. if (!defined('DS')) {
  18. /**
  19. * Defines DS as short form of DIRECTORY_SEPARATOR.
  20. */
  21. define('DS', DIRECTORY_SEPARATOR);
  22. }
  23. if (!function_exists('h')) {
  24. /**
  25. * Convenience method for htmlspecialchars.
  26. *
  27. * @param mixed $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
  28. * Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
  29. * implement a `__toString` method. Otherwise the class name will be used.
  30. * Other scalar types will be returned unchanged.
  31. * @param bool $double Encode existing html entities.
  32. * @param string|null $charset Character set to use when escaping.
  33. * Defaults to config value in `mb_internal_encoding()` or 'UTF-8'.
  34. * @return mixed Wrapped text.
  35. * @link https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#h
  36. */
  37. function h($text, bool $double = true, ?string $charset = null)
  38. {
  39. if (is_string($text)) {
  40. //optimize for strings
  41. } elseif (is_array($text)) {
  42. $texts = [];
  43. foreach ($text as $k => $t) {
  44. $texts[$k] = h($t, $double, $charset);
  45. }
  46. return $texts;
  47. } elseif (is_object($text)) {
  48. if (method_exists($text, '__toString')) {
  49. $text = $text->__toString();
  50. } else {
  51. $text = '(object)' . get_class($text);
  52. }
  53. } elseif ($text === null || is_scalar($text)) {
  54. return $text;
  55. }
  56. static $defaultCharset = false;
  57. if ($defaultCharset === false) {
  58. $defaultCharset = mb_internal_encoding() ?: 'UTF-8';
  59. }
  60. return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, $charset ?: $defaultCharset, $double);
  61. }
  62. }
  63. if (!function_exists('pluginSplit')) {
  64. /**
  65. * Splits a dot syntax plugin name into its plugin and class name.
  66. * If $name does not have a dot, then index 0 will be null.
  67. *
  68. * Commonly used like
  69. * ```
  70. * list($plugin, $name) = pluginSplit($name);
  71. * ```
  72. *
  73. * @param string $name The name you want to plugin split.
  74. * @param bool $dotAppend Set to true if you want the plugin to have a '.' appended to it.
  75. * @param string|null $plugin Optional default plugin to use if no plugin is found. Defaults to null.
  76. * @return array Array with 2 indexes. 0 => plugin name, 1 => class name.
  77. * @link https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#pluginSplit
  78. * @psalm-return array{string|null, string}
  79. */
  80. function pluginSplit(string $name, bool $dotAppend = false, ?string $plugin = null): array
  81. {
  82. if (strpos($name, '.') !== false) {
  83. $parts = explode('.', $name, 2);
  84. if ($dotAppend) {
  85. $parts[0] .= '.';
  86. }
  87. /** @psalm-var array{string, string}*/
  88. return $parts;
  89. }
  90. return [$plugin, $name];
  91. }
  92. }
  93. if (!function_exists('namespaceSplit')) {
  94. /**
  95. * Split the namespace from the classname.
  96. *
  97. * Commonly used like `list($namespace, $className) = namespaceSplit($class);`.
  98. *
  99. * @param string $class The full class name, ie `Cake\Core\App`.
  100. * @return array<string> Array with 2 indexes. 0 => namespace, 1 => classname.
  101. */
  102. function namespaceSplit(string $class): array
  103. {
  104. $pos = strrpos($class, '\\');
  105. if ($pos === false) {
  106. return ['', $class];
  107. }
  108. return [substr($class, 0, $pos), substr($class, $pos + 1)];
  109. }
  110. }
  111. if (!function_exists('pr')) {
  112. /**
  113. * print_r() convenience function.
  114. *
  115. * In terminals this will act similar to using print_r() directly, when not run on CLI
  116. * print_r() will also wrap `<pre>` tags around the output of given variable. Similar to debug().
  117. *
  118. * This function returns the same variable that was passed.
  119. *
  120. * @param mixed $var Variable to print out.
  121. * @return mixed the same $var that was passed to this function
  122. * @link https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#pr
  123. * @see debug()
  124. */
  125. function pr($var)
  126. {
  127. if (!Configure::read('debug')) {
  128. return $var;
  129. }
  130. $template = PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ? '<pre class="pr">%s</pre>' : "\n%s\n\n";
  131. printf($template, trim(print_r($var, true)));
  132. return $var;
  133. }
  134. }
  135. if (!function_exists('pj')) {
  136. /**
  137. * JSON pretty print convenience function.
  138. *
  139. * In terminals this will act similar to using json_encode() with JSON_PRETTY_PRINT directly, when not run on CLI
  140. * will also wrap `<pre>` tags around the output of given variable. Similar to pr().
  141. *
  142. * This function returns the same variable that was passed.
  143. *
  144. * @param mixed $var Variable to print out.
  145. * @return mixed the same $var that was passed to this function
  146. * @see pr()
  147. * @link https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#pj
  148. */
  149. function pj($var)
  150. {
  151. if (!Configure::read('debug')) {
  152. return $var;
  153. }
  154. $template = PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ? '<pre class="pj">%s</pre>' : "\n%s\n\n";
  155. printf($template, trim(json_encode($var, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)));
  156. return $var;
  157. }
  158. }
  159. if (!function_exists('env')) {
  160. /**
  161. * Gets an environment variable from available sources, and provides emulation
  162. * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
  163. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  164. * environment information.
  165. *
  166. * @param string $key Environment variable name.
  167. * @param string|bool|null $default Specify a default value in case the environment variable is not defined.
  168. * @return string|bool|null Environment variable setting.
  169. * @link https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#env
  170. */
  171. function env(string $key, $default = null)
  172. {
  173. if ($key === 'HTTPS') {
  174. if (isset($_SERVER['HTTPS'])) {
  175. return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
  176. }
  177. return strpos((string)env('SCRIPT_URI'), 'https://') === 0;
  178. }
  179. if ($key === 'SCRIPT_NAME' && env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  180. $key = 'SCRIPT_URL';
  181. }
  182. $val = $_SERVER[$key] ?? $_ENV[$key] ?? null;
  183. if ($val == null && getenv($key) !== false) {
  184. $val = getenv($key);
  185. }
  186. if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
  187. $addr = env('HTTP_PC_REMOTE_ADDR');
  188. if ($addr !== null) {
  189. $val = $addr;
  190. }
  191. }
  192. if ($val !== null) {
  193. return $val;
  194. }
  195. switch ($key) {
  196. case 'DOCUMENT_ROOT':
  197. $name = (string)env('SCRIPT_NAME');
  198. $filename = (string)env('SCRIPT_FILENAME');
  199. $offset = 0;
  200. if (!strpos($name, '.php')) {
  201. $offset = 4;
  202. }
  203. return substr($filename, 0, -(strlen($name) + $offset));
  204. case 'PHP_SELF':
  205. return str_replace((string)env('DOCUMENT_ROOT'), '', (string)env('SCRIPT_FILENAME'));
  206. case 'CGI_MODE':
  207. return PHP_SAPI === 'cgi';
  208. }
  209. return $default;
  210. }
  211. }
  212. if (!function_exists('triggerWarning')) {
  213. /**
  214. * Triggers an E_USER_WARNING.
  215. *
  216. * @param string $message The warning message.
  217. * @return void
  218. */
  219. function triggerWarning(string $message): void
  220. {
  221. $trace = debug_backtrace();
  222. if (isset($trace[1])) {
  223. $frame = $trace[1];
  224. $frame += ['file' => '[internal]', 'line' => '??'];
  225. $message = sprintf(
  226. '%s - %s, line: %s',
  227. $message,
  228. $frame['file'],
  229. $frame['line']
  230. );
  231. }
  232. trigger_error($message, E_USER_WARNING);
  233. }
  234. }
  235. if (!function_exists('deprecationWarning')) {
  236. /**
  237. * Helper method for outputting deprecation warnings
  238. *
  239. * @param string $message The message to output as a deprecation warning.
  240. * @param int $stackFrame The stack frame to include in the error. Defaults to 1
  241. * as that should point to application/plugin code.
  242. * @return void
  243. */
  244. function deprecationWarning(string $message, int $stackFrame = 1): void
  245. {
  246. if (!(error_reporting() & E_USER_DEPRECATED)) {
  247. return;
  248. }
  249. $trace = debug_backtrace();
  250. if (isset($trace[$stackFrame])) {
  251. $frame = $trace[$stackFrame];
  252. $frame += ['file' => '[internal]', 'line' => '??'];
  253. $relative = str_replace(DIRECTORY_SEPARATOR, '/', substr($frame['file'], strlen(ROOT) + 1));
  254. $patterns = (array)Configure::read('Error.ignoredDeprecationPaths');
  255. foreach ($patterns as $pattern) {
  256. $pattern = str_replace(DIRECTORY_SEPARATOR, '/', $pattern);
  257. if (fnmatch($pattern, $relative)) {
  258. return;
  259. }
  260. }
  261. $message = sprintf(
  262. "%s\n%s, line: %s\n" .
  263. 'You can disable all deprecation warnings by setting `Error.errorLevel` to ' .
  264. '`E_ALL & ~E_USER_DEPRECATED`. Adding `%s` to `Error.ignoredDeprecationPaths` ' .
  265. 'in your `config/app.php` config will mute deprecations from that file only.',
  266. $message,
  267. $frame['file'],
  268. $frame['line'],
  269. $relative
  270. );
  271. }
  272. static $errors = [];
  273. $checksum = md5($message);
  274. $duplicate = (bool)Configure::read('Error.allowDuplicateDeprecations', false);
  275. if (isset($errors[$checksum]) && !$duplicate) {
  276. return;
  277. }
  278. if (!$duplicate) {
  279. $errors[$checksum] = true;
  280. }
  281. trigger_error($message, E_USER_DEPRECATED);
  282. }
  283. }
  284. if (!function_exists('getTypeName')) {
  285. /**
  286. * Returns the objects class or var type of it's not an object
  287. *
  288. * @param mixed $var Variable to check
  289. * @return string Returns the class name or variable type
  290. */
  291. function getTypeName($var): string
  292. {
  293. return is_object($var) ? get_class($var) : gettype($var);
  294. }
  295. }