173 lines
No EOL
3.5 KiB
PHP
173 lines
No EOL
3.5 KiB
PHP
<?php
|
|
|
|
use Runtime\Bootstrap;
|
|
use Runtime\Contracts\Container\Container;
|
|
use Runtime\Factory\BootstrapFactory;
|
|
use Runtime\Http\Input\InputHandler;
|
|
use Runtime\Http\Request;
|
|
use Runtime\Http\Response;
|
|
use Runtime\Http\Url;
|
|
use Runtime\Http\View\Factory as ViewFactory;
|
|
use Runtime\Router\Route as Router;
|
|
|
|
if(!function_exists('app'))
|
|
{
|
|
/**
|
|
* @param null $abstract
|
|
* @param array $arguments
|
|
* @return BootstrapFactory|Container|mixed
|
|
*/
|
|
function app($abstract = null, $arguments = [])
|
|
{
|
|
if (is_null($abstract)) {
|
|
return Bootstrap::getInstance();
|
|
}
|
|
|
|
return Bootstrap::getInstance()->make($abstract, $arguments);
|
|
}
|
|
}
|
|
|
|
if(!function_exists('csrf_token'))
|
|
{
|
|
/**
|
|
* @return string|null
|
|
*/
|
|
function csrf_token(): ?string
|
|
{
|
|
$baseVerifier = Router::router()->getCsrfVerifier();
|
|
if ($baseVerifier !== null) {
|
|
return $baseVerifier->getTokenProvider()->getToken();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if(!function_exists('dump'))
|
|
{
|
|
function dump(...$params)
|
|
{
|
|
echo '<pre>';
|
|
foreach ($params as $param) {
|
|
var_dump($param);
|
|
}
|
|
echo '</pre>';
|
|
}
|
|
}
|
|
|
|
if(!function_exists('endsWith'))
|
|
{
|
|
/**
|
|
* @param $haystack
|
|
* @param $needle
|
|
* @return bool
|
|
*/
|
|
function endsWith($haystack, $needle)
|
|
{
|
|
$length = strlen($needle);
|
|
if ($length == 0) {
|
|
return true;
|
|
}
|
|
|
|
return (substr($haystack, -$length) === $needle);
|
|
}
|
|
}
|
|
|
|
if(!function_exists('input'))
|
|
{
|
|
/**
|
|
* @param string|null $index Parameter index name
|
|
* @param string|null $defaultValue Default return value
|
|
* @param array ...$methods Default methods
|
|
* @return InputHandler|array|string|null
|
|
*/
|
|
function input($index = null, $defaultValue = null, ...$methods)
|
|
{
|
|
if ($index !== null) {
|
|
return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
|
|
}
|
|
|
|
return request()->getInputHandler();
|
|
}
|
|
}
|
|
|
|
if(!function_exists('redirect'))
|
|
{
|
|
/**
|
|
* @param string $url
|
|
* @param int|null $code
|
|
*/
|
|
function redirect(string $url, ?int $code = null): void
|
|
{
|
|
if ($code !== null) {
|
|
response()->httpCode($code);
|
|
}
|
|
|
|
response()->redirect($url);
|
|
}
|
|
}
|
|
|
|
if(!function_exists('response'))
|
|
{
|
|
/**
|
|
* @return Response
|
|
*/
|
|
function response(): Response
|
|
{
|
|
return Router::response();
|
|
}
|
|
}
|
|
|
|
if(!function_exists('request'))
|
|
{
|
|
/**
|
|
* @return Request
|
|
*/
|
|
function request(): Request
|
|
{
|
|
return Router::request();
|
|
}
|
|
}
|
|
|
|
if(!function_exists('route'))
|
|
{
|
|
/**
|
|
* @param string|null $name
|
|
* @param string|array|null $parameters
|
|
* @param array|null $getParams
|
|
* @return Url
|
|
*/
|
|
function route(?string $name = null, $parameters = null, ?array $getParams = null): Url
|
|
{
|
|
return Router::getUrl($name, $parameters, $getParams);
|
|
}
|
|
}
|
|
|
|
if(!function_exists('startsWith'))
|
|
{
|
|
/**
|
|
* @param $haystack
|
|
* @param $needle
|
|
* @return bool
|
|
*/
|
|
function startsWith($haystack, $needle)
|
|
{
|
|
$length = strlen($needle);
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
}
|
|
}
|
|
|
|
if(!function_exists('view'))
|
|
{
|
|
/**
|
|
* @param $name
|
|
* @param array $arguments
|
|
* @return mixed
|
|
*/
|
|
function view($name, $arguments = [])
|
|
{
|
|
$factory = app(ViewFactory::class);
|
|
|
|
return $factory->make($name, $arguments);
|
|
}
|
|
} |