1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright https://yawik.org/COPYRIGHT.php |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Core\View\Helper; |
12
|
|
|
|
13
|
|
|
use Core\View\Helper\Proxy\HelperProxy; |
14
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
15
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
16
|
|
|
use Laminas\View\Helper\AbstractHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* View helper to safely use module specific view helpers in other modules. |
20
|
|
|
* |
21
|
|
|
* In most scenarios, to make something failsafe, it takes a lot of (redundant) code. |
22
|
|
|
* With this proxy helper, this can be simplified by just adding a few characters. |
23
|
|
|
* |
24
|
|
|
* <pre> |
25
|
|
|
* |
26
|
|
|
* // |
27
|
|
|
* // ORIGINAL |
28
|
|
|
* // |
29
|
|
|
* <?=$this->helper()->someOutput()?> |
30
|
|
|
* |
31
|
|
|
* // |
32
|
|
|
* // Failsafe code |
33
|
|
|
* // |
34
|
|
|
* <? if ($this->getHelperPluginManager()->has('plugin')): |
35
|
|
|
* $plugin = $this->getHelperPluginManager()->get('helper'); |
36
|
|
|
* |
37
|
|
|
* // do something with the plugin, i.E. call some method |
38
|
|
|
* echo $plugin->someOutput(); |
39
|
|
|
* else: |
40
|
|
|
* // helper does not exist. |
41
|
|
|
* // do something to hanlde this case. |
42
|
|
|
* echo ''; |
43
|
|
|
* endif; |
44
|
|
|
* ?> |
45
|
|
|
* |
46
|
|
|
* // |
47
|
|
|
* // Failsafe with Proxy: |
48
|
|
|
* // |
49
|
|
|
* <?=$this->proxy('helper')->someOutput()?> |
50
|
|
|
* |
51
|
|
|
* // Proxy view helper will return a {@link \Core\View\Helper\Proxy\HelperProxy} instance |
52
|
|
|
* // which will return null on every method call, if the helper does not exist. |
53
|
|
|
* <pre> |
54
|
|
|
* |
55
|
|
|
* Advanced usage: |
56
|
|
|
* |
57
|
|
|
* - Load a helper without risking an exception: |
58
|
|
|
* This will return a {@link \Core\View\Helper\Proxy\HelperProxy} instance. |
59
|
|
|
* <pre><? $helper = $this->proxy()->plugin('HelperServiceName', [options])?></pre> |
60
|
|
|
* |
61
|
|
|
* - Call an invokable helper and expect an array returned. |
62
|
|
|
* This will return an empty array, if the helper does not exist. |
63
|
|
|
* <pre><? $array = $this->proxy('helper', Proxy::EXPECT_ARRAY)?></pre> |
64
|
|
|
* |
65
|
|
|
* - Expect other return types: |
66
|
|
|
* <pre> |
67
|
|
|
* // Object (which is default) |
68
|
|
|
* $helper = $this->proxy('helper', Proxy::EXPECT_OBJECT); |
69
|
|
|
* |
70
|
|
|
* // Iterator (returns an IteratorAggregate which returns an empty ArrayIterator. |
71
|
|
|
* $helper = $this->proxy('helper', Proxy::EXPECT_ITERATOR); |
72
|
|
|
* |
73
|
|
|
* // Skalar values: Simply specify the expected result, which will be returned, |
74
|
|
|
* // if the helper does not exist. |
75
|
|
|
* $value = $this->proxy('helper', ''); // return empty string. |
76
|
|
|
* $value = $this->proxy('helper', false) // return FALSE |
77
|
|
|
* </pre> |
78
|
|
|
* |
79
|
|
|
* - Directly calls an invokable helper with Arguments |
80
|
|
|
* <pre> |
81
|
|
|
* <?=$this->proxy('invokableHelper', ['arg1', 'arg2'], false)?> |
82
|
|
|
* </pre> |
83
|
|
|
* |
84
|
|
|
* - Call a method on a plugin |
85
|
|
|
* (see {@link HelperProxy::call()}) |
86
|
|
|
* <pre> |
87
|
|
|
* $this->proxy('helper')->call('method', ['arg1',...], 'expectedValue'); |
88
|
|
|
* </pre> |
89
|
|
|
* |
90
|
|
|
* - Call consecutive methods on plugin |
91
|
|
|
* (see {@link HelperProxy::consecutive()}) |
92
|
|
|
* <pre> |
93
|
|
|
* $this->proxy('helper')->consecutive(['method1', 'method2' => ['arg1', ... ], ...]); |
94
|
|
|
* </pre> |
95
|
|
|
* |
96
|
|
|
* - Call chained methods on plugin |
97
|
|
|
* (see {@link HelperProxy::chain()}) |
98
|
|
|
* <pre> |
99
|
|
|
* $this->proxy('helper')->chain(['method' => ['arg'], 'method2'], 'expectedValue'); |
100
|
|
|
* </pre> |
101
|
|
|
* |
102
|
|
|
* @author Mathias Gelhausen <[email protected]> |
103
|
|
|
* @since 0.29 |
104
|
|
|
*/ |
105
|
|
|
class Proxy extends AbstractHelper |
106
|
|
|
{ |
107
|
|
|
/** |
108
|
|
|
* Loads and possiblity executes a view helper. |
109
|
|
|
* |
110
|
|
|
* @param null|string $plugin |
111
|
|
|
* @param array|string $args |
112
|
|
|
* @param mixed $expect |
113
|
|
|
* |
114
|
|
|
* @return mixed|self|ProxyNoopHelper|ProxyNoopIterator |
|
|
|
|
115
|
|
|
*/ |
116
|
3 |
|
public function __invoke($plugin = null, $args = null, $expect = HelperProxy::EXPECT_SELF) |
117
|
|
|
{ |
118
|
3 |
|
if (null === $plugin) { |
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
$plugin = $this->plugin($plugin); |
123
|
|
|
|
124
|
2 |
|
if (null === $args) { |
125
|
1 |
|
return $plugin; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return $plugin->call('__invoke', $args, $expect); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Loads a plugin from the plugin helper manager. |
133
|
|
|
* |
134
|
|
|
* Returns false, if either the helper plugin manager cannot be |
135
|
|
|
* retrieved from the renderer or the requested plugin does not exist. |
136
|
|
|
* |
137
|
|
|
* @param string $plugin |
138
|
|
|
* @param true|array $options if true, only return if plugin exists or not. |
139
|
|
|
* |
140
|
|
|
* @return bool|HelperProxy |
141
|
|
|
*/ |
142
|
5 |
|
public function plugin($plugin, $options = null) |
143
|
|
|
{ |
144
|
5 |
|
$renderer = $this->getView(); |
145
|
|
|
|
146
|
5 |
|
if (!method_exists($renderer, 'getHelperPluginManager')) { |
147
|
1 |
|
return true === $options ? false : new HelperProxy(false); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/* @var \Laminas\View\HelperPluginManager $manager */ |
151
|
4 |
|
$manager = $renderer->getHelperPluginManager(); |
152
|
4 |
|
$hasPlugin = $manager->has($plugin); |
153
|
|
|
|
154
|
4 |
|
if (true === $options) { |
155
|
2 |
|
return $hasPlugin; |
156
|
|
|
} |
157
|
|
|
|
158
|
3 |
|
if ($hasPlugin) { |
159
|
2 |
|
$pluginInstance = $manager->get($plugin, $options); |
160
|
|
|
} else { |
161
|
1 |
|
$pluginInstance = false; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
return new HelperProxy($pluginInstance); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Does a plugin exists? |
169
|
|
|
* |
170
|
|
|
* @param string $plugin |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
2 |
|
public function exists($plugin) |
175
|
|
|
{ |
176
|
2 |
|
$retVal = $this->plugin($plugin, true); |
177
|
2 |
|
return is_null($retVal) ? false:$retVal; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths