|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDraw\View\Renderer; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\View\Exception; |
|
14
|
|
|
use Zend\View\Model\ModelInterface as Model; |
|
15
|
|
|
use Zend\View\Renderer\PhpRenderer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Uses file_get_contents() instead of include |
|
19
|
|
|
* |
|
20
|
|
|
* <pre> |
|
21
|
|
|
* 'service_manager' => [ |
|
22
|
|
|
* 'factories' => [ |
|
23
|
|
|
* 'HttpViewManager' => 'WebinoDraw\Mvc\Service\HttpViewManagerFactory', |
|
24
|
|
|
* ], |
|
25
|
|
|
* ], |
|
26
|
|
|
* </pre> |
|
27
|
|
|
*/ |
|
28
|
|
|
class NotPhpRenderer extends PhpRenderer |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
private $templates = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function render($nameOrModel, $values = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($nameOrModel instanceof Model) { |
|
41
|
|
|
$model = $nameOrModel; |
|
42
|
|
|
$nameOrModel = $model->getTemplate(); |
|
43
|
|
|
|
|
44
|
|
|
if (empty($nameOrModel)) { |
|
45
|
|
|
throw new Exception\DomainException(sprintf( |
|
46
|
|
|
'%s: received View Model argument, but template is empty', |
|
47
|
|
|
__METHOD__ |
|
48
|
|
|
)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Give view model awareness via ViewModel helper |
|
52
|
|
|
$this->plugin('view_model')->setCurrent($model); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// find the script file name using the parent private method |
|
56
|
|
|
$this->addTemplate($nameOrModel); |
|
57
|
|
|
unset($nameOrModel); |
|
58
|
|
|
|
|
59
|
|
|
$template = array_pop($this->templates); |
|
60
|
|
|
$file = $this->resolver($template); |
|
61
|
|
|
|
|
62
|
|
|
if (!$file) { |
|
63
|
|
|
throw new Exception\RuntimeException(sprintf( |
|
64
|
|
|
'%s: Unable to render template "%s"; resolver could not resolve to a file', |
|
65
|
|
|
__METHOD__, |
|
66
|
|
|
$template |
|
67
|
|
|
)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (false === is_file($file)) { |
|
71
|
|
|
throw new Exception\UnexpectedValueException(sprintf( |
|
72
|
|
|
'%s: Unable to render template "%s"; file not found', |
|
73
|
|
|
__METHOD__, |
|
74
|
|
|
$file |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->getFilterChain()->filter(file_get_contents($file)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function addTemplate($template) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->templates[] = $template; |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: