1
|
|
|
<?php namespace Anomaly\Streams\Platform; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\AddonCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Addon\Plugin\Plugin; |
5
|
|
|
use Anomaly\Streams\Platform\Asset\Asset; |
6
|
|
|
use Anomaly\Streams\Platform\Entry\Command\GetEntryCriteria; |
7
|
|
|
use Anomaly\Streams\Platform\Image\Command\MakeImagePath; |
8
|
|
|
use Anomaly\Streams\Platform\Image\Command\MakeImageTag; |
9
|
|
|
use Anomaly\Streams\Platform\Image\Command\MakeImageUrl; |
10
|
|
|
use Anomaly\Streams\Platform\Image\Image; |
11
|
|
|
use Anomaly\Streams\Platform\Stream\Command\GetStream; |
12
|
|
|
use Anomaly\Streams\Platform\Stream\Command\GetStreams; |
13
|
|
|
use Anomaly\Streams\Platform\Support\Decorator; |
14
|
|
|
use Anomaly\Streams\Platform\Support\Str; |
15
|
|
|
use Anomaly\Streams\Platform\Support\Template; |
16
|
|
|
use Anomaly\Streams\Platform\Ui\Button\Command\GetButtons; |
17
|
|
|
use Anomaly\Streams\Platform\Ui\Command\GetElapsedTime; |
18
|
|
|
use Anomaly\Streams\Platform\Ui\Command\GetMemoryUsage; |
19
|
|
|
use Anomaly\Streams\Platform\Ui\Command\GetTranslatedString; |
20
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Command\GetFormCriteria; |
21
|
|
|
use Anomaly\Streams\Platform\Ui\Icon\Command\GetIcon; |
22
|
|
|
use Anomaly\Streams\Platform\View\Command\GetConstants; |
23
|
|
|
use Anomaly\Streams\Platform\View\Command\GetLayoutName; |
24
|
|
|
use Anomaly\Streams\Platform\View\Command\GetView; |
25
|
|
|
use Illuminate\Contracts\Auth\Guard; |
26
|
|
|
use Illuminate\Contracts\Config\Repository; |
27
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
28
|
|
|
use Illuminate\Http\Request; |
29
|
|
|
use Illuminate\Routing\Router; |
30
|
|
|
use Illuminate\Session\Store; |
31
|
|
|
use Illuminate\Translation\Translator; |
32
|
|
|
use Jenssegers\Agent\Agent; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class StreamsPlugin |
36
|
|
|
* |
37
|
|
|
* @link http://anomaly.is/streams-platform |
38
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
39
|
|
|
* @author Ryan Thompson <[email protected]> |
40
|
|
|
* @package Anomaly\Streams\Platform |
41
|
|
|
*/ |
42
|
|
|
class StreamsPlugin extends Plugin |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The string utility. |
47
|
|
|
* |
48
|
|
|
* @var Str |
49
|
|
|
*/ |
50
|
|
|
protected $str; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The URL generator. |
54
|
|
|
* |
55
|
|
|
* @var UrlGenerator |
56
|
|
|
*/ |
57
|
|
|
protected $url; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The auth guard. |
61
|
|
|
* |
62
|
|
|
* @var Guard |
63
|
|
|
*/ |
64
|
|
|
protected $auth; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The agent utility. |
68
|
|
|
* |
69
|
|
|
* @var Agent |
70
|
|
|
*/ |
71
|
|
|
protected $agent; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The asset utility. |
75
|
|
|
* |
76
|
|
|
* @var Asset |
77
|
|
|
*/ |
78
|
|
|
protected $asset; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The config repository. |
82
|
|
|
* |
83
|
|
|
* @var Repository |
84
|
|
|
*/ |
85
|
|
|
protected $config; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The image utility. |
89
|
|
|
* |
90
|
|
|
* @var Image |
91
|
|
|
*/ |
92
|
|
|
protected $image; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The router service. |
96
|
|
|
* |
97
|
|
|
* @var Router |
98
|
|
|
*/ |
99
|
|
|
protected $router; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The request object. |
103
|
|
|
* |
104
|
|
|
* @var Request |
105
|
|
|
*/ |
106
|
|
|
protected $request; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The session store. |
110
|
|
|
* |
111
|
|
|
* @var Store |
112
|
|
|
*/ |
113
|
|
|
protected $session; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The template parser. |
117
|
|
|
* |
118
|
|
|
* @var Template |
119
|
|
|
*/ |
120
|
|
|
protected $template; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The translator utility. |
124
|
|
|
* |
125
|
|
|
* @var Translator |
126
|
|
|
*/ |
127
|
|
|
protected $translator; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Create a new AgentPlugin instance. |
131
|
|
|
* |
132
|
|
|
* @param UrlGenerator $url |
133
|
|
|
* @param Str $str |
134
|
|
|
* @param Guard $auth |
135
|
|
|
* @param Agent $agent |
136
|
|
|
* @param Asset $asset |
137
|
|
|
* @param Image $image |
138
|
|
|
* @param Router $router |
139
|
|
|
* @param Repository $config |
140
|
|
|
* @param Request $request |
141
|
|
|
* @param Store $session |
142
|
|
|
* @param Template $template |
143
|
|
|
*/ |
144
|
|
|
public function __construct( |
145
|
|
|
UrlGenerator $url, |
146
|
|
|
Str $str, |
147
|
|
|
Guard $auth, |
148
|
|
|
Agent $agent, |
149
|
|
|
Asset $asset, |
150
|
|
|
Image $image, |
151
|
|
|
Router $router, |
152
|
|
|
Repository $config, |
153
|
|
|
Request $request, |
|
|
|
|
154
|
|
|
Store $session, |
155
|
|
|
Template $template |
156
|
|
|
) { |
157
|
|
|
$this->url = $url; |
158
|
|
|
$this->str = $str; |
159
|
|
|
$this->auth = $auth; |
160
|
|
|
$this->agent = $agent; |
161
|
|
|
$this->asset = $asset; |
162
|
|
|
$this->image = $image; |
163
|
|
|
$this->router = $router; |
164
|
|
|
$this->config = $config; |
165
|
|
|
$this->request = $request; |
166
|
|
|
$this->session = $session; |
167
|
|
|
$this->template = $template; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the plugin functions. |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function getFunctions() |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
new \Twig_SimpleFunction( |
179
|
|
|
'stream', |
180
|
|
|
function ($namespace, $slug = null) { |
181
|
|
|
return (new Decorator())->decorate( |
182
|
|
|
$this->dispatch(new GetStream($namespace, $slug ?: $namespace)) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
), |
186
|
|
|
new \Twig_SimpleFunction( |
187
|
|
|
'streams', |
188
|
|
|
function ($namespace) { |
189
|
|
|
return (new Decorator())->decorate( |
190
|
|
|
$this->dispatch(new GetStreams($namespace)) |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
), |
194
|
|
|
new \Twig_SimpleFunction( |
195
|
|
|
'entry', |
196
|
|
|
function ($namespace, $stream = null) { |
197
|
|
|
return (new Decorator())->decorate( |
198
|
|
|
$this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'first')) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
), |
202
|
|
|
new \Twig_SimpleFunction( |
203
|
|
|
'entries', |
204
|
|
|
function ($namespace, $stream = null) { |
205
|
|
|
return (new Decorator())->decorate( |
206
|
|
|
$this->dispatch(new GetEntryCriteria($namespace, $stream ?: $namespace, 'get')) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
), |
210
|
|
|
new \Twig_SimpleFunction( |
211
|
|
|
'image_path', |
212
|
|
|
function ($image) { |
213
|
|
|
return $this->dispatch(new MakeImagePath($image)); |
214
|
|
|
} |
215
|
|
|
), |
216
|
|
|
new \Twig_SimpleFunction( |
217
|
|
|
'image_url', |
218
|
|
|
function ($image) { |
219
|
|
|
return $this->dispatch(new MakeImageUrl($image)); |
220
|
|
|
} |
221
|
|
|
), |
222
|
|
|
new \Twig_SimpleFunction( |
223
|
|
|
'image', |
224
|
|
|
function ($image) { |
225
|
|
|
return $this->dispatch(new MakeImageTag($image)); |
226
|
|
|
}, |
227
|
|
|
[ |
228
|
|
|
'is_safe' => ['html'] |
229
|
|
|
] |
230
|
|
|
), |
231
|
|
|
new \Twig_SimpleFunction( |
232
|
|
|
'img', |
233
|
|
|
function ($image) { |
234
|
|
|
return $this->dispatch(new MakeImageTag($image)); |
235
|
|
|
}, |
236
|
|
|
[ |
237
|
|
|
'is_safe' => ['html'] |
238
|
|
|
] |
239
|
|
|
), |
240
|
|
|
new \Twig_SimpleFunction( |
241
|
|
|
'form', |
242
|
|
|
function () { |
243
|
|
|
|
244
|
|
|
$arguments = func_get_args(); |
245
|
|
|
|
246
|
|
|
if (count($arguments) >= 2) { |
247
|
|
|
$arguments = [ |
248
|
|
|
'namespace' => array_get(func_get_args(), 0), |
249
|
|
|
'stream' => array_get(func_get_args(), 1), |
250
|
|
|
'entry' => array_get(func_get_args(), 2), |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (count($arguments) == 1) { |
255
|
|
|
$arguments = func_get_arg(0); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->dispatch(new GetFormCriteria($arguments)); |
259
|
|
|
}, |
260
|
|
|
[ |
261
|
|
|
'is_safe' => ['html'] |
262
|
|
|
] |
263
|
|
|
), |
264
|
|
|
new \Twig_SimpleFunction( |
265
|
|
|
'icon', |
266
|
|
|
function ($type, $class = null) { |
267
|
|
|
return (new Decorator())->decorate($this->dispatch(new GetIcon($type, $class))); |
268
|
|
|
}, |
269
|
|
|
[ |
270
|
|
|
'is_safe' => ['html'] |
271
|
|
|
] |
272
|
|
|
), |
273
|
|
|
new \Twig_SimpleFunction( |
274
|
|
|
'view', |
275
|
|
|
function ($view, array $data = []) { |
276
|
|
|
return $this->dispatch(new GetView($view, $data))->render(); |
277
|
|
|
}, |
278
|
|
|
[ |
279
|
|
|
'is_safe' => ['html'] |
280
|
|
|
] |
281
|
|
|
), |
282
|
|
|
new \Twig_SimpleFunction( |
283
|
|
|
'buttons', |
284
|
|
|
function ($buttons) { |
285
|
|
|
return $this->dispatch(new GetButtons($buttons))->render(); |
286
|
|
|
}, |
287
|
|
|
[ |
288
|
|
|
'is_safe' => ['html'] |
289
|
|
|
] |
290
|
|
|
), |
291
|
|
|
new \Twig_SimpleFunction( |
292
|
|
|
'constants', |
293
|
|
|
function () { |
294
|
|
|
return $this->dispatch(new GetConstants())->render(); |
295
|
|
|
}, |
296
|
|
|
[ |
297
|
|
|
'is_safe' => ['html'] |
298
|
|
|
] |
299
|
|
|
), |
300
|
|
|
new \Twig_SimpleFunction( |
301
|
|
|
'env', |
302
|
|
|
function ($key, $default = null) { |
303
|
|
|
return env($key, $default); |
304
|
|
|
} |
305
|
|
|
), |
306
|
|
|
new \Twig_SimpleFunction( |
307
|
|
|
'decorate', |
308
|
|
|
function ($value) { |
309
|
|
|
return (new Decorator())->decorate($value); |
310
|
|
|
} |
311
|
|
|
), |
312
|
|
|
new \Twig_SimpleFunction( |
313
|
|
|
'request_time', |
314
|
|
|
function ($decimal = 2) { |
315
|
|
|
return $this->dispatch(new GetElapsedTime($decimal)); |
316
|
|
|
} |
317
|
|
|
), |
318
|
|
|
new \Twig_SimpleFunction( |
319
|
|
|
'memory_usage', |
320
|
|
|
function ($precision = 1) { |
321
|
|
|
return $this->dispatch(new GetMemoryUsage($precision)); |
322
|
|
|
} |
323
|
|
|
), |
324
|
|
|
new \Twig_SimpleFunction( |
325
|
|
|
'layout', |
326
|
|
|
function ($layout, $default = 'default') { |
327
|
|
|
return $this->dispatch(new GetLayoutName($layout, $default)); |
328
|
|
|
} |
329
|
|
|
), |
330
|
|
|
new \Twig_SimpleFunction( |
331
|
|
|
'request_*', |
332
|
|
View Code Duplication |
function ($name) { |
|
|
|
|
333
|
|
|
|
334
|
|
|
$arguments = array_slice(func_get_args(), 1); |
335
|
|
|
|
336
|
|
|
return call_user_func_array([$this->request, camel_case($name)], $arguments); |
337
|
|
|
} |
338
|
|
|
), |
339
|
|
|
new \Twig_SimpleFunction( |
340
|
|
|
'trans', |
341
|
|
|
function ($key, array $parameters = [], $locale = 'en') { |
342
|
|
|
return $this->dispatch(new GetTranslatedString($key, $parameters, $locale)); |
343
|
|
|
} |
344
|
|
|
), |
345
|
|
|
new \Twig_SimpleFunction( |
346
|
|
|
'str_*', |
347
|
|
View Code Duplication |
function ($name) { |
|
|
|
|
348
|
|
|
|
349
|
|
|
$arguments = array_slice(func_get_args(), 1); |
350
|
|
|
|
351
|
|
|
return call_user_func_array([$this->str, camel_case($name)], $arguments); |
352
|
|
|
} |
353
|
|
|
), |
354
|
|
|
new \Twig_SimpleFunction( |
355
|
|
|
'url_*', |
356
|
|
View Code Duplication |
function ($name) { |
|
|
|
|
357
|
|
|
|
358
|
|
|
$arguments = array_slice(func_get_args(), 1); |
359
|
|
|
|
360
|
|
|
return call_user_func_array([$this->url, camel_case($name)], $arguments); |
361
|
|
|
} |
362
|
|
|
), |
363
|
|
|
new \Twig_SimpleFunction( |
364
|
|
|
'asset_*', |
365
|
|
View Code Duplication |
function ($name) { |
|
|
|
|
366
|
|
|
|
367
|
|
|
$arguments = array_slice(func_get_args(), 1); |
368
|
|
|
|
369
|
|
|
return call_user_func_array([$this->asset, camel_case($name)], $arguments); |
370
|
|
|
}, ['is_safe' => ['html']] |
371
|
|
|
), |
372
|
|
|
new \Twig_SimpleFunction( |
373
|
|
|
'addon', |
374
|
|
|
function ($identifier) { |
375
|
|
|
return app(AddonCollection::class)->get($identifier); |
376
|
|
|
} |
377
|
|
|
), |
378
|
|
|
new \Twig_SimpleFunction( |
379
|
|
|
'addons', |
380
|
|
|
function ($type = null) { |
381
|
|
|
|
382
|
|
|
$addons = app(AddonCollection::class); |
383
|
|
|
|
384
|
|
|
if ($type) { |
385
|
|
|
$addons = $addons->{str_plural($type)}(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $addons; |
389
|
|
|
} |
390
|
|
|
), |
391
|
|
|
new \Twig_SimpleFunction('input_get', [$this->request, 'input']), |
392
|
|
|
new \Twig_SimpleFunction('asset', [$this->url, 'asset'], ['is_safe' => ['html']]), |
393
|
|
|
new \Twig_SimpleFunction('action', [$this->url, 'action'], ['is_safe' => ['html']]), |
394
|
|
|
new \Twig_SimpleFunction('url', [$this, 'url'], ['is_safe' => ['html']]), |
395
|
|
|
new \Twig_SimpleFunction('route', [$this->url, 'route'], ['is_safe' => ['html']]), |
396
|
|
|
new \Twig_SimpleFunction('route_has', [$this->router, 'has'], ['is_safe' => ['html']]), |
397
|
|
|
new \Twig_SimpleFunction('secure_url', [$this->url, 'secure'], ['is_safe' => ['html']]), |
398
|
|
|
new \Twig_SimpleFunction('secure_asset', [$this->url, 'secureAsset'], ['is_safe' => ['html']]), |
399
|
|
|
new \Twig_SimpleFunction('config', [$this->config, 'get']), |
400
|
|
|
new \Twig_SimpleFunction('config_get', [$this->config, 'get']), |
401
|
|
|
new \Twig_SimpleFunction('config_has', [$this->config, 'has']), |
402
|
|
|
new \Twig_SimpleFunction('auth_user', [$this->auth, 'user']), |
403
|
|
|
new \Twig_SimpleFunction('auth_check', [$this->auth, 'check']), |
404
|
|
|
new \Twig_SimpleFunction('auth_guest', [$this->auth, 'guest']), |
405
|
|
|
new \Twig_SimpleFunction('trans_exists', [$this->translator, 'exists']), |
406
|
|
|
new \Twig_SimpleFunction('message_get', [$this->session, 'pull']), |
407
|
|
|
new \Twig_SimpleFunction('message_exists', [$this->session, 'has']), |
408
|
|
|
new \Twig_SimpleFunction('session', [$this->session, 'get']), |
409
|
|
|
new \Twig_SimpleFunction('parse', [$this->template, 'render'], ['is_safe' => ['html']]), |
410
|
|
|
new \Twig_SimpleFunction('csrf_token', [$this->session, 'token'], ['is_safe' => ['html']]), |
411
|
|
|
new \Twig_SimpleFunction('csrf_field', 'csrf_field', ['is_safe' => ['html']]), |
412
|
|
|
new \Twig_SimpleFunction('session_get', [$this->session, 'get']), |
413
|
|
|
new \Twig_SimpleFunction('session_pull', [$this->session, 'pull']), |
414
|
|
|
new \Twig_SimpleFunction('session_has', [$this->session, 'has']), |
415
|
|
|
new \Twig_SimpleFunction('agent_device', [$this->agent, 'device']), |
416
|
|
|
new \Twig_SimpleFunction('agent_browser', [$this->agent, 'browser']), |
417
|
|
|
new \Twig_SimpleFunction('agent_platform', [$this->agent, 'platform']), |
418
|
|
|
new \Twig_SimpleFunction('agent_is_phone', [$this->agent, 'isPhone']), |
419
|
|
|
new \Twig_SimpleFunction('agent_is_robot', [$this->agent, 'isRobot']), |
420
|
|
|
new \Twig_SimpleFunction('agent_is_tablet', [$this->agent, 'isTablet']), |
421
|
|
|
new \Twig_SimpleFunction('agent_is_mobile', [$this->agent, 'isMobile']), |
422
|
|
|
new \Twig_SimpleFunction('agent_is_desktop', [$this->agent, 'isDesktop']) |
423
|
|
|
]; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get the filters. |
428
|
|
|
* |
429
|
|
|
* @return array |
430
|
|
|
*/ |
431
|
|
|
public function getFilters() |
432
|
|
|
{ |
433
|
|
|
return [ |
434
|
|
|
new \Twig_SimpleFilter('camel_case', [$this->str, 'camel']), |
435
|
|
|
new \Twig_SimpleFilter('snake_case', [$this->str, 'snake']), |
436
|
|
|
new \Twig_SimpleFilter('studly_case', [$this->str, 'studly']), |
437
|
|
|
new \Twig_SimpleFilter('humanize', [$this->str, 'humanize']), |
438
|
|
|
new \Twig_SimpleFilter( |
439
|
|
|
'str_*', |
440
|
|
View Code Duplication |
function ($name) { |
|
|
|
|
441
|
|
|
|
442
|
|
|
$arguments = array_slice(func_get_args(), 1); |
443
|
|
|
|
444
|
|
|
return call_user_func_array([$this->str, camel_case($name)], $arguments); |
445
|
|
|
} |
446
|
|
|
), |
447
|
|
|
]; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Return a URL. |
452
|
|
|
* |
453
|
|
|
* @param null $path |
454
|
|
|
* @param array $parameters |
455
|
|
|
* @param null $secure |
456
|
|
|
* @return string |
457
|
|
|
*/ |
458
|
|
|
public function url($path = null, $parameters = [], $secure = null) |
459
|
|
|
{ |
460
|
|
|
return $this->url->to($path, $parameters, $secure); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|