|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
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 Jitamin\Helper; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Jitamin\Foundation\Base; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Template Hook helpers. |
|
19
|
|
|
*/ |
|
20
|
|
|
class HookHelper extends Base |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Add assets JS or CSS. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $type |
|
26
|
|
|
* @param string $hook |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function asset($type, $hook) |
|
31
|
|
|
{ |
|
32
|
|
|
$buffer = ''; |
|
33
|
|
|
|
|
34
|
|
|
foreach ($this->hook->getListeners($hook) as $params) { |
|
|
|
|
|
|
35
|
|
|
$buffer .= $this->helper->asset->$type($params['template']); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $buffer; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Render all attached hooks. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $hook |
|
45
|
|
|
* @param array $variables |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function render($hook, array $variables = []) |
|
50
|
|
|
{ |
|
51
|
|
|
$buffer = ''; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->hook->getListeners($hook) as $params) { |
|
|
|
|
|
|
54
|
|
|
if (!empty($params['variables'])) { |
|
55
|
|
|
$variables = array_merge($variables, $params['variables']); |
|
56
|
|
|
} elseif (!empty($params['callable'])) { |
|
57
|
|
|
$result = call_user_func_array($params['callable'], $variables); |
|
58
|
|
|
|
|
59
|
|
|
if (is_array($result)) { |
|
60
|
|
|
$variables = array_merge($variables, $result); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$buffer .= $this->template->render($params['template'], $variables); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $buffer; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Attach a template to a hook. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $hook |
|
74
|
|
|
* @param string $template |
|
75
|
|
|
* @param array $variables |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function attach($hook, $template, array $variables = []) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->hook->on($hook, [ |
|
|
|
|
|
|
82
|
|
|
'template' => $template, |
|
83
|
|
|
'variables' => $variables, |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Attach a template to a hook with a callable. |
|
91
|
|
|
* |
|
92
|
|
|
* Arguments passed to the callback are the one passed to the hook |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $hook |
|
95
|
|
|
* @param string $template |
|
96
|
|
|
* @param Closure $callable |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function attachCallable($hook, $template, Closure $callable) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->hook->on($hook, [ |
|
|
|
|
|
|
103
|
|
|
'template' => $template, |
|
104
|
|
|
'callable' => $callable, |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.