engelsystem/src/helpers.php

112 lines
2.0 KiB
PHP
Raw Normal View History

<?php
// Some useful functions
2017-09-19 14:50:20 +02:00
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator;
2017-08-30 19:57:01 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2017-08-31 17:30:54 +02:00
/**
2017-09-19 14:50:20 +02:00
* Get the global app instance
2017-08-31 17:30:54 +02:00
*
* @param string $id
* @return mixed
*/
function app($id = null)
{
if (is_null($id)) {
2017-09-19 14:50:20 +02:00
return Application::getInstance();
2017-08-31 17:30:54 +02:00
}
2017-09-19 14:50:20 +02:00
return Application::getInstance()->get($id);
2017-08-31 17:30:54 +02:00
}
/**
* Get or set config values
*
* @param string|array $key
* @param mixed $default
* @return mixed|Config
*/
function config($key = null, $default = null)
{
2017-08-31 17:30:54 +02:00
$config = app('config');
if (empty($key)) {
2017-08-31 17:30:54 +02:00
return $config;
}
if (is_array($key)) {
2017-08-31 17:30:54 +02:00
$config->set($key);
return true;
}
2017-08-31 17:30:54 +02:00
return $config->get($key, $default);
}
/**
* @param string $key
* @param mixed $default
* @return Request|mixed
*/
function request($key = null, $default = null)
{
2017-08-31 17:30:54 +02:00
$request = app('request');
if (is_null($key)) {
return $request;
}
return $request->input($key, $default);
}
2017-08-30 19:57:01 +02:00
/**
* @param string $key
* @param mixed $default
* @return SessionInterface|mixed
*/
function session($key = null, $default = null)
{
2017-09-20 01:09:11 +02:00
$session = app('session');
2017-08-30 19:57:01 +02:00
if (is_null($key)) {
return $session;
}
return $session->get($key, $default);
}
/**
* @param string $template
* @param mixed[] $data
* @return Renderer|string
*/
function view($template = null, $data = null)
{
2017-08-31 17:30:54 +02:00
$renderer = app('renderer');
if (is_null($template)) {
return $renderer;
}
return $renderer->render($template, $data);
}
/**
* @param string $path
* @param array $parameters
2017-09-20 01:09:11 +02:00
* @return UrlGenerator|string
*/
2017-09-20 01:09:11 +02:00
function url($path = null, $parameters = [])
{
2017-09-20 01:09:11 +02:00
$urlGenerator = app('routing.urlGenerator');
if (is_null($path)) {
return $urlGenerator;
}
return $urlGenerator->to($path, $parameters);
}