summaryrefslogtreecommitdiff
path: root/src/helpers.php
blob: de140c4e3ce2f8a1381ee31096250c1aad96c73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGeneratorInterface;
use Engelsystem\Renderer\Renderer;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
 * Get the global app instance
 *
 * @param string $id
 * @return mixed|Application
 */
function app($id = null)
{
    if (is_null($id)) {
        return Application::getInstance();
    }

    return Application::getInstance()->get($id);
}

/**
 * @return Authenticator
 */
function auth()
{
    return app('authenticator');
}

/**
 * @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)
{
    /** @var Config $config */
    $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 Request|mixed
 */
function request($key = null, $default = null)
{
    $request = app('request');

    if (is_null($key)) {
        return $request;
    }

    return $request->input($key, $default);
}

/**
 * @param string $content
 * @param int    $status
 * @param array  $headers
 * @return Response
 */
function response($content = '', $status = 200, $headers = [])
{
    /** @var Response $response */
    $response = app('psr7.response');
    $response = $response
        ->withContent($content)
        ->withStatus($status);

    foreach ($headers as $key => $value) {
        $response = $response->withAddedHeader($key, $value);
    }

    return $response;
}

/**
 * @param string $key
 * @param mixed  $default
 * @return SessionInterface|mixed
 */
function session($key = null, $default = null)
{
    /** @var SessionInterface $session */
    $session = app('session');

    if (is_null($key)) {
        return $session;
    }

    return $session->get($key, $default);
}

/**
 * Translate the given message
 *
 * @param string $key
 * @param array  $replace
 * @return string|Translator
 */
function trans($key = null, $replace = [])
{
    /** @var Translator $translator */
    $translator = app('translator');

    if (is_null($key)) {
        return $translator;
    }

    return $translator->translate($key, $replace);
}

/**
 * Translate the given message
 *
 * @param string $key
 * @param array  $replace
 * @return string
 */
function __($key, $replace = [])
{
    /** @var Translator $translator */
    $translator = app('translator');

    return $translator->translate($key, $replace);
}

/**
 * Translate the given message
 *
 * @param string $key
 * @param string $keyPlural
 * @param int    $number
 * @param array  $replace
 * @return string
 */
function _e($key, $keyPlural, $number, $replace = [])
{
    /** @var Translator $translator */
    $translator = app('translator');

    return $translator->translatePlural($key, $keyPlural, $number, $replace);
}

/**
 * @param string $path
 * @param array  $parameters
 * @return UrlGeneratorInterface|string
 */
function url($path = null, $parameters = [])
{
    $urlGenerator = app('http.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 = [])
{
    $renderer = app('renderer');

    if (is_null($template)) {
        return $renderer;
    }

    return $renderer->render($template, $data);
}