-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoload.php
More file actions
269 lines (245 loc) · 7.9 KB
/
Copy pathautoload.php
File metadata and controls
269 lines (245 loc) · 7.9 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* Mantle HTTP Helpers
*
* Intentionally not Namespaced to allow for root-level access to
* framework methods.
*
* @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound, Squiz.Commenting.FunctionComment
*
* @package Mantle
*/
declare(strict_types=1);
use Mantle\Contracts\Http\Routing\Response_Factory;
use Mantle\Contracts\Http\View\Factory as View_Factory;
use Mantle\Http\View\View;
use Mantle\Support\Mixed_Data;
use Symfony\Component\Routing\Generator\UrlGenerator;
if ( ! function_exists( 'response' ) ) {
/**
* Return a new response for the application.
*
* @param string $content Response content, optional.
* @param int $status Response status code, optional.
* @param array $headers Response headers, optional.
* @return Response_Factory
*/
function response( ...$args ) {
$factory = app( Response_Factory::class );
if ( empty( $args ) ) {
return $factory;
}
return $factory->make( ...$args );
}
}
if ( ! function_exists( 'redirect' ) ) {
/**
* Get an instance of the redirector.
*
* @param string|null $to URL to redirect to, optional.
* @param int $status Status code, optional.
* @param array<string, string> $headers Headers, optional.
* @param bool|null $secure Whether the redirect should be secure, optional.
* @return \Mantle\Http\Routing\Redirector
*/
function redirect( ?string $to = null, int $status = 302, array $headers = [], ?bool $secure = null ) {
if ( is_null( $to ) ) {
return app( 'redirect' );
}
return app( 'redirect' )->to( $to, $status, $headers, $secure );
}
}
if ( ! function_exists( 'request' ) ) {
/**
* Get an instance of the current request or an input item from the request.
*
* @param array<string>|string|null $key Request key.
* @param mixed $default Default value.
* @return \Mantle\Http\Request|string|array<mixed>|null
*/
function request( $key = null, $default = null ) {
if ( is_null( $key ) ) {
return app( 'request' );
}
if ( is_array( $key ) ) {
return app( 'request' )->only( $key );
}
$value = app( 'request' )->__get( $key );
return $value ?? value( $default );
}
}
if ( ! function_exists( 'view' ) ) {
/**
* Return a new view or the view factory.
*
* @param string|null $slug View slug.
* @param array<string, mixed>|string $name View name or variables.
* @param array<string, mixed> $variables Variables for the view, optional.
* @phpstan-return ($slug is null ? View_Factory : View)
*/
function view( ?string $slug = null, array|string|null $name = null, array $variables = [] ): View|View_Factory {
$factory = app( View_Factory::class );
return $slug ? $factory->make( $slug, $name, $variables ) : $factory;
}
}
if ( ! function_exists( 'render_view' ) ) {
/**
* Render a new view.
*
* @deprecated Use view() instead.
*
* @param string $slug View slug.
* @param array|string $name View name, optional. Supports passing variables in if
* $variables is not used.
*/
#[\Deprecated( 'Use view() instead.' )]
function render_view( ...$args ): void {
// @phpstan-ignore echo.nonString
echo view( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! function_exists( 'render_main_template' ) ) {
/**
* Render the contents of the main template passed to the wrapper.
*
* The contents of the '_mantle_contents' variable are assumed to be pre-sanitized.
*/
function render_main_template(): void {
echo mantle_get_var( '_mantle_contents' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! function_exists( 'loop' ) ) {
/**
* Loop over a collection/array of posts.
*
* @param \ArrayAccess|array $data Data to loop over.
* @param string $slug View slug.
* @param array|string $name View name, optional. Supports passing variables in if
* $variables is not used.
*/
function loop( ...$args ): string {
return view()
->loop( ...$args )
->map( fn ( View $item ) => $item->render() )
->implode( '' );
}
}
if ( ! function_exists( 'render_loop' ) ) {
/**
* Render the loop() function.
*
* @param \ArrayAccess|array $data Data to loop over.
* @param string $slug View slug.
* @param array|string $name View name, optional. Supports passing variables in if
* $variables is not used.
*/
function render_loop( ...$args ): void {
echo loop( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! function_exists( 'iterate' ) ) {
/**
* Iterate over an array of arbitrary items, passing the index and item to a
* given template part.
*
* @param \ArrayAccess|array $data Data to loop over.
* @param string $slug View slug.
* @param array|string $name View name, optional. Supports passing variables in if
* $variables is not used.
*/
function iterate( ...$args ): string {
return view()
->iterate( ...$args )
->map( fn ( View $item ) => $item->render() )
->implode( '' );
}
}
if ( ! function_exists( 'render_iterate' ) ) {
/**
* Render over iterate().
*
* @param \ArrayAccess|array $data Data to loop over.
* @param string $slug View slug.
* @param array|string $name View name, optional. Supports passing variables in if
* $variables is not used.
*/
function render_iterate( ...$args ): void {
echo iterate( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! function_exists( 'mantle_get_var' ) ) {
/**
* Retrieve a template variable.
*
* @param string $key Variable to get.
* @param mixed $default Default value if unset.
*/
function mantle_get_var( string $key, mixed $default = null ): mixed {
return app( View_Factory::class )->get_var( $key, $default );
}
}
if ( ! function_exists( 'mantle_get_mixed_var' ) ) {
/**
* Retrieve a template variable as a Mixed_Data object.
*
* @param string $key Variable to get.
* @param mixed $default Default value if unset.
*/
function mantle_get_mixed_var( string $key, mixed $default = null ): Mixed_Data {
return Mixed_Data::of( mantle_get_var( $key, $default ) );
}
}
if ( ! function_exists( 'route' ) ) {
/**
* Generate a URL to a named route.
*
* @param string $name Route name.
* @param array<mixed> $args Route arguments.
* @return string
*/
function route( string $name, array $args = [], bool $relative = false ) {
return app( 'url' )->generate( $name, $args, $relative ? UrlGenerator::ABSOLUTE_PATH : UrlGenerator::ABSOLUTE_URL );
}
}
if ( ! function_exists( 'abort' ) ) {
/**
* Throw an HttpException with the given data.
*
* @param int $code Error code or exception.
* @param string $message Response message
* @param array<string, string> $headers HTTP Headers
*/
function abort( int $code, string $message = '', array $headers = [] ): void {
app()->abort( $code, $message, $headers );
}
}
if ( ! function_exists( 'abort_if' ) ) {
/**
* Throw an HttpException with the given data if the given condition is true.
*
* @param mixed $condition
* @param int $code
* @param string $message Response message
* @param array<string, string> $headers HTTP Headers
*/
function abort_if( mixed $condition, int $code, string $message = '', array $headers = [] ): void {
if ( $condition ) {
abort( $code, $message, $headers );
}
}
}
if ( ! function_exists( 'abort_unless' ) ) {
/**
* Throw an HttpException with the given data unless the given condition is true.
*
* @param mixed $boolean
* @param int $code
* @param string $message Response message
* @param array<string, string> $headers HTTP Headers
*/
function abort_unless( $condition, int $code, string $message = '', array $headers = [] ): void {
if ( ! $condition ) {
abort( $code, $message, $headers );
}
}
}