|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
use Spiral\Core\Core; |
|
10
|
|
|
use Spiral\Core\DirectoriesInterface; |
|
11
|
|
|
use Spiral\Core\EnvironmentInterface; |
|
12
|
|
|
use Spiral\Debug\Dumper; |
|
13
|
|
|
use Spiral\Http\Routing\RouterInterface; |
|
14
|
|
|
use Spiral\Translator\Exceptions\TranslatorException; |
|
15
|
|
|
use Spiral\Translator\TranslatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
if (!function_exists('spiral')) { |
|
18
|
|
|
/** |
|
19
|
|
|
* Shortcut to shared container get method. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $alias Class name or alias. |
|
22
|
|
|
* |
|
23
|
|
|
* @return object|null |
|
24
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
|
25
|
|
|
*/ |
|
26
|
|
|
function spiral(string $alias) |
|
27
|
|
|
{ |
|
28
|
|
|
return Core::sharedContainer()->get($alias); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!function_exists('bind')) { |
|
33
|
|
|
/** |
|
34
|
|
|
* Shortcut to container Autowire definition. |
|
35
|
|
|
* |
|
36
|
|
|
* Example: |
|
37
|
|
|
* 'name' => bind(SomeClass::name, [...]) |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $alias Class name or alias. |
|
40
|
|
|
* @param array $parameters |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Spiral\Core\Container\Autowire |
|
43
|
|
|
*/ |
|
44
|
|
|
function bind(string $alias, array $parameters = []) |
|
45
|
|
|
{ |
|
46
|
|
|
return new \Spiral\Core\Container\Autowire($alias, $parameters); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!function_exists('directory')) { |
|
51
|
|
|
/** |
|
52
|
|
|
* Get directory alias value. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $alias Directory alias, ie. "framework". |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
function directory(string $alias): string |
|
59
|
|
|
{ |
|
60
|
|
|
return spiral(DirectoriesInterface::class)->directory($alias); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!function_exists('env')) { |
|
65
|
|
|
/** |
|
66
|
|
|
* Gets the value of an environment variable. Supports boolean, empty and null. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param mixed $default |
|
70
|
|
|
* |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
function env(string $key, $default = null) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
return spiral(EnvironmentInterface::class)->get($key, $default); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (!function_exists('e')) { |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Short alias for htmlentities(). This function is identical to htmlspecialchars() in all ways, |
|
83
|
|
|
* except with htmlentities(), all characters which have HTML character entity equivalents are |
|
84
|
|
|
* translated into these entities. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $string |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
function e(string $string): string |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
return htmlentities($string, ENT_QUOTES, 'UTF-8', false); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!function_exists('dump')) { |
|
97
|
|
|
/** |
|
98
|
|
|
* Dump value. |
|
99
|
|
|
* |
|
100
|
|
|
* @param mixed $value Value to be dumped. |
|
101
|
|
|
* @param int $output |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
function dump($value, $output = Dumper::OUTPUT_ECHO): string |
|
106
|
|
|
{ |
|
107
|
|
|
return spiral(Dumper::class)->dump($value, $output); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (!function_exists('interpolate')) { |
|
112
|
|
|
/** |
|
113
|
|
|
* Format string using previously named arguments from values array. Arguments that are not found |
|
114
|
|
|
* will be skipped without any notification. Extra arguments will be skipped as well. |
|
115
|
|
|
* |
|
116
|
|
|
* Example: |
|
117
|
|
|
* Hello [:name]! Good [:time]! |
|
118
|
|
|
* + array('name'=>'Member','time'=>'day') |
|
119
|
|
|
* |
|
120
|
|
|
* Output: |
|
121
|
|
|
* Hello Member! Good Day! |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $format Formatted string. |
|
124
|
|
|
* @param array $values Arguments (key=>value). Will skip n |
|
125
|
|
|
* @param string $prefix Value prefix, "{" by default. |
|
126
|
|
|
* @param string $postfix Value postfix "}" by default. |
|
127
|
|
|
* |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
function interpolate( |
|
|
|
|
|
|
131
|
|
|
string $format, |
|
132
|
|
|
array $values, |
|
133
|
|
|
string $prefix = '{', |
|
134
|
|
|
string $postfix = '}' |
|
135
|
|
|
): string { |
|
136
|
|
|
return \Spiral\interpolate($format, $values, $prefix, $postfix); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (!function_exists('uri')) { |
|
141
|
|
|
/** |
|
142
|
|
|
* Generate valid route URL using route name and set of parameters. Should support controller |
|
143
|
|
|
* and action name separated by ":" - in this case router should find appropriate route and |
|
144
|
|
|
* create url using it. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $route Route name. |
|
147
|
|
|
* @param array|\Traversable $parameters |
|
148
|
|
|
* |
|
149
|
|
|
* @return UriInterface |
|
150
|
|
|
* @throws \Spiral\Http\Exceptions\RouterException |
|
151
|
|
|
* @throws \Spiral\Http\Exceptions\RouteException |
|
152
|
|
|
* @throws \Spiral\Http\Exceptions\UndefinedRouteException |
|
153
|
|
|
*/ |
|
154
|
|
|
function uri(string $route, $parameters = []): UriInterface |
|
155
|
|
|
{ |
|
156
|
|
|
return spiral(RouterInterface::class)->uri($route, $parameters); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (!function_exists('l')) { |
|
161
|
|
|
/** |
|
162
|
|
|
* Translate message using default or specific bundle name. |
|
163
|
|
|
* |
|
164
|
|
|
* Examples: |
|
165
|
|
|
* l('Some Message'); |
|
166
|
|
|
* l('Hello {name}!', ['name' => $name]); |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $string |
|
169
|
|
|
* @param array $options |
|
170
|
|
|
* @param string $domain |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
* @throws TranslatorException |
|
174
|
|
|
*/ |
|
175
|
|
|
function l(string $string, array $options = [], string $domain = null): string |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
return spiral(TranslatorInterface::class)->trans($string, $options, $domain); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (!function_exists('p')) { |
|
182
|
|
|
/** |
|
183
|
|
|
* Pluralize string using language pluralization options and specified numeric value. |
|
184
|
|
|
* |
|
185
|
|
|
* Examples: |
|
186
|
|
|
* p("{n} user|{n} users", $users); |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $string Can include {n} as placeholder. |
|
189
|
|
|
* @param int $number |
|
190
|
|
|
* @param array $options |
|
191
|
|
|
* @param string $domain |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
* @throws TranslatorException |
|
195
|
|
|
*/ |
|
196
|
|
|
function p( |
|
|
|
|
|
|
197
|
|
|
string $string, |
|
198
|
|
|
int $number, |
|
199
|
|
|
array $options = [], |
|
200
|
|
|
string $domain = null |
|
201
|
|
|
): string { |
|
202
|
|
|
return spiral(TranslatorInterface::class)->transChoice($string, $number, $options, $domain); |
|
203
|
|
|
} |
|
204
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.