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\Strategy; |
12
|
|
|
|
13
|
|
|
use WebinoDraw\Exception; |
14
|
|
|
use Zend\Http\PhpEnvironment\Response; |
15
|
|
|
use Zend\View\ViewEvent; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Draw XHTML with this view strategy |
19
|
|
|
*/ |
20
|
|
|
class DrawStrategy extends AbstractDrawStrategy |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Application service name |
24
|
|
|
*/ |
25
|
|
|
const SERVICE = 'WebinoDrawStrategy'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ViewEvent $event |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function injectResponse(ViewEvent $event) |
32
|
|
|
{ |
33
|
|
|
if (!$this->shouldRespond($event)) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/* @var $response Response */ |
38
|
|
|
$options = $this->draw->getOptions(); |
39
|
|
|
$response = $event->getResponse(); |
40
|
|
|
$body = $response->getBody(); |
41
|
|
|
$spec = $options->getInstructions(); |
42
|
|
|
$model = $event->getModel(); |
43
|
|
|
$isXml = $this->resolveIsXml($response); |
|
|
|
|
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$response->setContent( |
47
|
|
|
$this->draw->draw( |
48
|
|
|
$body, |
49
|
|
|
$spec, |
50
|
|
|
$this->collectModelVariables($model), |
|
|
|
|
51
|
|
|
$isXml |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
} catch (Exception\DrawException $exc) { |
56
|
|
|
$_exc = $exc; |
57
|
|
|
while ($subExc = $_exc->getPrevious()) { |
58
|
|
|
$_exc = $subExc; |
59
|
|
|
|
60
|
|
|
if ($subExc instanceof Exception\ExceptionalDrawExceptionInterface) { |
61
|
|
|
$model->setVariables($subExc->getDrawVariables()); |
62
|
|
|
$response->setContent( |
63
|
|
|
$this->draw->draw( |
64
|
|
|
$body, |
65
|
|
|
$spec, |
66
|
|
|
$this->collectModelVariables($model), |
|
|
|
|
67
|
|
|
$isXml |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
throw $exc; |
74
|
|
|
|
75
|
|
|
} catch (\Exception $exc) { |
76
|
|
|
throw $exc; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Response $response |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
private function resolveIsXml(Response $response) |
85
|
|
|
{ |
86
|
|
|
$contentType = $response->getHeaders()->get('content-type'); |
87
|
|
|
if (empty($contentType)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return ('text/xml' === $contentType->getMediaType()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: