1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Patron package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Patron; |
13
|
|
|
|
14
|
|
|
use Brickrouge\Pager; |
15
|
|
|
|
16
|
|
|
use ICanBoogie\Core; |
17
|
|
|
use ICanBoogie\Render\TemplateName; |
18
|
|
|
|
19
|
|
|
class Hooks |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Renders a page element. |
23
|
|
|
* |
24
|
|
|
* <pre> |
25
|
|
|
* <p:pager |
26
|
|
|
* count = int |
27
|
|
|
* page = int |
28
|
|
|
* limit = int |
29
|
|
|
* with = string |
30
|
|
|
* range = expression |
31
|
|
|
* noarrows = boolean> |
32
|
|
|
* <!-- Content: p:with-param*, template? --> |
33
|
|
|
* </p:pager> |
34
|
|
|
* </pre> |
35
|
|
|
* |
36
|
|
|
* @param array $args |
37
|
|
|
* @param Engine $patron |
38
|
|
|
* @param mixed $template |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
static public function markup_pager(array $args, Engine $patron, $template) |
43
|
|
|
{ |
44
|
|
|
$count = null; |
45
|
|
|
$limit = null; |
46
|
|
|
$page = null; |
47
|
|
|
$range = null; |
48
|
|
|
$noarrows = null; |
49
|
|
|
$with = null; |
50
|
|
|
|
51
|
|
|
extract($args); |
52
|
|
|
|
53
|
|
|
if (!$range) |
54
|
|
|
{ |
55
|
|
|
if (isset($patron->context['range'])) |
56
|
|
|
{ |
57
|
|
|
$range = $patron->context['range']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($range) |
62
|
|
|
{ |
63
|
|
|
$count = $range['count']; |
64
|
|
|
$limit = $range['limit']; |
65
|
|
|
$page = isset($range['page']) ? $range['page'] : 0; |
66
|
|
|
|
67
|
|
|
if (isset($range['with'])) |
68
|
|
|
{ |
69
|
|
|
$with = $range['with']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$pager = new Pager('div', [ |
74
|
|
|
|
75
|
|
|
Pager::T_COUNT => $count, |
76
|
|
|
Pager::T_LIMIT => $limit, |
77
|
|
|
Pager::T_POSITION => $page, |
78
|
|
|
Pager::T_NO_ARROWS => $noarrows, |
79
|
|
|
Pager::T_WITH => $with |
80
|
|
|
|
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
return $template ? $patron($template, $pager) : (string) $pager; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Adds a template. |
88
|
|
|
* |
89
|
|
|
* <pre> |
90
|
|
|
* <p:template |
91
|
|
|
* name = qname> |
92
|
|
|
* <!-- Content: p:with-param*, template --> |
93
|
|
|
* </p:template> |
94
|
|
|
* </pre> |
95
|
|
|
* |
96
|
|
|
* The `name` attribute defines the name of the template. The content of the markup defines |
97
|
|
|
* the template. |
98
|
|
|
* |
99
|
|
|
* @param array $args |
100
|
|
|
* @param Engine $patron |
101
|
|
|
* @param mixed $template |
102
|
|
|
*/ |
103
|
|
|
static public function markup_template(array $args, Engine $patron, $template) |
104
|
|
|
{ |
105
|
|
|
$patron->addTemplate(TemplateName::from($args['name'])->as_partial, $template); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Calls a template. |
110
|
|
|
* |
111
|
|
|
* <pre> |
112
|
|
|
* <p:call-template |
113
|
|
|
* name = qname> |
114
|
|
|
* <!-- Content: p:with-param* --> |
115
|
|
|
* </p:call-template> |
116
|
|
|
* </pre> |
117
|
|
|
* |
118
|
|
|
* @param array $args |
119
|
|
|
* @param Engine $patron |
120
|
|
|
* @param mixed $template |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
static public function markup_call_template(array $args, Engine $patron, $template) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
return $patron->callTemplate(TemplateName::from($args['name'])->as_partial, $args); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Applies a template to each entries of the provided array. |
131
|
|
|
* |
132
|
|
|
* <pre> |
133
|
|
|
* <p:foreach |
134
|
|
|
* in = expression | this |
135
|
|
|
* as = qname | this> |
136
|
|
|
* <!-- Content: p:with-param*, template --> |
137
|
|
|
* </p:foreach> |
138
|
|
|
* </pre> |
139
|
|
|
* |
140
|
|
|
* At each turn the following variables are updated in `self`: |
141
|
|
|
* |
142
|
|
|
* - `count`: The number of entries. |
143
|
|
|
* - `position`: The position of the current entry. |
144
|
|
|
* - `left`: The number of entries left. |
145
|
|
|
* - `even`: "even" if the position is even, an empty string otherwise. |
146
|
|
|
* - `key`: The key of the entry. |
147
|
|
|
* |
148
|
|
|
* @param array $args |
149
|
|
|
* @param Engine $patron |
150
|
|
|
* @param mixed $template |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
static public function markup_foreach(array $args, Engine $patron, $template) |
155
|
|
|
{ |
156
|
|
|
# |
157
|
|
|
# get entries array from context |
158
|
|
|
# |
159
|
|
|
|
160
|
|
|
$entries = $args['in']; |
161
|
|
|
|
162
|
|
|
if (!$entries) |
163
|
|
|
{ |
164
|
|
|
return null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!is_array($entries) && !is_object($entries)) |
168
|
|
|
{ |
169
|
|
|
$patron->error |
170
|
|
|
( |
171
|
|
|
'Invalid source for %param. Source must either be an array or a traversable object. Given: !entries', array |
172
|
|
|
( |
173
|
|
|
'%param' => 'in', '!entries' => $entries |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
# |
181
|
|
|
# create body from iterations |
182
|
|
|
# |
183
|
|
|
|
184
|
|
|
$count = count($entries); |
185
|
|
|
$position = 0; |
186
|
|
|
$left = $count; |
187
|
|
|
$even = 'even'; |
188
|
|
|
$key = null; |
189
|
|
|
|
190
|
|
|
$context = [ |
191
|
|
|
|
192
|
|
|
'count' => &$count, |
193
|
|
|
'position' => &$position, |
194
|
|
|
'left' => &$left, |
195
|
|
|
'even' => &$even, |
196
|
|
|
'key' => &$key |
197
|
|
|
|
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
$as = $args['as']; |
201
|
|
|
|
202
|
|
|
$patron->context['self'] = array_merge($patron->context['self'], $context); |
203
|
|
|
|
204
|
|
|
$rc = ''; |
205
|
|
|
|
206
|
|
|
foreach ($entries as $key => $entry) |
207
|
|
|
{ |
208
|
|
|
$position++; |
209
|
|
|
$left--; |
210
|
|
|
$even = ($position % 2) ? '' : 'even'; |
|
|
|
|
211
|
|
|
|
212
|
|
|
if ($as) |
213
|
|
|
{ |
214
|
|
|
$patron->context[$as] = $entry; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$rc .= $patron($template, $entry); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $rc; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Provides a simple if-then conditionality. |
225
|
|
|
* |
226
|
|
|
* <pre> |
227
|
|
|
* <p:if |
228
|
|
|
* test = expression |
229
|
|
|
* select = expression |
230
|
|
|
* equals = value> |
231
|
|
|
* <!-- Content: p:with-param*, template --> |
232
|
|
|
* </p:if> |
233
|
|
|
* </pre> |
234
|
|
|
* |
235
|
|
|
* Either `test` or `select` and an operator (e.g. `equals`) should be defined. |
236
|
|
|
* |
237
|
|
|
* Example: |
238
|
|
|
* |
239
|
|
|
* <pre> |
240
|
|
|
* <p:if test="@has_title">This article has a title</p:if> |
241
|
|
|
* <p:if test="@has_title.not()">This article has no title</p:if> |
242
|
|
|
* <p:if select="@comments_count" equals="10">This article has 10 comments</p:if> |
243
|
|
|
* </pre> |
244
|
|
|
* |
245
|
|
|
* @param array $args |
246
|
|
|
* @param Engine $patron |
247
|
|
|
* @param mixed $template |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
* |
251
|
|
|
* @throws \Exception when both `test` and `select` are defined. |
252
|
|
|
*/ |
253
|
|
|
static public function markup_if(array $args, Engine $patron, $template) |
254
|
|
|
{ |
255
|
|
|
if (isset($args['test']) && isset($args['select'])) |
256
|
|
|
{ |
257
|
|
|
throw new \Exception("Ambiguous test. Both <q>test</q> and <q>select</q> are defined."); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if ($args['equals'] !== null) |
261
|
|
|
{ |
262
|
|
|
$true = $args['select'] == $args['equals']; |
263
|
|
|
} |
264
|
|
|
else |
265
|
|
|
{ |
266
|
|
|
$true = !empty($args['test']); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
# |
270
|
|
|
# if the evaluation is not empty (0 or ''), we publish the template |
271
|
|
|
# |
272
|
|
|
|
273
|
|
|
return $true ? $patron($template) : null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Selects one among a number of possible alternatives. |
278
|
|
|
* |
279
|
|
|
* <pre> |
280
|
|
|
* <!-- Category: instruction --> |
281
|
|
|
* <p:choose> |
282
|
|
|
* <!-- Content: (p:when+, p:otherwise?) --> |
283
|
|
|
* </p:choose> |
284
|
|
|
* |
285
|
|
|
* <p:when |
286
|
|
|
* test = boolean-expression> |
287
|
|
|
* <!-- Content: template --> |
288
|
|
|
* </p:when> |
289
|
|
|
* |
290
|
|
|
* <p:otherwise> |
291
|
|
|
* <!-- Content: template --> |
292
|
|
|
* </p:otherwise> |
293
|
|
|
* </pre> |
294
|
|
|
* |
295
|
|
|
* It consists of a sequence of `p:when` elements followed by an optional `p:otherwise` |
296
|
|
|
* element. Each `p:when` element has a single attribute, test, which specifies an expression. |
297
|
|
|
* The content of the `p:when` and `p:otherwise` elements is a template. When an `p:choose` |
298
|
|
|
* element is processed, each of the `p:when` elements is tested in turn, by evaluating the |
299
|
|
|
* expression and converting the resulting object to a boolean as if by a call to the boolean |
300
|
|
|
* function. The content of the first, and only the first, `p:when` element whose test is true |
301
|
|
|
* is instantiated. If no `p:when` is true, the content of the `p:otherwise` element is |
302
|
|
|
* instantiated. If no `p:when` element is true, and no `p:otherwise` element is present, |
303
|
|
|
* nothing is created. |
304
|
|
|
* |
305
|
|
|
* @param array $args |
306
|
|
|
* @param Engine $patron |
307
|
|
|
* @param mixed $template |
308
|
|
|
* |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
|
|
static public function markup_choose(array $args, Engine $patron, $template) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$otherwise = null; |
314
|
|
|
|
315
|
|
|
# |
316
|
|
|
# handle 'when' children as they are defined. |
317
|
|
|
# if we find an 'otherwise' we keep it for later |
318
|
|
|
# |
319
|
|
|
|
320
|
|
|
foreach ($template as $node) |
321
|
|
|
{ |
322
|
|
|
$name = $node->name; |
323
|
|
|
|
324
|
|
|
if ($name == 'otherwise') |
325
|
|
|
{ |
326
|
|
|
$otherwise = $node; |
327
|
|
|
|
328
|
|
|
continue; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if ($name != 'when') |
332
|
|
|
{ |
333
|
|
|
$patron->error('Unexpected child: :node', [ ':node' => $node ]); |
334
|
|
|
|
335
|
|
|
return null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$value = $patron->evaluate($node->args['test'], true); |
339
|
|
|
|
340
|
|
|
if ($value) |
341
|
|
|
{ |
342
|
|
|
return $patron($node->nodes); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
# |
347
|
|
|
# otherwise |
348
|
|
|
# |
349
|
|
|
|
350
|
|
|
if (!$otherwise) |
351
|
|
|
{ |
352
|
|
|
return null; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $patron($otherwise->nodes); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Binds a name to a value. |
360
|
|
|
* |
361
|
|
|
* <pre> |
362
|
|
|
* <!-- Category: top-level-element --> |
363
|
|
|
* <!-- Category: instruction --> |
364
|
|
|
* <p:variable |
365
|
|
|
* name = qname |
366
|
|
|
* select = expression> |
367
|
|
|
* <!-- Content: p:with-param*, template? --> |
368
|
|
|
* </p:variable> |
369
|
|
|
* |
370
|
|
|
* <!-- Category: top-level-element --> |
371
|
|
|
* <p:param |
372
|
|
|
* name = qname |
373
|
|
|
* select = expression> |
374
|
|
|
* <!-- Content: template? --> |
375
|
|
|
* </p:param> |
376
|
|
|
* </pre> |
377
|
|
|
* |
378
|
|
|
* The value to which a variable is bound (the value of the variable) can be an object of any |
379
|
|
|
* of the types that can be returned by expressions. There are two elements that can be used |
380
|
|
|
* to bind variables: `p:variable` and `p:with-param`. The difference is that the value |
381
|
|
|
* specified on the `p:with-param` variable is only a default value for the binding; when |
382
|
|
|
* the template within which the `p:with-param` element occurs is invoked, parameters may |
383
|
|
|
* be passed that are used in place of the default values. |
384
|
|
|
* |
385
|
|
|
* Both `p:variable` and `p:with-param` have a required name attribute, which specifies the |
386
|
|
|
* name of the variable. The value of the name attribute is a qualified name. |
387
|
|
|
* |
388
|
|
|
* Example: |
389
|
|
|
* |
390
|
|
|
* <pre> |
391
|
|
|
* <p:variable name="count" select="@comments_count" /> |
392
|
|
|
* <p:variable name="count">There are #{@comments_count} comments</p:variable> |
393
|
|
|
* </pre> |
394
|
|
|
* |
395
|
|
|
* @param array $args |
396
|
|
|
* @param Engine $patron |
397
|
|
|
* @param mixed $template |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
static public function markup_variable(array $args, Engine $patron, $template) |
402
|
|
|
{ |
403
|
|
|
$select = $args['select']; |
404
|
|
|
|
405
|
|
|
if ($select && $template) |
406
|
|
|
{ |
407
|
|
|
$patron->error('Ambiguous selection'); |
408
|
|
|
|
409
|
|
|
return null; |
410
|
|
|
} |
411
|
|
|
else if ($select) |
412
|
|
|
{ |
413
|
|
|
$value = $select; |
414
|
|
|
} |
415
|
|
|
else |
416
|
|
|
{ |
417
|
|
|
$value = $patron($template); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
$name = $args['name']; |
421
|
|
|
|
422
|
|
|
$patron->context[$name] = $value; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Parses a template with a bounded value. |
427
|
|
|
* |
428
|
|
|
* <pre> |
429
|
|
|
* <p:with |
430
|
|
|
* select = expression> |
431
|
|
|
* <!-- Content: p:with-param*, template --> |
432
|
|
|
* </p:with> |
433
|
|
|
* </pre> |
434
|
|
|
* |
435
|
|
|
* Example: |
436
|
|
|
* |
437
|
|
|
* <pre> |
438
|
|
|
* <p:with select="articles.first().comments.last()"> |
439
|
|
|
* Last comment: <a href="#{@url}">#{@title}</a> |
440
|
|
|
* </p:with> |
441
|
|
|
* </pre> |
442
|
|
|
* |
443
|
|
|
* @param array $args |
444
|
|
|
* @param Engine $patron |
445
|
|
|
* @param mixed $template |
446
|
|
|
* |
447
|
|
|
* @return string |
448
|
|
|
*/ |
449
|
|
|
static public function markup_with(array $args, Engine $patron, $template) |
450
|
|
|
{ |
451
|
|
|
if ($template === null) |
452
|
|
|
{ |
453
|
|
|
$patron->error('A template is required.'); |
454
|
|
|
|
455
|
|
|
return null; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$select = $args['select']; |
459
|
|
|
|
460
|
|
|
return $patron($template, $select); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Translates and interpolates a string. |
465
|
|
|
* |
466
|
|
|
* <pre> |
467
|
|
|
* <p:translate |
468
|
|
|
* native = string> |
469
|
|
|
* <!-- Content: p:with-param* --> |
470
|
|
|
* </p:translate> |
471
|
|
|
* </pre> |
472
|
|
|
* |
473
|
|
|
* The arguments for the interpolation are provided using the attributes of the markup, or the |
474
|
|
|
* `with-param` construction. |
475
|
|
|
* |
476
|
|
|
* Example: |
477
|
|
|
* |
478
|
|
|
* <pre> |
479
|
|
|
* <p:translate native="Posted on :date by !name"> |
480
|
|
|
* <p:with-param name="date"><time datetime="#{@date}" pubdate="pubdate">#{@date.format_date()}</time></p:with-param> |
481
|
|
|
* <p:with-param name="name" select="@user.name" /> |
482
|
|
|
* </p:translate> |
483
|
|
|
* </pre> |
484
|
|
|
* |
485
|
|
|
* @param array $args |
486
|
|
|
* @param Engine $patron |
487
|
|
|
* @param mixed $template |
488
|
|
|
* |
489
|
|
|
* @return string |
490
|
|
|
*/ |
491
|
|
|
static public function markup_translate(array $args, Engine $patron, $template) |
|
|
|
|
492
|
|
|
{ |
493
|
|
|
$native = $args['native']; |
494
|
|
|
|
495
|
|
|
return call_user_func('ICanBoogie\I18n\t', $native, $args); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Decorates a content with a template. |
500
|
|
|
* |
501
|
|
|
* <pre> |
502
|
|
|
* <p:decorate |
503
|
|
|
* with = string> |
504
|
|
|
* <!-- Content: p:with-param*, template --> |
505
|
|
|
* </p:decorate> |
506
|
|
|
* </pre> |
507
|
|
|
* |
508
|
|
|
* The content of the markup is rendered to create the component to decorate, it is then passed |
509
|
|
|
* to the decorating template as the `component` variable. |
510
|
|
|
* |
511
|
|
|
* The name of the decorating template is specified with the `with` attribute, and is |
512
|
|
|
* interpolated e.g. if "page" is specified the template name "@page" is used; if "admin/page" |
513
|
|
|
* is specified the template name "admin/@page" is used. |
514
|
|
|
* |
515
|
|
|
* The parameters specified using `with-param` are all turned into variables. |
516
|
|
|
* |
517
|
|
|
* Example: |
518
|
|
|
* |
519
|
|
|
* <pre> |
520
|
|
|
* <p:decorate with="page"> |
521
|
|
|
* <p:page:content id="body" /> |
522
|
|
|
* </p:decorate> |
523
|
|
|
* </pre> |
524
|
|
|
* |
525
|
|
|
* @param array $args |
526
|
|
|
* @param Engine $engine |
527
|
|
|
* @param mixed $template |
528
|
|
|
* |
529
|
|
|
* @return string |
530
|
|
|
*/ |
531
|
|
|
static public function markup_decorate(array $args, Engine $engine, $template) |
532
|
|
|
{ |
533
|
|
|
$template_name = $args['with']; |
534
|
|
|
|
535
|
|
|
$rendered_template = $engine($template); |
536
|
|
|
|
537
|
|
|
unset($args['with']); |
538
|
|
|
|
539
|
|
|
$engine->context['component'] = $rendered_template; |
540
|
|
|
|
541
|
|
|
foreach ($args as $name => $value) |
542
|
|
|
{ |
543
|
|
|
$engine->context[$name] = $value; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
return $engine->callTemplate(TemplateName::from($template_name)->as_layout, $args); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/* |
550
|
|
|
* Brickrouge |
551
|
|
|
*/ |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* CSS assets can be collected and rendered into `LINK` elements with the `p:document:css` |
555
|
|
|
* element. The `href` attribute is used to add an asset to the collection. The `weight` |
556
|
|
|
* attribute specifies the weight of that asset. If the `weight` attribute is not specified, |
557
|
|
|
* the weight of the asset is defaulted to 100. If the `href` attribute is not specified, |
558
|
|
|
* the assets are rendered. If a template is specified the collection is passed as `this`, |
559
|
|
|
* otherwise the collection is rendered into an HTML string of `LINK` elements. |
560
|
|
|
* |
561
|
|
|
* <pre> |
562
|
|
|
* <p:document:css |
563
|
|
|
* href = string |
564
|
|
|
* weight = int> |
565
|
|
|
* <!-- Content: p:with-params, template? --> |
566
|
|
|
* </p:document:css> |
567
|
|
|
* </pre> |
568
|
|
|
* |
569
|
|
|
* Example: |
570
|
|
|
* |
571
|
|
|
* <pre> |
572
|
|
|
* <p:document:css href="/public/page.css" /> |
573
|
|
|
* <p:document:css href="/public/reset.css" weight="-100" /> |
574
|
|
|
* <p:document:css /> |
575
|
|
|
* </pre> |
576
|
|
|
* |
577
|
|
|
* will produce: |
578
|
|
|
* |
579
|
|
|
* <pre> |
580
|
|
|
* <link href="/public/reset.css" type="text/css" rel="stylesheet" /> |
581
|
|
|
* <link href="/public/page.css" type="text/css" rel="stylesheet" /> |
582
|
|
|
* </pre> |
583
|
|
|
* |
584
|
|
|
* @param array $args |
585
|
|
|
* @param Engine $engine |
586
|
|
|
* @param mixed $template |
587
|
|
|
* |
588
|
|
|
* @return void|string |
589
|
|
|
*/ |
590
|
|
View Code Duplication |
static public function markup_document_css(array $args, Engine $engine, $template) |
|
|
|
|
591
|
|
|
{ |
592
|
|
|
$document = \Brickrouge\get_document(); |
593
|
|
|
|
594
|
|
|
if (isset($args['href'])) |
595
|
|
|
{ |
596
|
|
|
$document->css->add($args['href'], $args['weight'], dirname($engine->get_file())); |
597
|
|
|
|
598
|
|
|
return null; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
return $template ? $engine($template, $document->css) : (string) $document->css; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* JavaScript assets can be collected and rendered into `SCRIPT` elements with the `p:document:js` |
606
|
|
|
* element. The `href` attribute is used to add an asset to the collection. The `weight` |
607
|
|
|
* attribute specifies the weight of that asset. If the `weight` attribute is not specified, |
608
|
|
|
* the weight of the asset is defaulted to 100. If the `href` attribute is not specified, |
609
|
|
|
* the assets are rendered. If a template is specified the collection is passed as `this`, |
610
|
|
|
* otherwise the collection is rendered into an HTML string of `SCRIPT` elements. |
611
|
|
|
* |
612
|
|
|
* <pre> |
613
|
|
|
* <p:document:js |
614
|
|
|
* href = string |
615
|
|
|
* weight = int> |
616
|
|
|
* <!-- Content: p:with-params, template? --> |
617
|
|
|
* </p:document:js> |
618
|
|
|
* </pre> |
619
|
|
|
* |
620
|
|
|
* Example: |
621
|
|
|
* |
622
|
|
|
* <pre> |
623
|
|
|
* <p:document:js href="/public/page.js" /> |
624
|
|
|
* <p:document:js href="/public/reset.js" weight="-100" /> |
625
|
|
|
* <p:document:js /> |
626
|
|
|
* </pre> |
627
|
|
|
* |
628
|
|
|
* will produce: |
629
|
|
|
* |
630
|
|
|
* <pre> |
631
|
|
|
* <script src="/public/reset.css" type="text/javascript"></script> |
632
|
|
|
* <script src="/public/page.css" type="text/javascript"></script> |
633
|
|
|
* </pre> |
634
|
|
|
* |
635
|
|
|
* @param array $args |
636
|
|
|
* @param Engine $engine |
637
|
|
|
* @param mixed $template |
638
|
|
|
* |
639
|
|
|
* @return void|string |
640
|
|
|
*/ |
641
|
|
View Code Duplication |
static public function markup_document_js(array $args, Engine $engine, $template) |
|
|
|
|
642
|
|
|
{ |
643
|
|
|
$document = \Brickrouge\get_document(); |
644
|
|
|
|
645
|
|
|
if (isset($args['href'])) |
646
|
|
|
{ |
647
|
|
|
$document->js->add($args['href'], $args['weight'], dirname($engine->get_file())); |
648
|
|
|
|
649
|
|
|
return null; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
return $template ? $engine($template, $document->js) : (string) $document->js; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/* |
656
|
|
|
* ICanBoogie |
657
|
|
|
*/ |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Synthesizes the "patron.markups" config. |
661
|
|
|
* |
662
|
|
|
* @param array $fragments |
663
|
|
|
* |
664
|
|
|
* @return array |
665
|
|
|
*/ |
666
|
|
View Code Duplication |
static public function synthesize_markups_config(array $fragments) |
|
|
|
|
667
|
|
|
{ |
668
|
|
|
$markups = []; |
669
|
|
|
|
670
|
|
|
foreach ($fragments as $path => $fragment) |
671
|
|
|
{ |
672
|
|
|
if (empty($fragment['patron.markups'])) |
673
|
|
|
{ |
674
|
|
|
continue; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
$markups = array_merge($markups, $fragment['patron.markups']); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
return $markups; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Synthesizes the "patron.functions" config. |
685
|
|
|
* |
686
|
|
|
* @param array $fragments |
687
|
|
|
* |
688
|
|
|
* @return array |
689
|
|
|
*/ |
690
|
|
View Code Duplication |
static public function synthesize_functions_config(array $fragments) |
|
|
|
|
691
|
|
|
{ |
692
|
|
|
$functions = []; |
693
|
|
|
|
694
|
|
|
foreach ($fragments as $path => $fragment) |
695
|
|
|
{ |
696
|
|
|
if (empty($fragment['patron.functions'])) |
697
|
|
|
{ |
698
|
|
|
continue; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
$functions = array_merge($functions, $fragment['patron.functions']); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
return $functions; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Attaches event hooks to `MarkupCollection::alter` and `FunctionCollection::alter` in order |
709
|
|
|
* to add the markups and functions defined in the `patron.markups` and `patron.function` |
710
|
|
|
* configs. |
711
|
|
|
* |
712
|
|
|
* @param Core\BootEvent $event |
713
|
|
|
* @param Core $app |
714
|
|
|
*/ |
715
|
|
|
static public function on_core_boot(Core\BootEvent $event, Core $app) |
|
|
|
|
716
|
|
|
{ |
717
|
|
|
$app->events->attach(function(MarkupCollection\AlterEvent $event, MarkupCollection $markups) use ($app) { |
718
|
|
|
|
719
|
|
|
foreach((array) $app->configs['patron.markups'] as $name => $definition) |
720
|
|
|
{ |
721
|
|
|
$markups[$name] = $definition + [ 1 => [ ] ]; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
}); |
725
|
|
|
|
726
|
|
|
$app->events->attach(function(FunctionCollection\AlterEvent $event, FunctionCollection $markups) use ($app) { |
727
|
|
|
|
728
|
|
|
foreach((array) $app->configs['patron.functions'] as $name => $definition) |
729
|
|
|
{ |
730
|
|
|
$markups[$name] = $definition; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
}); |
734
|
|
|
} |
735
|
|
|
} |
736
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.