-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForceCharsetPlugin.php
42 lines (40 loc) · 1.2 KB
/
ForceCharsetPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
require __DIR__ . '/vendor/autoload.php';
use Guzzle\Common\Event;
use Guzzle\Http\Message\Header;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ForceCharsetPlugin implements EventSubscriberInterface
{
private $forcedCharset = 'utf8';
public static function getSubscribedEvents()
{
return array(
'request.complete' => 'onRequestComplete'
);
}
public function setForcedCharset($charset)
{
$this->forcedCharset = $charset;
return $this;
}
public function getForcedCharset()
{
return $this->forcedCharset;
}
public function onRequestComplete(Event $event)
{
$response = $event['response'];
$header = $response->getHeader('content-type');
$modified = new Header(
$header->getName(),
array_map(array($this, 'modifyParams'), $header->parseParams()),
$header->getGlue()
);
$response->setHeader('content-type', $modified);
}
private function modifyParams(array $headerParams)
{
$headerParams['charset'] = $this->getForcedCharset();
return urldecode(http_build_query($headerParams, null, ';'));
}
}