選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

133 行
2.7KB

  1. <?php
  2. use Framework\App;
  3. use Framework\Validation;
  4. use Framework\View;
  5. if (!function_exists('app')) {
  6. function app(string $alias = null): mixed
  7. {
  8. if (is_null($alias)) {
  9. return App::getInstance();
  10. }
  11. return App::getInstance()->resolve($alias);
  12. }
  13. }
  14. if (!function_exists('view')) {
  15. function view(string $template, array $data = []): View\View
  16. {
  17. return app('view')->render($template, $data);
  18. }
  19. }
  20. if (!function_exists('validate')) {
  21. function validate(array $data, array $rules, string $sessionName = 'errors')
  22. {
  23. return app('validator')->validate($data, $rules, $sessionName);
  24. }
  25. }
  26. if (!function_exists('response')) {
  27. function response()
  28. {
  29. return app('response');
  30. }
  31. }
  32. if (!function_exists('redirect')) {
  33. function redirect(string $url)
  34. {
  35. return response()->redirect($url);
  36. }
  37. }
  38. if (!function_exists('csrf')) {
  39. function csrf()
  40. {
  41. $session = session();
  42. if (!$session) {
  43. throw new Exception('Session is not enabled');
  44. }
  45. $session->put('token', $token = bin2hex(random_bytes(32)));
  46. return $token;
  47. }
  48. }
  49. if (!function_exists('secure')) {
  50. function secure()
  51. {
  52. $session = session();
  53. if (!$session) {
  54. throw new Exception('Session is not enabled');
  55. }
  56. if (!isset($_POST['csrf']) || !$session->has('token') || !hash_equals($session->get('token'), $_POST['csrf'])) {
  57. throw new Exception('CSRF token mismatch');
  58. }
  59. }
  60. }
  61. if (!function_exists('dd')) {
  62. function dd(...$params)
  63. {
  64. var_dump(...$params);
  65. die;
  66. }
  67. }
  68. if (!function_exists('basePath')) {
  69. function basePath(string $newBasePath = null): ?string
  70. {
  71. return app('paths.base');
  72. }
  73. }
  74. if (!function_exists('env')) {
  75. function env(string $key, mixed $default = null): mixed
  76. {
  77. if (isset($_SERVER[$key])) {
  78. return $_SERVER[$key];
  79. }
  80. if (isset($_ENV[$key])) {
  81. return $_ENV[$key];
  82. }
  83. $value = getenv($key);
  84. if ($value !== false) {
  85. return $value;
  86. }
  87. return $default;
  88. }
  89. }
  90. if (!function_exists('config')) {
  91. function config(string $key = null, mixed $default = null): mixed
  92. {
  93. if (is_null($key)) {
  94. return app('config');
  95. }
  96. return app('config')->get($key, $default);
  97. }
  98. }
  99. if (!function_exists('session')) {
  100. function session(string $key = null, mixed $default = null): mixed
  101. {
  102. if (is_null($key)) {
  103. return app('session');
  104. }
  105. return app('session')->get($key, $default);
  106. }
  107. }

Powered by TurnKey Linux.