|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the xAPI package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Christian Flothmann <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace XApi\LrsBundle\Response; |
|
15
|
|
|
|
|
16
|
|
|
use LogicException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Jérôme Parmentier <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class MultipartResponse extends Response |
|
25
|
|
|
{ |
|
26
|
|
|
protected $subtype; |
|
27
|
|
|
protected $boundary; |
|
28
|
|
|
protected $statementPart; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Response[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $parts; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param AttachmentResponse[] $attachmentsParts |
|
37
|
|
|
* @param int $status |
|
38
|
|
|
* @param string|null $subtype |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(JsonResponse $statementPart, array $attachmentsParts = [], $status = 200, array $headers = [], $subtype = null) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct(null, $status, $headers); |
|
43
|
|
|
|
|
44
|
|
|
if (null === $subtype) { |
|
45
|
|
|
$subtype = 'mixed'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->subtype = $subtype; |
|
49
|
|
|
$this->boundary = uniqid('', true); |
|
50
|
|
|
$this->statementPart = $statementPart; |
|
51
|
|
|
|
|
52
|
|
|
$this->setAttachmentsParts($attachmentsParts); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function addAttachmentPart(AttachmentResponse $part) |
|
59
|
|
|
{ |
|
60
|
|
|
if (null !== $part->getContent()) { |
|
61
|
|
|
$this->parts[] = $part; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param AttachmentResponse[] $attachmentsParts |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setAttachmentsParts(array $attachmentsParts) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->parts = [$this->statementPart]; |
|
75
|
|
|
|
|
76
|
|
|
foreach ($attachmentsParts as $part) { |
|
77
|
|
|
$this->addAttachmentPart($part); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function prepare(Request $request) |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($this->parts as $part) { |
|
86
|
|
|
$part->prepare($request); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->headers->set('Content-Type', sprintf('multipart/%s; boundary="%s"', $this->subtype, $this->boundary)); |
|
90
|
|
|
$this->headers->set('Transfer-Encoding', 'chunked'); |
|
91
|
|
|
|
|
92
|
|
|
return parent::prepare($request); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function sendContent() |
|
96
|
|
|
{ |
|
97
|
|
|
$content = ''; |
|
98
|
|
|
foreach ($this->parts as $part) { |
|
99
|
|
|
$content .= sprintf('--%s', $this->boundary)."\r\n"; |
|
100
|
|
|
$content .= $part->headers."\r\n"; |
|
101
|
|
|
$content .= $part->getContent(); |
|
102
|
|
|
$content .= "\r\n"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$content .= sprintf('--%s--', $this->boundary)."\r\n"; |
|
106
|
|
|
|
|
107
|
|
|
echo $content; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @throws LogicException when the content is not null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setContent($content): void |
|
116
|
|
|
{ |
|
117
|
|
|
if (null !== $content) { |
|
118
|
|
|
throw new LogicException('The content cannot be set on a MultipartResponse instance.'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return false |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getContent() |
|
126
|
|
|
{ |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|