diff options
author | msquare <msquare@notrademark.de> | 2017-11-28 15:43:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 15:43:51 +0100 |
commit | 599f2fd264bfc7b1b6826fe206442806e317340f (patch) | |
tree | 50cf84d7d07d11bd65b45c2c17f37632f6cd8eff /src/helpers.php | |
parent | a5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff) | |
parent | eda7f7788ea8012bd8be46405c56a666c11f3fa5 (diff) |
Merge pull request #365 from engelsystem/feature-igel-rewrite
Feature igel rewrite
Diffstat (limited to 'src/helpers.php')
-rw-r--r-- | src/helpers.php | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 00000000..5a48498a --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,144 @@ +<?php +// Some useful functions + +use Engelsystem\Application; +use Engelsystem\Config\Config; +use Engelsystem\Http\Request; +use Engelsystem\Renderer\Renderer; +use Engelsystem\Routing\UrlGenerator; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +/** + * Get the global app instance + * + * @param string $id + * @return mixed + */ +function app($id = null) +{ + if (is_null($id)) { + return Application::getInstance(); + } + + return Application::getInstance()->get($id); +} + +/** + * @param string $path + * @return string + */ +function base_path($path = '') +{ + return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + +/** + * Get or set config values + * + * @param string|array $key + * @param mixed $default + * @return mixed|Config + */ +function config($key = null, $default = null) +{ + $config = app('config'); + + if (empty($key)) { + return $config; + } + + if (is_array($key)) { + $config->set($key); + return true; + } + + return $config->get($key, $default); +} + +/** + * @param string $path + * @return string + */ +function config_path($path = '') +{ + return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + +/** + * @param string $key + * @param mixed $default + * @return mixed + */ +function env($key, $default = null) +{ + $value = getenv($key); + if ($value === false) { + return $default; + } + + return $value; +} + +/** + * @param string $key + * @param mixed $default + * @return Request|mixed + */ +function request($key = null, $default = null) +{ + $request = app('request'); + + if (is_null($key)) { + return $request; + } + + return $request->input($key, $default); +} + +/** + * @param string $key + * @param mixed $default + * @return SessionInterface|mixed + */ +function session($key = null, $default = null) +{ + $session = app('session'); + + if (is_null($key)) { + return $session; + } + + return $session->get($key, $default); +} + +/** + * @param string $path + * @param array $parameters + * @return UrlGenerator|string + */ +function url($path = null, $parameters = []) +{ + $urlGenerator = app('routing.urlGenerator'); + + if (is_null($path)) { + return $urlGenerator; + } + + return $urlGenerator->to($path, $parameters); +} + +/** + * @param string $template + * @param mixed[] $data + * @return Renderer|string + */ +function view($template = null, $data = null) +{ + $renderer = app('renderer'); + + if (is_null($template)) { + return $renderer; + } + + return $renderer->render($template, $data); +} |