|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PdfStrategy file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Leandro Silva <[email protected]> |
|
6
|
|
|
* @category LosPdf |
|
7
|
|
|
* @license https://github.com/Lansoweb/LosPdf/blob/master/LICENSE BSD-3 License |
|
8
|
|
|
* @link http://github.com/LansoWeb/LosPdf |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace LosPdf\View\Strategy; |
|
11
|
|
|
|
|
12
|
|
|
use Zend\EventManager\EventManagerInterface; |
|
13
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
|
14
|
|
|
use Zend\View\ViewEvent; |
|
15
|
|
|
use LosPdf\View\Renderer\AbstractRenderer; |
|
16
|
|
|
use LosPdf\View\Model\PdfModel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* PdfStrategy class |
|
20
|
|
|
* |
|
21
|
|
|
* @author Leandro Silva <[email protected]> |
|
22
|
|
|
* @category LosPdf |
|
23
|
|
|
* @license https://github.com/Lansoweb/LosPdf/blob/master/LICENSE BSD-3 License |
|
24
|
|
|
* @link http://github.com/LansoWeb/LosPdf |
|
25
|
|
|
*/ |
|
26
|
|
|
class PdfStrategy implements ListenerAggregateInterface |
|
27
|
|
|
{ |
|
28
|
|
|
protected $listeners = []; |
|
29
|
|
|
|
|
30
|
|
|
protected $renderer; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(AbstractRenderer $renderer) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->renderer = $renderer; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function attach(EventManagerInterface $events, $priority = 1) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->listeners[] = $events->attach(ViewEvent::EVENT_RENDERER, [$this, 'selectRenderer'], $priority); |
|
40
|
|
|
$this->listeners[] = $events->attach(ViewEvent::EVENT_RESPONSE, [$this, 'injectResponse'], $priority); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function detach(EventManagerInterface $events) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach ($this->listeners as $index => $listener) { |
|
46
|
|
|
if ($events->detach($listener)) { |
|
47
|
|
|
unset($this->listeners[$index]); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function selectRenderer(ViewEvent $e) |
|
53
|
|
|
{ |
|
54
|
|
|
$model = $e->getModel(); |
|
55
|
|
|
|
|
56
|
|
|
if ($model instanceof PdfModel) { |
|
57
|
|
|
return $this->renderer; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function injectResponse(ViewEvent $e) |
|
64
|
|
|
{ |
|
65
|
|
|
$renderer = $e->getRenderer(); |
|
66
|
|
|
if ($renderer !== $this->renderer) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$result = $e->getResult(); |
|
71
|
|
|
|
|
72
|
|
|
if (!is_string($result)) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$response = $e->getResponse(); |
|
77
|
|
|
$response->setContent($result); |
|
78
|
|
|
$response->getHeaders()->addHeaderLine('content-type', 'application/pdf'); |
|
79
|
|
|
|
|
80
|
|
|
$fileName = $e->getModel()->getOption('filename'); |
|
|
|
|
|
|
81
|
|
|
if (isset($fileName)) { |
|
82
|
|
|
if (substr($fileName, -4) != '.pdf') { |
|
83
|
|
|
$fileName .= '.pdf'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$response->getHeaders()->addHeaderLine( |
|
87
|
|
|
'Content-Disposition', |
|
88
|
|
|
'attachment; filename='.$fileName); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.